1

I have a method that creates several buttons

public Button[] generaBottoniRisposta(int numeroBottoni, Context context){  
    Button bottoni[]= new Button[numeroBottoni];

    /*genero un tot di bottoni in base a numeroBottoni, è necessario avere il context*/
    for(int i=0; i < bottoni.length;i++){           
            bottoni[i] = new Button(context);
            bottoni[i].setId(i);
            bottoni[i].setText(String.valueOf(i+1));
            LayoutParams param = new LinearLayout.LayoutParams(50, 50);
            bottoni[i].setLayoutParams(param);          
        }

    return  bottoni;
    }

and then another method that add them to a gridlayout.

I want to set the width of those buttons, but i'm not able to do it.

I tried a lot of stuff, setWidth(), setMaxWidth(), invalidate() etc.

Something weird happens. If I try to make the button bigger than its default size it works, if i try to make the button smaller than its default size it doesn't work!

How should I do? thank you

MDP
  • 4,177
  • 21
  • 63
  • 119

5 Answers5

3

Try using LayoutParams, something like..

RelativeLayout.LayoutParams rel_bottone = new RelativeLayout.LayoutParams(buttonWidth, buttonHeight);
button.setLayoutParams(rel_bottone);

And the layout depends on the parent layout that contains the buttons..

Nermeen
  • 15,883
  • 5
  • 59
  • 72
  • My buttons are inside a gridlayout. I tried to use GridLayout.LayoutParams giving the width of the cells, instead of trying to set the width of the button, and now it seems to work. But i'm wondering if i can set the width of the buttons in a similar way – MDP Feb 07 '13 at 09:50
0

I think setlayoutparams should do what you want. Like in the answer in this thread. Set View Width Programmatically

Community
  • 1
  • 1
just_user
  • 11,769
  • 19
  • 90
  • 135
0

try this:

LinearLayout.LayoutParams params = button.getLayoutParams();
params.width = 100;
button.setLayoutParams(params);

one more thing LinearLayout.LayoutParams is used when parent is a LinearLayout and when parent is RelativeLayout use RelativeLayout.LayoutParams.

Saifuddin Sarker
  • 853
  • 3
  • 8
  • 26
0

Try something like this :

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
button.setLayoutParams(params);
lokoko
  • 5,785
  • 5
  • 35
  • 68
0

My buttons are inside a gridlayout. I tried to use GridLayout.LayoutParams giving the width of the cells, instead of trying to set the width of the button, and now it seems to work.

this is the code I use to add the buttons to the GridLayout

Spec row = GridLayout.spec(numeroRiga, 1); 
            Spec colspan = GridLayout.spec(numeroColonna, 1);
            GridLayout.LayoutParams gridLayoutParam = new GridLayout.LayoutParams(row,colspan);
            gridLayoutParam.width=50;
            gridLayoutParam.height=50;

gridLayout.addView(button,gridLayoutParam);

But i'm wondering if i can set the width of the buttons in a similar way.

MDP
  • 4,177
  • 21
  • 63
  • 119