1

I know the Style works as it is exactly that Style that I was using before albeit in XML. A simple example of what I am doing (not working)..

LinearLayout buttonlayout = (LinearLayout) dialogLayout.findViewById(R.id.layout_menu_buttons);

Button bSettings = new Button(getActivity(), null, R.style.button_menu);

buttonlayout.addView(bSettings);

I had it working perfectly in XML, I was able to create buttons without styles no problem (this was working but didn't let me apply a style dynamically)..

Button bSettings = new Button(getActivity());
    bSettings.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

Complete code:

    // Getting reference to the Menu layout
    LinearLayout buttonlayout = (LinearLayout) dialogLayout.findViewById(R.id.layout_menu_buttons);
    buttonlayout.removeAllViewsInLayout();

    // Dynamically create buttons       
    Button bSettings = new Button(getActivity(), null, R.style.button_menu);        
    Button bHelp = new Button(getActivity(), null, R.style.button_menu);        
    Button bHistory = new Button(getActivity(), null, R.style.button_menu);     
    Button bAbout = new Button(getActivity(), null, R.style.button_menu);

    // Adding to Layout
    buttonlayout.addView(bSettings);
    buttonlayout.addView(bHelp);
    buttonlayout.addView(bHistory);
    buttonlayout.addView(bAbout);

button_menu style..

<style name="button_menu" parent="@style/Fill.Width">
    <item name="android:background">@drawable/selector_example1_button_background</item>
    <item name="android:layout_margin">10dp</item>
    <item name="android:padding">30dp</item>
</style>

Note: I have checked using the DDMS Dump View and found that all of the buttons are there, they all have the correct text etc. I just can't see them, they don't seem to be actually using any of the properties that are included in the style.

I have also tried setting the LayoutParams again after setting the style which made no difference.

Bit confused by this..

Any ideas?

mgibson
  • 6,103
  • 4
  • 34
  • 49
  • http://stackoverflow.com/questions/2016249/how-to-programmatically-setting-style-attribute-in-a-view – Geobits Jul 16 '13 at 15:01
  • I know there is no built in mechanism to changes styles at run time. I am having problems applying a style on creation which is a documented option. I am just having trouble doing it in my DialogFragment. – mgibson Jul 16 '13 at 15:03
  • I see. Most people that ask this are trying to do it at runtime. It's a common problem, but if you define your style as an attribute, it should work just fine: http://stackoverflow.com/questions/8369504/why-so-complex-to-set-style-from-code-in-android – Geobits Jul 16 '13 at 15:09
  • I have tried that and it gives me the same results in two different test cases - no style attributes inherited. – mgibson Jul 16 '13 at 15:33

2 Answers2

0

the issue is you're breaking the layout params by creating a new layout params object and overwriting the existing layout params on the view.

LinearLayout buttonLayout;
Button testV = new Button(this, null, R.style.button_menu);
Button testV2 = new Button(this, null, R.style.button_menu);
buttonLayout.addView(testV);
buttonLayout.addView(testV2);

LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) testV.getLayoutParams();
//we cast to linearLayout params because we're putting this view in a linearLayout
layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
testV2.setLayoutParams(layoutParams);

in this example we're creating our new Button with a style then getting the params from it and changing what we want (due to how objects work we dont have to set this back where it came from) now in the case of the second button (testV2) because it's based on the same style as testV (and therefore the layoutParams aswell) we can just set the layout params of testV2 to the same as testV.

Eluvatar
  • 2,265
  • 19
  • 24
  • Hmm, I have been working on a separate test case. I am trying to include styles the same way as you are talking about and every time all I can see is the text, I can't to get anything attributes to be inherited from a style on initialization. I tried your code, it unfortunately makes no difference. – mgibson Jul 16 '13 at 15:32
0

Ended up creating a button template each with whatever style is selected by the current theme and then inflated that XML layout..

Button b = (Button) getLayoutInflater().inflate(R.layout.styled_button, null);

Then by changing the theme, you change the style of the Buttons. See this question for a good example Dynamically change theme and therefore widget style

Community
  • 1
  • 1
mgibson
  • 6,103
  • 4
  • 34
  • 49