How can I set a fixed width for an Android button? Everytime I try to set a fixed width it fills the current parent (the RelativeView). Here is my XML:
The best and easiest way to set a button dynamically is
Button index=new Button(this);
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 42, getResources().getDisplayMetrics());
The height and width in pixels px. 45 being the height in dp and 42 being the width in dp.
index.setLayoutParams(new .LayoutParams(width, height));
So, for example, if you've placed your button within a TableRow within a TableLayout, you should have it as TableRow.LayoutParams
index.setLayoutParams(new TableRow.LayoutParams(width, height));