Android – How to programmatically set the button style in a Linearlayout?

I am programmatically creating a LinearLayout for an AlertDialog with some buttons.

I WANT to do this:

<LinearLayout android:id="@+id/footer" android:layout_width="fill_parent"
style="@android:style/ButtonBar">

But with code like this:

LinearLayout buttons = new LinearLayout(parentContext);
buttons.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams buttonsParams =
    new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
    LayoutParams.WRAP_CONTENT);
topLayout.addView(buttons, buttonsParams);
buttons.setLayoutParams(buttonsParams);
Button btnAdd = new Button(context);
btnAdd.setText("Add");

How can I set the style of the buttons (use a button bar) programmitically?

Answer

Hope I’m not too late to join the party 🙂

Well, Styles can be applied to a View at initialization phase. For example:

LinearLayout button = new LinearLayout(context, null, android.R.style.ButtonBar);

Leave a Reply

Your email address will not be published. Required fields are marked *