Android : Unable to change width of dynamic button

460 views Asked by At

I am trying to make dynamic buttons of fixed dimensions. I am able to alter the height but unable to change the width. The width of the button seems to MATCH_PARENT (it takes up whole screen width. If there are two buttons, each button width is half screen width and when there is only one button, button width is screen width).

TableLayout table = (TableLayout)findViewById(r.id.table);
TableRow tableRow = new TableRow(this);
table.addView(tableRow);
Button button = new Button(this);
button.setLayoutParams(new TableRow.LayoutParams(30,30));
tableRow.addView(button);

Could anybody point out where I am going wrong.

2

There are 2 answers

0
Rishi Paul On BEST ANSWER

Only This You have to do to change Button Dynamically

First I get Button

 Button btn = (Button) findViewById(R.id.button1);

Then Button Click event I put This Code Only

TableRow.LayoutParams lp = new TableRow.LayoutParams(70, 70);
btn.setLayoutParams(lp);

And Works Fine. Try It. Will solve your Problem

0
Borra Suneel On

Instated "button.setLayoutParams(new TableRow.LayoutParams(30,30));" change to following code change the button Height and Width.

 LayoutParams lp = new LayoutParams(40, 40);
    table.addView(button, lp);