Here's XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/LightStyle"
android:layout_width="fill_parent"
android:layout_height="55dip"
android:clickable="true"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" />
</RelativeLayout>
How to set style
attribute programmatically?
I used views defined in XML in my composite ViewGroup, inflated them added to Viewgroup. This way I cannot dynamically change style but I can make some style customizations. My composite:
}
and my view in xml where i can assign styles:
You can create the xml containing the layout with the desired style and then change the background resource of your view, like this.
What worked for me:
AND
You can apply a style to your activity by doing:
or Android default:
in your activity, before
setContentView()
.Non of the provided answers are correct.
You CAN set style programatically.
Short answer is take a look at http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/content/Context.java#435
Long answer. Here's my snippet to set custom defined style programatically to your view:
1) Create a style in your styles.xml file
Do not forget to define your custom attributes in attrs.xml file
My attrsl.xml file:
Notice you can use any name for your styleable (my CustomWidget)
Now lets set the style to the widget Programatically Here's My simple widget:
layout:
And finally StyleLoader class implementation
You can find fully working example at https://github.com/Defuera/SetStylableProgramatically