Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 667
PlanetaryFortressRush Avatar asked Jul 10 '12 21:07

PlanetaryFortressRush


People also ask

How to change Button style in Android studio programmatically?

Generally you can't change styles programmatically; you can set the look of a screen, or part of a layout, or individual button in your XML layout using themes or styles. Themes can, however, be applied programmatically.

How to give style to Button in Android?

To change the default Button style of the application we can use the android:buttonStyle attribute in the AppTheme style inside the styles. xml.

How can change button background drawable programmatically in Android?

setBackgroundResource() method is used to change the button background programmatically. setBackgroundResource(int id) accepts id of drawable resource and applies the background to the button.


2 Answers

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); 
like image 104
waqaslam Avatar answered Sep 28 '22 04:09

waqaslam


This worked for me:

int buttonStyle = R.style.your_button_style; Button button = new Button(new ContextThemeWrapper(context, buttonStyle), null, buttonStyle) 
like image 31
Chris Lamothe Avatar answered Sep 28 '22 03:09

Chris Lamothe