52

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?

6 Answers6

56

This worked for me:

int buttonStyle = R.style.your_button_style;
Button button = new Button(new ContextThemeWrapper(context, buttonStyle), null, buttonStyle)
Allan Veloso
  • 5,823
  • 1
  • 38
  • 36
Chris Lamothe
  • 1,473
  • 13
  • 11
55

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);
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • 1
    This works only from API Level 11, don't you know how to do it in older APIs? – Oliv Apr 22 '13 at 12:46
  • 6
    @Oliv I believe its not possible for older APIs. However, as an alternate, I would suggest you to Define your `LinearLayout` as an XML with all the styling you want and simply **inflate** that XML to an object inside your code. – waqaslam Apr 22 '13 at 14:30
  • 1
    If I change `Button` to `LinearLayout`, I loose other methods on Button class like setText() – ishandutta2007 Oct 01 '17 at 05:12
  • 15
    Doesn't work for me. As a result a button has no style at all and looks like flat text – Konstantin Konopko Sep 16 '18 at 09:16
17

This is the only answer that actually delivered what I was looking for: http://belencruz.com/2013/04/set-styles-programmatically-in-android/

Essentially, create a layout XML file that simply has a <Button/> element with the style you want. Then, inflate it programmatically and just customize the button text.

Something along the lines of:

<?xml version="1.0" encoding="utf-8"?>
<Button style="@style/your_custom_style_here"/>

In code:

Button button = (Button)getLayoutInflater().inflate(R.layout.your_custom_button_layout_file, null);
button.setText("Your Text");
Luis Artola
  • 771
  • 6
  • 20
10

The Paris library from AirBnB is perfect for this, allowing for programmatic configuration like so:

Paris.styleBuilder(textView)
        .add(R.style.MyGreenTextView)
        .textSizeRes(R.dimen.my_text_size_small)
        .apply();
Chris Lacy
  • 4,222
  • 3
  • 35
  • 33
5

Instead of waqaslams

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

(which is, judging by the comments, API dependable) I have also read ridoys answer from here [ Android Button Styling Programatically ] which is

transferBtn.setBackgroundResource(R.layout.buttonstyle);

(exposing the above answers because one of these may also work for you, depending of API version you use),

But this is what worked for me (mind the change from "layout" to "drawable"):

Button b = new Button(this);
b.setBackgroundResource(R.drawable.button_custom_green);

(answer from [How to programmatically setting style attribute in a view)

Community
  • 1
  • 1
UяošKoт
  • 416
  • 6
  • 10
3

In my case style was not loaded at all when I pass them into Button constructor. It seems that was because I was using the material design view. Here what solved my case:

val themeWrapper = ContextThemeWrapper(
      context,
      R.style.AppTheme_ButtonPrimary // my button style
)

val button = MaterialButton(themeWrapper)

Please note that MaterialButton was created instead of Button.

I found this solution at: How to specify a style for a view programmatically in Android.

Community
  • 1
  • 1
user9440008
  • 572
  • 1
  • 9
  • 21