0

I have defined red color to contentBg style for TextView. Can i change the TextView color red to blue by changing or renaming style name myContentNew from the parent style? The TextView style name-contentBG should not be changed. Is it possible in android sytle.

<RelativeLayout 
   style="@style/myContent"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">
<TextView 
   style="@style/contentBG"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"/>
</RelativeLayout>

My code should be like below

<RelativeLayout 
       style="@style/myContentNew"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content">
    <TextView 
       style="@style/contentBG"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"/>
 </RelativeLayout>

You can get clear idea with my attached image.enter image description here

like CSS:

.myContent .contentBg{background-color:red}
.myContentNew .contentBg{background-color:blue}

Thanks

5
  • It seems you are tryin to setup a theme right? Do you need to do this in one layout or all the layouts? Commented May 28, 2013 at 6:20
  • Actually i would like to use this as active or inactive state with the above requirements Commented May 28, 2013 at 6:36
  • I don't think you can setup this kind of inheritance directly. The only way I see is to setup 2 themes and use styles as references that will change according to the theme selected. Commented May 28, 2013 at 7:43
  • Can u please show me how can we do that? Commented May 28, 2013 at 7:59
  • I added an answer, I hope it's clear enough! Commented May 28, 2013 at 8:24

2 Answers 2

1

I'm afraid not.

Once the contentBG style is set. It can't be changed but in code.

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

Comments

1

Here is an example with the creation of 2 different themes:

You need first to modify your manifest file to call the default custom theme, for example:

<application
    android:name="RateDayApplication"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/CustomTheme" >

Then, in your styles.xml file, prepare 2 different themes, containing the same style contentBG:

<!-- *** THEME WITH RED BG*** -->
<style name="CustomTheme" parent="@android:style/Theme.Holo.Light">
    <item name="contentBG">@style/contentBGred</item>

</style>

<style name="contentBGred" >
    <item name="android:colorBackground">@color/red</item>
</style>

<!-- *** THEME WITH BLUE BG*** -->
<style name="CustomThemeNew" parent="@android:style/Theme.Holo.Light">
    <item name="contentBG">@style/contentBGblue</item>
</style>

<style name="contentBGblue" >
    <item name="android:colorBackground">@color/blue</item>
</style>

IMPORTANT: here I use Theme.Holo.Light as parent theme, you can change it if you are using a different one.

In the 2 themes above, the style has the same name, it will be use as a reference in your XML files. In order to declare this reference, you must add it in the file untitled attrs.xml under your values folder.

Here is the content:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <attr name="contentBG" format="reference" />
</resources>

Once it is done, you just need to call this style this way in your XML files (you don't need the style on the layout anymore):

<RelativeLayout 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">
<TextView 
   style="?contentBG"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"/>
</RelativeLayout>

The question mark here means that the style will be loaded using a reference. The style that will be used will be the one of the current theme applied on the Activity.

You can easily setup the theme you want on your activity using this way:

setTheme(R.style.CustomTheme);

or

setTheme(R.style.CustomThemeNew);

Comments

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.