2

I have spinner with a style selection (I set the spinner style in strings.xml) and it's works well if I set the style in main.xml. But I want to know how to set the style programmatically in which the style already defined in strings.xml.

Refer my code below

main.xml:

      <Spinner
         android:id="@+id/PassengersSpinner1"
         android:layout_width="100dp"
         android:layout_height="40dp"
         android:layout_span="3"
         android:layout_marginTop="6dp" 
         style="@style/SpinnerStyle" />

strings.xml:

 <style
    name="SpinnerStyle" 
    parent="@android:style/Widget.Spinner">     
    <item name="android:background">@android:drawable/btn_default</item>            
 </style>

Now, I want to know that how can I set this strings.xml programmatically without main.xml?

Omid Nazifi
  • 5,235
  • 8
  • 30
  • 56
sivan esan
  • 143
  • 1
  • 3
  • 8

1 Answers1

2

From all my reading, you cannot do it, because the style has to be set before the view is created.

You can do it if you create the view in code (since then you can set the style before creating the view).

If you want to do that, a good example is here. Both the question and the answer give you methods to achieve your goal.

Community
  • 1
  • 1
Barak
  • 16,318
  • 9
  • 52
  • 84