开发者

Android setting text view color from java code

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.setF开发者_JS百科ocusable(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


try like this , the following worked fine for me

textview.setTextColor(this.getResources().getColor(R.color.orange));


text.setTextColor(Color.parseColor("#FFFFFF"));


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


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!


textview.setTextColor(ContextCompat.getColor(context, R.color.your_color));


Yes, You can try this

textview.setTextColor(this.getResources().getColor(R.color.orange));


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


This is what worked for me.

Import first: import android.graphics.Color;

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜