Programmatically change the width of the Button in android
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) yourButton.getLayoutParams();
layoutParams.width = 150;
yourButton.setLayoutParams(layoutParams);
the "ConstraintLayout" can change up to your layout and set size by "layoutParams.width". This case it's work for me!
button.setLayoutParams(new LinearLayout.LayoutParams(10, 100));
Should have work for you.
There is one Awesome Example online for this : check this
you should apply LayoutParams
of the ViewGroup
that contains your View
. That is, if your button is inside RelativeLayout
, you should use RelativeLayout.LayoutParams
can do this
int width= (int)getResources().getDimension(R.dimen.button_width);
int heigth= (int)getResources().getDimension(R.dimen.button_heigth);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width, heigth);
button.setLayoutParams(layoutParams);
you can do it this way.
LinearLayout.LayoutParams params = button.getLayoutParams();
params.width = 100;
button.setLayoutParams(params);
but make sure you do this, after adding your button to a parent. and LinearLayout.LayoutParams
is used when parent is a LinearLayout
and when parent is RelativeLayout
use RelativeLayout.LayoutParams
.