adithi
adithi

Reputation: 973

how can we change the button size in java code

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

Answers (3)

Wajdan Ali
Wajdan Ali

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.

Watch this tutorial

Upvotes: 1

user3578286
user3578286

Reputation: 147

btn.getLayoutParams().width=300;
btn.getLayoutParams().height=100;

This will set your button width 300px button height100px.

Upvotes: 4

Dharmendra
Dharmendra

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

Related Questions