Setting Global Styles for Views in Android

Setting global styles for Views in Android

Actually, you can set a default style for TextViews (and most other built-in widgets) without needing to do a custom java class or setting the style individually.

If you take a look in themes.xml in the Android source, you will see a bunch of attributes for the default style for various widgets. The key is the textViewStyle (or editTextStyle, etc.) attribute which you override in your custom theme. You can override these in the following way:

Create a styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
<item name="android:textViewStyle">@style/MyTextViewStyle</item>
</style>

<style name="MyTextViewStyle" parent="android:Widget.TextView">
<item name="android:textColor">#F00</item>
<item name="android:textStyle">bold</item>
</style>
</resources>

Then just apply that theme to your application in AndroidManifest.xml:

<application […] android:theme="@style/MyTheme">…

And all your text views will default to the style defined in MyTextViewStyle (in this instance, bold and red)!

This was tested on devices from API level 4 onward and seems to work great.

How to a apply style to all the TextViews I have?

Yes it is.

What you need to do is create an

<style name="Theme.RedPlanetTheme" parent="android:Theme">
<item name="android:shadowColor">@color/tv_shadow</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">0.7</item>
</style>

Then in your manifest you would do this.

<application
android:debuggable="true"
android:icon="@drawable/icon"
android:label="Red Planet App"
android:theme="@style/Theme.RedPlanetTheme" >

This will allow your whole application to inherit your custom theme style.

Edit

If you want to apply to only textview, then we just need to customize the ThemeRedPlanet a bit more.

<style name="Theme.RedPlanetTheme" parent="android:Theme">    
<item name="android:textAppearance">@style/MyRedTextAppearance</item>
</style>

<style name="MyRedTextAppearance" parent="@android:style/TextAppearance">
<item name="android:shadowColor">@color/tv_shadow</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">0.7</item>
</style>

I'm using this as example. Steve Pomeroy applys more directly globally themed (set once kinda deal)

Styling composite views

You can define custom theme attributes for that. The views would then just reference the theme attribute and you can change the style by changing the theme attribute value.

First declare the custom theme attributes in res/values/attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="MyCustomAttributes">
<attr name="avatarStyle" format="reference" />
<attr name="usernameStyle" format="reference" />
...
</declare-styleable>

</resources>

Then define them in your theme:

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="avatarStyle">@style/CustomStyleA.Avatar</item>
<item name="usernameStyle">@style/CustomStyleA.Username</item>
...
</style>

And reference them like this:

<ImageView
style="?avatarStyle"
android:id="@+id/avatar"
android:src="@drawable/avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

How to apply common style only for one LinearLayout


<LinearLayout
android:orientation="horizontal"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="30">

<TextView
android:text="Client"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/textView8"
android:layout_weight="10"
style="@style/ItemTransacationHeader"/>

<TextView
android:text="Quantity"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/textView9"
android:layout_weight="10"
style="@style/ItemTransacationHeader"/>

<TextView
android:text="Product Value"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/textView12"
android:layout_weight="10"
style="@style/ItemTransacationHeader"/>

</LinearLayout>

Set text alignment property for TextViews application wide

Create a styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
<item name="android:textViewStyle">@style/MyTextViewStyle</item>
</style>

<style name="MyTextViewStyle"
parent="android:Widget.TextView">
<item name="android:textColor">#F00</item>
<item name="android:textStyle">bold</item>
</style>
</resources>

Then just apply that theme to your application in AndroidManifest.xml:

<application […] android:theme="@style/MyTheme">…
And all your text views will default to the style defined in MyTextViewStyle (in this instance, bold and red)!

This was tested on devices from API level 4 onward and seems to work great.



Related Topics



Leave a reply



Submit