Android: Dynamically Change TextView Background color
Asked Answered
Z

6

11

I have following textview in my Activity. I want to change the background color of the textview dynamically.

My problem is I don't want to get the colors from Resouce file or other colors.RED method. I'm getting colors from webservie in websafe mode (i.e #FFF, #000 etc).

How can I pass these colors as background to TextView. Thanks in advance for your time.

<TextView
                android:id="@+id/colorCode"
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true" android:background="#FF0000" android:layout_marginRight="5dp"/>
Zama answered 11/11, 2011 at 6:36 Comment(0)
D
35

Below is snippet might help you where txtChannelName is an object of TextView

 txtChannelName.setBackgroundColor(Color.RED);

or

txtChannelName.setBackgroundColor(Color.parseColor("#ffffff"));
Delaine answered 11/11, 2011 at 6:42 Comment(1)
Thanks but I was already aware of this method. I mentioned in my question too about this //colors from Resouce file or other colors.RED method// I wanna know whether I can pass #FFF as input to background color of a textview??.Zama
A
8

yow can set color from android or color in format rbg like this:

TextView txtView = (TextView) findViewById(R.id.yourId);
txtView.setBackgroundColor(Color.parseColor("#AA3456"));

or:

txtView.setBackgroundColor(Color.BLUE);
Aucoin answered 30/5, 2013 at 11:53 Comment(0)
P
4

You can try:

String color = "FF0000";   // For example your color is FF0000
TextView txt = new TextView(this);         
txt.setBackgroundColor(Integer.parseInt(color, 16)+0xFF000000);

OR

//This is the most preferrable
txt.setBackgroundColor(Color.parseColor("#FF0000"));    
Phyle answered 11/11, 2011 at 6:47 Comment(0)
C
2

In your activity you do something like that:

TextView textView = (TextView) findViewById(R.id.colorCode);
int myDynamicColor = Color.parseColor("#FFFF00"); // Here you can pass a string taken from the user or from wherever you want.
textView.setBackgroundColor(myDynamicColor);

Hope this helps.

Crownpiece answered 11/11, 2011 at 6:45 Comment(1)
This is wot I'm talking about :)Zama
A
1

You can now change your background color programatically. 100% working for me. Try it.

 RelativeLayout relativeLayout = findViewById(R.id.relativeLayout);
 relativeLayout.setBackgroundColor(ContextCompat.getColor(yourContext, R.color.yourColor));

Thanks me later.

Anabatic answered 13/1, 2021 at 7:52 Comment(0)
A
0

XML file saved at res/values/colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="opaque_red">#f00</color>
   <color name="translucent_red">#80ff0000</color>
</resources>

Then from your program access those color like following:

Resources res = getResources();
int color = res.getColor(R.color.opaque_red);
textView.setBackgroundColor(color);
Alchemize answered 11/11, 2011 at 6:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.