android TextView: setting the background color dynamically doesn't work

AndroidTextview

Android Problem Overview


Setting the background color programatically of an android TextView doesn't seem to work. I'm I missing something!

TextView et = new TextView(activity);
et.setText("350");
et.setBackgroundColor(R.color.white);

I also have this file (colors.xml) in my res/values folder

<resources>
        <color name="white">#ffffffff</color>
        <color name="black">#ff000000</color>
</resources>

[EDIT]: Also, setting the text color causes the TextView to disappear.

TextView c1 = new TextView(activity);
c1.setTextColor(R.color.solid_red);
c1.setText("My Text");

Android Solutions


Solution 1 - Android

Use et.setBackgroundResource(R.color.white);

Solution 2 - Android

Try this:

TextView c1 = new TextView(activity);
c1.setTextColor(getResources().getColor(R.color.solid_red));
c1.setText("My Text");

I agree that a color and a resource have the same type, but I also spend a few hours to find this solution.

Solution 3 - Android

To set red color:

textView.setBackgroundColor(0xfff00000);

Or

<color name="solid_red">#fff00000</color>

textView.setBackgroundResource(R.color.solid_red);

Solution 4 - Android

I had a similar issue where I was creating a numeric color without considering the leading alpha channel. ie. mytext.setTextColor(0xFF0000) (thinking this would be red ). While this is a red color it is also 100% transparent as it = 0x00FF0000; The correct 100% opaque value is 0xFFFF0000 or mytext.setTextcolor(0xFFFF0000).

Solution 5 - Android

Just this 1 line of code changed the background programmatically

tv.setBackgroundColor(Color.parseColor("#808080"));

Solution 6 - Android

Well I had situation when web service returned a color in hex format like "#CC2233" and I wanted to put this color on textView by using setBackGroundColor(), so I used android Color class to get int value of hex string and passed it to mentioned function. Everything worked. This is example:

String myHexColor = "#CC2233";
TextView myView = (TextView) findViewById(R.id.myTextView);
myView.setBackGroundColor(Color.pasrsehexString(myHexColor));

P.S. posted this answer because other solutions didn't work for me. I hope this will help someone:)

Solution 7 - Android

here is in little detail,

if you are in activity use this

textview.setBackground(ContextCompat.getColor(this,R.color.yourcolor));

if you are in fragment use below code

textview.setBackground(ContextCompat.getColor(getActivity(),R.color.yourcolor));

if you are in recyclerview adapter use below code

textview.setBackground(ContextCompat.getColor(context,R.color.yourcolor));

// use holder.textview if you are in onBindviewholder
//here context is passed from fragment

Solution 8 - Android

tv.setTextColor(getResources().getColor(R.color.solid_red));

Solution 9 - Android

Here are the steps to do it correctly:

  1. First of all, declare an instance of TextView in your MainActivity.java as follows:

    TextView mTextView;
    
  2. Set some text DYNAMICALLY(if you want) as follows:

    mTextView.setText("some_text");
    
  3. Now, to set the background color, you need to define your own color in the res->values->colors.xml file as follows:

    <resources>
        <color name="my_color">#000000</color>
    </resources>
    
  4. You can now use "my_color" color in your java file to set the background dynamically as follows:

    mTextView.setBackgroundResource(R.color.my_color);
    

Solution 10 - Android

Color.parseHexColor("17ee27") did not work for me, instead Color.parseColor("17ee27") worked perfectly.

Solution 11 - Android

two ways to do that:

1.create color in colors.xml file like:

<resources>
        <color name="white">#ffffff</color>
</resources>

and use it int activity java class as:

et.setBackgroundResource(R.color.white);

2.

et.setBackgroundColor(getResources().getColor(R.color.white));
                or
et.setBackgroundColor(Color.parseColor("#ffffff"));

Solution 12 - Android

If you would like to support all versions: Try this:

myTextView.setBackgroundColor(ContextCompat.getColor(this,R.color.mycolor)); 

Solution 13 - Android

Jut use

ArrayAdapter<String> adaptername = new ArrayAdapter<String>(this,
			android.R.layout.simple_dropdown_item_1line, your array list);

Solution 14 - Android

For setting colors from resource follow this:

textView.setBackgroundColor(getResources().getColor(R.color.ButtonColorRed));

Here ButtonColorRed is color name in color resource

Solution 15 - Android

If you try the above solutions and it still doesn't work, have a look at your TextView xml and make sure that you remove android:background (cuz it may overlap your et.setBackGroundColor(R.color.yourcolor)

Solution 16 - Android

you can use android:textColor= " whatever text color u want to give" in xml file where your text view is declared.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTawaniView Question on Stackoverflow
Solution 1 - Androidbhatt4982View Answer on Stackoverflow
Solution 2 - AndroidSeb DA ROCHAView Answer on Stackoverflow
Solution 3 - AndroidDmitryView Answer on Stackoverflow
Solution 4 - AndroidbadMonkeyView Answer on Stackoverflow
Solution 5 - AndroidVivek WardeView Answer on Stackoverflow
Solution 6 - Androiduser1252459View Answer on Stackoverflow
Solution 7 - AndroiddharmxView Answer on Stackoverflow
Solution 8 - AndroidYash PatilView Answer on Stackoverflow
Solution 9 - AndroidSahil SharmaView Answer on Stackoverflow
Solution 10 - AndroidSiddharthView Answer on Stackoverflow
Solution 11 - AndroidHamadView Answer on Stackoverflow
Solution 12 - AndroidMladen RakonjacView Answer on Stackoverflow
Solution 13 - AndroidPavan AnadkatView Answer on Stackoverflow
Solution 14 - AndroidAmazingAshuView Answer on Stackoverflow
Solution 15 - AndroidTrang NguyenView Answer on Stackoverflow
Solution 16 - AndroidRichaView Answer on Stackoverflow