Is there a way to apply a style to all elements of a specific type in a layout xml without actually adding the style attribute to all of them?
I have a lot of TextView elements in a GridLayout. It would be nice if I could say that all TextView elements should have the style “CellFont” without actually having to put that statement into every single one of the TextView elements.
Solution below:
Added the GridLayoutCellFont style to style.xml and added that style as the android:theme to the Activity in the application manifest.
style.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="GridLayoutCellFont">
<item name="android:textSize">22dp</item>
</style>
</resources>
Manifest:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:theme="@style/GridLayoutCellFont"
android:label="@string/app_name"
android:name=".TestLayoutsActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Two options you can either use Style or set new Theme read more at Styles and Themes tutorial