0

Lets say I have a custom attribute in a few styles like so:

<style name="Theme1" parent="android:Theme.Holo">
    <item name="textViewBackground">#000022</item>
    <item name="android:background">#000000</item>
  </style>
<style name="Theme2" parent="android:Theme.Holo">
    <item name="textViewBackground">#AA2200</item>
    <item name="android:background">#000000</item>
  </style>

Can I then reference that in a standard view like a TextView? Something like:

<TextView
    android:id="@+id/txtNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@CurrentTheme.textViewBackground"
    android:text="Number" />

1 Answer 1

1

You need to define a TextView style, like this:

<style name="Theme1" parent="android:Theme.Holo">
    <item name="android:textViewStyle">@style/TextViewStyle1</item>
    <item name="android:background">#000000</item>
</style>

<style name="Theme2" parent="android:Theme.Holo">
    <item name="android:textViewStyle">@style/TextViewStyle2</item>
    <item name="android:background">#000000</item>
</style>

<style name="TextViewStyle1" parent="@android:style/Widget.TextView">
    <item name="android:background">#000022</item>
</style>

<style name="TextViewStyle2" parent="@android:style/Widget.TextView">
    <item name="android:background">#AA2200</item>
</style>

Then you won't even need to set the android:background attribute on each TextView in your xml, it will apply to all TextViews that do not specify a different background.

Sign up to request clarification or add additional context in comments.

1 Comment

I was hoping to have one theme that defines a whole bunch of global variables instead of creating a separate theme for each element. Oh well. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.