3

I have reffered this question and implemented circular background for TextView using circle.xml (in res/drawable) and setting it as android:background="@drawable/circle" for TextView. But what I need is , I need to set the background color dynamically through code. Just like the lollipop contacts app as shown below

enter image description here

How can I acheive this? I need the TextView background in circular shape always as shown in above image

Community
  • 1
  • 1
dev
  • 1,085
  • 4
  • 19
  • 26

3 Answers3

9

You can change TextView background color in many ways like:

textView.setBackgroundColor(Color.parseColor("#f44336"));

or

textView.setBackgroundColor(Color.RED);

or

textView.setBackgroundColor(Color.rgb(255, 0, 0));

or

textView.setBackgroundColor(getColor(R.color.red_color));

and many other ways too...

Edit:

If you want to change your TextView background color that was defined in your drawable file, do it like this:

GradientDrawable:

GradientDrawable tvBackground = (GradientDrawable) textView.getBackground();
tvBackground.setColor(Color.parseColor("#f44336"));

StateListDrawable:

StateListDrawable tvBackground = (StateListDrawable) textView.getBackground();
tvBackground.setColorFilter(Color.parseColor("#f44336"), PorterDuff.Mode.SRC_ATOP);

But if you don't want to set a color filter, you can get the drawable of each state separately by following the answer in this link.

Community
  • 1
  • 1
Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
  • Yeah, all these are options one could use to achieve the desired effect. I was just about adding the rest. – Akah Sep 20 '15 at 13:28
  • But this doesn't give me the circular background shape. it changes only the background color. The circle background will be gone and the new color will be shown in square background – dev Sep 20 '15 at 14:04
  • You didn't mention in your question that you want it to be in a circular shape. – Hussein El Feky Sep 20 '15 at 14:06
  • GradientDrawable does not work for TextView. Getting error: java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.GradientDrawable – drod Jul 10 '16 at 21:15
  • @drod It seems that you are using StateListDrawable instead of GradientDrawable. StateListDrawable can have many background states thus no definite background. So try my updated answer. – Hussein El Feky Jul 11 '16 at 16:57
  • Is there a one liner for this in Kotlin? – nmu Aug 14 '17 at 21:14
2

I think you wanted to ask how to generate random color to set as your textview background. Well, there are many ways. e.g;

textview.setBackgroundColor(Color.rgb((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)));
  • Ya. but this doesn't work for generating random color. `new Color((int)(Math.random() * 0x1000000))` this statement is invalid – dev Sep 20 '15 at 13:52
1

My text view has a circle shape defined as

// circleshape.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="schemas.android.com/apk/res/android"; android:shape="oval"> 
<solid android:color="@android:color/darker_gray" /> 
<corners android:bottomRightRadius="8dp" android:bottomLeftRadius="8dp" android:topRightRadius="8dp" android:topLeftRadius="8dp"/> 
</shape>

I applied it to Textview using background="@drawable/circleshape"

This makes the textview circular. Now use the code below to

GradientDrawable tvBackground = (GradientDrawable) viewHolder.userInitialsText.getBackground();

//myHexColorCode  is like "0xff00ff"
tvBackground.setColor(Color.parseColor(myHexColorCode));
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
techtinkerer
  • 1,280
  • 2
  • 15
  • 26