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

  • Home
  • SEARCH
  • 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 7646739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:17:06+00:00 2026-05-31T10:17:06+00:00

I’m fighting with a very strange behaviour in my Android app. I have a

  • 0

I’m fighting with a very strange behaviour in my Android app. I have a TextView, where colours of background and text are set dynamically based on some conditions. To simplify, some condition is checked against an error and colours are set for the text view in this manner (note that the frame layout that the text view is added to has the background colour set to @android:color/black):

TextView tv = new TextView(this);
((FrameLayout)this.findViewById(R.id.text_frame)).add(tv);

String val = getValue();
tv.setText(val);

if(isError(val)) {
    tv.setBackGroundColor(R.color.bg_error);
    tv.setTextColor(R.color.txt_error);
}
else {
    tv.setBackGroundColor(R.color.bg_normal);
    tv.setTextColor(R.color.txt_normal);
}

The colours are defined in XML like so:

<color name="bg_normal">#ffffff</color>
<color name="bg_error">#ff0000</color>
<color name="txt_normal">#0000ff</color>
<color name="txt_error">#008080</color>

I didn’t expect anything weird of this code, as I used similar stuff before many times. However in this case, this code doesn’t work. Regardless of what I try, the colours of text and background of the text view remain black.

Now, if I change my code to the following, everything works perfectly.

if(isError(val)) {
    tv.setBackGroundColor(Color.rgb(255, 0, 0));
    tv.setTextColor(Color.rgb(0, 128, 128));
}
else {
    tv.setBackGroundColor(Color.rgb(255, 255, 255));
    tv.setTextColor(Color.rgb(0, 0, 255);
}

While I can, of course, leave it like this (it works – why fix it?), I would much rather get the colours from XML as it would be so much easier to maintain and modify later.

Has anyone got any ideas as to what the problem might be and how to solve it?

P.S. I will preempt the suggestion that I create the text view in XML and set its colours there by saying that this is a simplified code. There are many text views like this created dynamically with their number depending on external parameter. At coding time, I don’t know whether there will be 1 such text view or 100.

  • 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-05-31T10:17:08+00:00Added an answer on May 31, 2026 at 10:17 am

    setBackgroundColor() takes a color encoded within an int. You supply a resource id though, which will generate wrong/different results.¹

    Rather get the color encoded from the resources like this:

    tv.setBackGroundColor(getResources().getColor(R.color.bg_normal));
    

    ¹ given that is weird design that you have to trip over, other resources setters also take an int, almost consistently for a res id. This is somewhat of a special case. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.