Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7970103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:20:45+00:00 2026-06-04T07:20:45+00:00

after searching here for some hours and testing this and that, i could neither

  • 0

after searching here for some hours and testing this and that, i could neither solve my problem, nor find any question/answers here that apply to my problem – but maybe i only use the wrong search terms – so i’m thankful for any hints in the right direction!

Basically it’s about a app using 2 Themes (Dark & Light), where some TextViews show a status – status can be On or Off, and for better recognition of the state, the color of the text should reflect the state: Red for Off and Green for On.

When i did not use any themes or styles, i simply used the TextView’s .setColor(#ffff0000) for the red color, and so on…

then i started to make use of the Dark / Light Themes for better visibility in different situations – e.g. outdoors black text on white background is easier to read.

therefore i’ve defined my 2 themes, overwriting the textAppearance:

<style name="Theme.MyApp" parent="Theme.Sherlock">
...
<item name="android:textAppearance">@style/TextNormal</item>
...
</style>

<style name="Theme.MyApp.Light" parent="Theme.Sherlock.Light">
...
 <item name="android:textAppearance">@style/TextNormalLight</item>
...

<style name="TextNormal">
    <item name="android:textColor">#ffffffff</item>
</style>

<style name="TextNormalLight">
    <item name="android:textColor">#ff000000</item>
</style>

This works fine, and the “normal” textviews are displayed in the correct color depending on the theme. So far so good!

Instead of using TextView’s setColor, i switched to use .setTextAppearance
tv.setTextAppearance(getSherlockActivity(), R.style.TextRed);

and defined some styles (only pasting one, the others are TextGreen,…):

<style name="TextRed">
    <item name="android:textColor">#ffff0000</item>
</style>

this also works fine!

and to finally get to my problem:

now i want to have different Colors for “Red” depending on the theme…

so i keep the TextRed style, and add a style to use in Light theme, for testing, i just made a weird color to see if it’s working:

<style name="TextRedLight">
    <item name="android:textColor">#ffffff00</item>
</style>

this also work’s if i put a switch-statement around the TextView’s .setTextAppearance, and select the correct style depending on the selected theme – pseudo code:

switch (currentheme) {
   case Dark: tv.setTextAppearance(... R.style.TextRed); break;
   case Light: tv.setTextAppearance(... R.style.TextRedLight); break;
}

and the challenge / question now is – how can i avoid the switch statement?

When refering to styles in layout.xml files, we can use the ? references to honour the current theme… (instead of “@”)

in this case, i need to set the style at run time and honour the theme: so it has to be done by code and not by xml – but setTextAppearance does only accept a id for the style, not a theme-wildcard + id…

and this i where i’m stuck… yes, i could wrap a class around this and keep it to one line in the code and “hide” the theme detection/switch statement in this class, but on the other hand – Android’s complex style/theming architecture should have a solution for this problem as well, shouldn’t it…?

Using themes and styles seems a bit senseless as long as needing helperclasses or switch/if-statements to get some enhanced stuff done…

Thanks for any hints / ideas!!!

edit 1 – after Tomáš answer:
updated style.xml:

<style name="TextNormal" parent="Theme.ARMoDroid">
    <item name="android:textColor">#ffffffff</item>
</style>
 <style name="TextNormal" parent="Theme.ARMoDroid.Light">
    <item name="android:textColor">#ff000000</item>
</style>
<style name="TextRed" parent="Theme.ARMoDroid">
    <item name="android:textColor">#ffff0000</item>
</style>
 <style name="TextRed" parent="Theme.ARMoDroid.Light">
    <item name="android:textColor">#ffffff00</item>
</style>

The TextViews with TextNormal assigned in the layout.xml, or when referencing TextNormal from both themes with:

<item name="android:textAppearance">@style/TextNormal</item>

works as before (with this different approach), also when using red text.

But the initial problem still persists:
when i want to change (at runtime) the color of the text to red, using

tv.setTextAppearance(getActivity(),R.style.TextRed)

it always uses the last definition of TextRed – so with above definition, the text will be colored #ffffff00 -> in yellow. when i swap the order of the 2 TextRed definitions, it’s using the last definition – #ffff0000 – and the text is red.

So when setting the style at runtime, setTextAppearance is not Theme-Aware – it does not distinguish between the current theme and the 2 defined styles.

we can define one style that has different textcolor-values depending on the theme, and depending on the selected theme it’s colored correctly – as long as it’s style is assigned in the layout.xml, or the theme set’s this style as default.

but when switching the styles dynamically in code at runtime, we still need something like the switch-statement above – detect which theme is selected and depending on the theme, apply a different style to the textview.

what i want to achieve is, that it should pick the correct “TextRed” style depending on the current theme. so no need for hardcoding theme awareness… once a new theme is added the mentioned switch-statement has to be updated as well…

edit 2: my current “solution”

i thought i might also post the current solution, but that’s not really a nice solution – though it’s working – but it’s a custom code for each style and with another theme it’s even more code…

The helper function looks like:

     public static int getThemedStyle(int normalStyle) {
     if (prefs==null) { prefs = PreferenceManager.getDefaultSharedPreferences(appContext); }
     int theme = Integer.valueOf(prefs.getString("GUI_Theme","0"));
     if (theme==0) return normalStyle; //dark theme
     else { //return correct  Light-Theme Style
         switch (normalStyle) {
            case R.style.TextNormal: return R.style.TextNormalLight;
            case R.style.TextRed: return R.style.TextRedLight;
            case R.style.TextOrange: return R.style.TextOrangeLight;
            case R.style.TextGreen: return R.style.TextGreenLight;
            case R.style.TextYellow: return R.style.TextYellowLight;
            case R.style.TextBlue: return R.style.TextBlueLight;
            default: return normalStyle;
         } //switch
     } //else theme==0
 } //getThemedStyle

in the fragment i cache the style-reference by calling above function on onCreate:

styleRed = preferences.getThemedStyle(R.style.TextRed);
styleOrange = preferences.getThemedStyle(R.style.TextOrange);
styleGreen = preferences.getThemedStyle(R.style.TextGreen);

and finally assign the style using:

tv.setTextAppearance(getSherlockActivity(), styleRed);

working, but complicated and if that’s the way it’s itended to be, it’s sad… 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:20:46+00:00Added an answer on June 4, 2026 at 7:20 am

    Answering my own question:

    found this

    so instead of my – working – switchstatement, there’s still the need for some code, but it’s at least no hardcoding of theme-ids,… -> updated my getThemedStyle with this snippet

    public static int getThemedStyle(int normalStyle) {
     Resources.Theme theme = appContext.getTheme();
     TypedValue styleID = new TypedValue();
     if (theme.resolveAttribute(R.attr.TextAppRed, styleID, true)) return styleID.data;
      else return normalStyle;
    } //getThemedStyle
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After some searching on Google and here, I still haven't found any answer to
After searching, I dont find the appropriate way to resolve following problem. After activating
First attempt to use this cool site - after searching for 2 hours: So
After searching a lot, reading every tutorials I've found and asking some questions here,
I have got a big problem. After searching through the internet, I didn't find
It appears after much searching that there seems to be a common problem when
I'm no php expert (a mere beginner) but need some help! After hours searching
Hello all! This is my first post on stackoverflow. After hours of searching and
After many hours spent on reading documentations and searching for some tips on the
After searching through some existing libraries for JSON, I have finally ended up with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.