8

I'm trying to add a bunch of buttons to a layout like this:

for( int i = 0; i < 10; i++ ) {
    Button button = new Button( this );
    button.setText( "" + i );
    ( ( LinearLayout )dialog.findViewById( R.id.Buttons ) ).addView( button );
}

My problem is how do I do this programmatically to all the buttons:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:textSize="32dip" />

I've been looking at LayoutParams but it doesn't look complete. Like how do I set the textSize to 32 dip?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Espen
  • 3,607
  • 11
  • 48
  • 75

5 Answers5

20

Set your attributes using the following code:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setGravity(Gravity.CENTER_HORIZONTAL);
button.setTextSize(32);

If you want to specify the text size units use:

button.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 32);
dymmeh
  • 22,247
  • 5
  • 53
  • 60
  • I'm having trouble with the gravity though. setGravity( Gravity.CENTER_HORIZONTAL ) does not do the same as android:layout_gravity="center_horizontal" in the xml file. The xml file button is centered, but not the ones created with setGravity(). – Espen Nov 14 '11 at 21:18
  • Try this with the layout params: LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL); – dymmeh Nov 14 '11 at 22:08
  • Or after initializing params call: params.gravity = Gravity.CENTER_HORIZONTAL; – dymmeh Nov 14 '11 at 22:09
  • The reason setGravity doesn't work the way you want is because android:gravity is different than android:layout_gravity – dymmeh Nov 14 '11 at 22:10
  • Thanx, but I figured it out. Instead of setting layout_gravity on each button, I set gravity on the parent view LinearLayout. – Espen Nov 14 '11 at 22:10
  • That works too. Saves you from setting the value on each button. – dymmeh Nov 14 '11 at 22:11
4

LayoutParams relates to the parent ViewGroup which will contain the view. So in your case it's a LinearLayout so you need to create parameters for that one. Here's what I'm talking about:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.weight = 1f;

Button button = new Button(this);
button.setLayoutParams(lp);
button.setText("" + i);
((LinearLayout)dialog.findViewById(R.id.Buttons)).addView(button);
Knickedi
  • 8,742
  • 3
  • 43
  • 45
3

Use LayoutParams for the height, width and gravity with

LinearLayout.LayoutParams (int width, int height)

where you can use WRAP_CONTENT for the ints.

Then there are Button.setGravity() and Button.setTextSize() for the last two.

Hope this helps.

Glenn.nz
  • 2,261
  • 2
  • 17
  • 20
0

You'd use a LayoutParams object for the layout settings, and to set the text size use setTextSize() from the Button class.

You can set the gravity with setGravity() too.

John Leehey
  • 22,052
  • 8
  • 61
  • 88
0

TextSize is not inside Layout params. To set textSize you have to

button.setTextSize(32);
weakwire
  • 9,284
  • 8
  • 53
  • 78