Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set android:width to 0dp in code?

I'm trying to add a button dynamically through code.

But i'm stuck that how to set android:layout_width of button to 0dp through code.

like image 543
Arsalan Avatar asked Jan 25 '13 19:01

Arsalan


People also ask

What is Android layout width?

Android layout Width & Height In this code you can see layout_width property. This property is used to set the width of the given control. This property has following options:- Wrap_content :- when you set width as wrap_content then it will take the space according to the content.

Why to use Constraint layout?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread). As such, we are planning on enriching its API and capabilities over time.


2 Answers

Try this :

LayoutParams lp = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
myButton.setLayoutParams(lp);

Import,

import android.view.ViewGroup.LayoutParams;

Thanks

like image 159
Pratik Sharma Avatar answered Sep 30 '22 13:09

Pratik Sharma


Your button have layoutParams. Get those params, update the width and reapply them:

ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams)myButton.getLayoutParams();
lp.width = 0;
myButton.setLayoutParams(lp);
like image 24
ben75 Avatar answered Sep 30 '22 13:09

ben75