Reputation: 973
I created the button in java code and i cant change the size of the button.I used
btn = new Button(Activity.this);
btn.setText("\t\t" + lbl + "\t\t\t ");
btn.setBackgroundResource(R.drawable.blue_button);
btn.setwidth(100);
but no use.Any idea? Thanks
Upvotes: 2
Views: 16453
Reputation: 177
//converting dps to pixels dip-pixel
//username refers to edittext in the main UI thread
You need
Resources r = getResources();
int px =(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP
,200
,r.DisplayMetrics());
username.setWidth(px);
you need to convert device pixels into pixels to set the width. Here, 200 is the width of edittext UI widget in dp.
Upvotes: 1
Reputation: 147
btn.getLayoutParams().width=300;
btn.getLayoutParams().height=100;
This will set your button width 300px
button height100px
.
Upvotes: 4
Reputation: 33996
btn = new Button(Activity.this);
btn.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
btn.setText("\t\t" + lbl + "\t\t\t ");
btn.setBackgroundResource(R.drawable.blue_button);
btn.setwidth(100);
Upvotes: 4