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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With