19

I have a list and i write a custom adapter for this. And I want to set some text color for this (e.g. Orange color code #F06D2F). I am presenting the code snippet for my getView() method.

TextView text = new TextView(this.context);
// text.setPadding(25, 5, 0, 0);

text.setBackgroundResource(R.drawable.back_horizontal);

// text.setClickable(false);
// text.setFocusable(false);
text.setEllipsize(TruncateAt.END);
text.setSingleLine(true);

// text.setTextColor(R.color.yellow);

text.setTextColor(R.color.Orange);
text.setGravity(Gravity.CENTER_VERTICAL);


helvetica_normal = Typeface.createFromAsset(context.getAssets(), "fonts/helvetica.ttf");

text.setTypeface(helvetica_normal);
// text.setTextColor(R.color.yellow);



text.setText(objects[position]);

LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
manager.addView(text, layoutParams);

The problem is that i can't see the color set to orange. What went wrong?

Note: The context is passed in constructor as well as objects (the string array)

Thanks for your help

nayaab
  • 481
  • 4
  • 13
Prasham
  • 6,646
  • 8
  • 38
  • 55
  • 1
    did you tried this text.setTextColor(android.graphics.Color.RED);, check this and see if red color is set or not?? – Sankar Ganesh PMP Dec 21 '10 at 12:29
  • @sankar i want to set a color code, that i have defined in xml file, and can't find in Color class. Currently my application's theme don't allow any of these Color class code so i have to use a hex code. :-( – Prasham Dec 21 '10 at 12:34
  • Don't you need to define your colours with opacity? Orange = 0xfff06d2f – fredley Dec 21 '10 at 12:45
  • i had tried it, and worked for me, i had posted my code as an aswer to you – Sankar Ganesh PMP Dec 21 '10 at 12:52

9 Answers9

63

try like this , the following worked fine for me

textview.setTextColor(this.getResources().getColor(R.color.orange));
laalto
  • 150,114
  • 66
  • 286
  • 303
Sankar Ganesh PMP
  • 11,927
  • 11
  • 57
  • 90
  • 2
    The key thing here is fredley's comment above - if you define a color without an opacity, then it will fail. This solution achieves this more-or-less by accident. If Presham had just added opacity to his original color definition, his original code would have worked also. – Caspar Harmer May 05 '11 at 11:17
  • or if u are in a Fragment getActivity().getResources().getColor(R.color.mycolor); – Ewoks Dec 12 '12 at 09:49
  • 7
    this.getResources().getColor() is deprecated. Its better to use ContextCompat.getColor(). – sfmirtalebi Dec 02 '16 at 21:55
24
text.setTextColor(Color.parseColor("#FFFFFF"));
AnilPatel
  • 2,356
  • 1
  • 24
  • 40
  • 1
    It's better to define the colour at the XML file and then call it using Sankar Ganesh answer. Otherwise it would be a mess if in the future you want to change that colour – Alex Cuadrón Aug 23 '18 at 11:14
9

You Can also use text.setTextColor(0xFFF06D2F);
but not just text.setTextColor(0xF06D2F);

Vins
  • 4,089
  • 2
  • 35
  • 50
5

This worked for me, and it is simple. First, import "Color"

import android.graphics.Color;

Then all you have to do is this:

text.setTextColor(Color.RED);

Just discovered this today (9/20/13). You can go ahead and declare a variable like this:

private final int ORANGE = 0xFFFF3300;

Then all you have to do is:

text.setTextColor(ORANGE);

Note that the first two hex characters are for opacity ("FF" means opaque). Then, in the example above, the second "FF" is for red, then "33" is for green, and "00" is for blue. Should be possible to create a great many colors this way.

I am pretty new at this Android programming - this is my first post to this forum. Thanks to all of you for your contributions!

Rainbow975
  • 47
  • 1
  • 2
4
textview.setTextColor(ContextCompat.getColor(context, R.color.your_color));
BDL
  • 21,052
  • 22
  • 49
  • 55
Subhanandh
  • 141
  • 1
  • 11
  • Although this code might solve the problem, a good answer always needs an explanation. – BDL Oct 03 '17 at 09:24
2

Yes, You can try this

textview.setTextColor(this.getResources().getColor(R.color.orange));
NullUserException
  • 83,810
  • 28
  • 209
  • 234
0

If you want to change your Text Color and take value from values/colors.xml If statement is holding text color for higher api because else version is depreciated in api23

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
{
textview_name.setTextColor(getColor(R.color.your_color_name));
}
else
{               textview_name.setTextColor(getResources().getColor(R.color.your_color_name));
}
0

This is what worked for me.

Import first: import android.graphics.Color;

Then you can use: textview.setTextColor(Color.BLUE);

JayM
  • 61
  • 8
0

For Kotlin just use holder.text.setTextColor(Color.RED);

Kamau Mbûgua
  • 777
  • 1
  • 10
  • 19