android TextView: setting the background color dynamically doesn't work
Question
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");
Solution
Use et.setBackgroundResource(R.color.white);
OTHER TIPS
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.
To set red color:
textView.setBackgroundColor(0xfff00000);
Or
<color name="solid_red">#fff00000</color>
textView.setBackgroundResource(R.color.solid_red);
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)
.
Just this 1 line of code changed the background programmatically
tv.setBackgroundColor(Color.parseColor("#808080"));
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:)
Here are the steps to do it correctly:
First of all, declare an instance of TextView in your MainActivity.java as follows:
TextView mTextView;
Set some text DYNAMICALLY(if you want) as follows:
mTextView.setText("some_text");
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>
You can now use "my_color" color in your java file to set the background dynamically as follows:
mTextView.setBackgroundResource(R.color.my_color);
tv.setTextColor(getResources().getColor(R.color.solid_red));
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
Color.parseHexColor("17ee27")
did not work for me, instead Color.parseColor("17ee27")
worked perfectly.
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"));
If you would like to support all versions, do this:
myTextView.setBackgroundColor(ContextCompat.getColor(this,R.color.mycolor));
Jut use
ArrayAdapter<String> adaptername = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, your array list);
you can use android:textColor= "
whatever text color u want to give" in xml file where your text view is declared.