Android styles and themes mainly based on the attributes such as fonts size, font color, background color, margin, font type.Therefore It will represent using XML structure. While we define android styles in a separate file, we use that defined style for the views in the XML file that specifies the layout. Style definition needs a new XML in res>values directory of our project with <resources> as the root directory.
Its more like to CSS.Now we will see how to use styles and themes in the android.
Sample style file :
<resources>
<style name=”EditTextStyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:textSize">40dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">#009999</item>
</style>
</resources>
After our Android style is ready, we can use it in our XML file using the Style attribute. To understand how we do it, consider the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/txtEdit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="@style/EditTextStyle"
android:text="Hello World"/>
</LinearLayout>
Let see how to do a Style Inheritance.
<resources>
<style name="DataFlairTheme" parent="android:style/Theme">
<item name="android:textColorPrimary">#ffff0000</item>
//add required styles
</style>
</resources>
Themes in android
Theme is a default template of the styles that the view in the app is applied to all the attributes that support it.
To apply a theme in Android, do the following:
<color name="colorIt">#b0b0ff</color>
<style name="DataFlairTheme" parent="Theme.AppCompat.Light">
<item name="android:windowBackground">@color/colorIt</item>
<item name="android:colorBackground">@color/colorIt</item>
</style>
Or you can apply it to entire application as below:
<activity android:theme="@android:style/CustomTheme">
<application android:theme="@android:style/CustomTheme">
No Comments