Android - How to programmatically set the button style in a Linearlayout?
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");
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);
This worked for me:
int buttonStyle = R.style.your_button_style;
Button button = new Button(new ContextThemeWrapper(context, buttonStyle), null, buttonStyle)