Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically change the width of the Button in android

Tags:

android

How to change the width of a Button programmatically. I am able to change the height but the width does not change. Following is my code snippet

private void createButton(final String label) {      Button button = new Button(this);     button.setText(label);       button.setWidth(10);     button.setHeight(100);      button.setOnClickListener(new OnClickListener()      {          @Override         public void onClick(View v)          {           }     });     mMenuContainer.addView(button, mMenuItemLayoutParamters);   } 

But the width of the Button occupies the width of the screen.

like image 642
Joyson Avatar asked Jul 02 '12 12:07

Joyson


1 Answers

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

like image 107
MKJParekh Avatar answered Oct 19 '22 21:10

MKJParekh