180

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");
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Tawani
  • 11,067
  • 20
  • 82
  • 106

16 Answers16

363

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

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
bhatt4982
  • 8,024
  • 2
  • 26
  • 18
  • 6
    The android API is really something, why couldn't it throw an error? – Tawani Sep 23 '09 at 17:05
  • 54
    Because it is not an error. setBackgroundColor() takes a color in numeric form (e.g., 0xFFFF0000 for red). R.color.white is also a number. – CommonsWare Sep 23 '09 at 17:15
  • 6
    D'oh! This definitely could have been done better with enums rather than ubiquitous ints. Thanks for the answer. – Wojciech Górski Sep 02 '12 at 15:21
  • 2
    They *could* be using different type signatures! One called ColorId and another called HexColor or something. Both are integers but they aren't the same *type* –  Oct 13 '12 at 19:27
  • 1
    Very strange. From documentation: The resource should refer to a **Drawable** object or 0 to remove the background. – Artem Oct 02 '15 at 14:40
77

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.

Seb DA ROCHA
  • 1,301
  • 10
  • 5
66

To set red color:

textView.setBackgroundColor(0xfff00000);

Or

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

textView.setBackgroundResource(R.color.solid_red);
Dmitry
  • 1,004
  • 8
  • 9
  • 2
    To echo badMonkey's answer given this is the more popular answer, you MUST set the high order bits to non zero for the transparency. I get caught all the time by specifying the RGB without the A. To set the background to pure blue, use 0xff0000ff, not 0x0000ff or it won't work. – JohnnyLambada Jul 03 '13 at 00:57
  • In the .xml the value of a element works with only the classic 6 digits: #FF0000 – Ed_ Jul 25 '14 at 17:32
  • @WeaponX: it works fine with transparent colors (in 8 digits). – CoolMind Jan 19 '16 at 15:32
  • @Weapn X The function takes an integer, 4 bytes. 6 hex digits is 3 bytes. –  Sep 26 '19 at 15:32
20

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).

duggu
  • 37,851
  • 12
  • 116
  • 113
badMonkey
  • 1,687
  • 1
  • 22
  • 23
13

Just this 1 line of code changed the background programmatically

tv.setBackgroundColor(Color.parseColor("#808080"));
Vivek Warde
  • 1,936
  • 8
  • 47
  • 77
8

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:)

user1252459
  • 563
  • 7
  • 7
6

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
dharmx
  • 694
  • 1
  • 13
  • 20
4
tv.setTextColor(getResources().getColor(R.color.solid_red));
duggu
  • 37,851
  • 12
  • 116
  • 113
Yash Patil
  • 469
  • 4
  • 9
4

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);
    
3

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

Siddharth
  • 9,349
  • 16
  • 86
  • 148
3

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"));
Hamad
  • 5,096
  • 13
  • 37
  • 65
2

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

myTextView.setBackgroundColor(ContextCompat.getColor(this,R.color.mycolor)); 
Kabir
  • 852
  • 7
  • 11
Mladen Rakonjac
  • 9,562
  • 7
  • 42
  • 55
1

Jut use

ArrayAdapter<String> adaptername = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, your array list);
duggu
  • 37,851
  • 12
  • 116
  • 113
Pavan Anadkat
  • 111
  • 1
  • 3
  • 9
0

For setting colors from resource follow this:

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

Here ButtonColorRed is color name in color resource

Pranav Karnik
  • 3,318
  • 3
  • 35
  • 47
AmazingAshu
  • 17
  • 1
  • 6
0

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)

Trang Nguyen
  • 21
  • 1
  • 2
-11

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

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Richa
  • 1