Hossein POURAKBAR
Hossein POURAKBAR

Reputation: 1181

How to apply themes for an exact view?

For example when I define a style tag in XML, all views of all types get that theme. Is there any solution to difference between styles for view types? here is some code:

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

    <style name="buttonTheme" parent="android:Theme.Holo">
        <item name="android:drawableLeft">@drawable/ic_launcher</item>
    </style>

    <style name="textTheme" parent="android:Theme.Holo">
        <item name="android:textColor">#ff0000</item>
    </style>

</resources>

Upvotes: 1

Views: 782

Answers (2)

Jim
Jim

Reputation: 381

You can do this by adding the attribute android:theme="" to the specific activity in your AndroidManifest.xml file

Example:

<activity android:theme="@style/CustomTheme">

Upvotes: 0

devunwired
devunwired

Reputation: 63293

I think there's a small confusion in terms between a style and a theme. You are talking about defining custom styles for a widget in your application. A Theme is a collection of these styles applied globally to an Activity or Application. The custom styles you have created for a button and text view can be applied to a new custom theme so all buttons and text items share the same attributes.

I think what you are looking for is something more like this.

<style name="ApplicationTheme" parent="android:Theme.Holo">
    <item name="buttonStyle">@style/MyButtonStyle</item>
    <item name="textAppearance">@style/MyTextAppearance</item>
</style>

<style name="MyButtonStyle" parent="android:Widget.Button">
    <item name="android:drawableLeft">@drawable/ic_launcher</item>
</style>

<style name="MyTextAppearance" parent="android:TextAppearance">
    <item name="android:textColor">#ff0000</item>
</style>

Here, we have created two new styles (one for the button appearance and one for default text appearance), and then applied those as attributes in a new custom theme. You can now apply this theme in your manifest or in View constructors by referencing @style/ApplicationTheme or R.style.ApplicationTheme

Upvotes: 5

Related Questions