How to set text color of a TextView programmatically?
Use,..
Color.parseColor("#bdbdbd");
like,
mTextView.setTextColor(Color.parseColor("#bdbdbd"));
Or if you have defined color code in resource's color.xml
file than
(From API >= 23)
mTextView.setTextColor(ContextCompat.getColor(context, R.color.<name_of_color>));
(For API < 23)
mTextView.setTextColor(getResources().getColor(R.color.<name_of_color>));
How to set the text color of TextView in code?
You should use:
holder.text.setTextColor(Color.RED);
You can use various functions from the Color
class to get the same effect of course.
Color.parseColor
(Manual) (like LEX uses)text.setTextColor(Color.parseColor("#FFFFFF"));
Color.rgb
andColor.argb
(Manual rgb) (Manual argb) (like Ganapathy uses)holder.text.setTextColor(Color.rgb(200,0,0));
holder.text.setTextColor(Color.argb(0,200,0,0));And of course, if you want to define your color in an
XML
file, you can do this:<color name="errorColor">#f00</color>
because the
getColor()
function is deprecated1, you need to use it like so:ContextCompat.getColor(context, R.color.your_color);
You can also insert plain HEX, like so:
myTextView.setTextColor(0xAARRGGBB);
Where you have an alpha-channel first, then the color value.
Check out the complete manual of course, public class Color extends Object.
1This code used to be in here as well:
textView.setTextColor(getResources().getColor(R.color.errorColor));
This method is now deprecated in Android M. You can however use it from the contextCompat in the support library, as the example now shows.
How to set text color for all the TextViews programatically?
You can use Custom TextView
for that.
As shown here:
MyTextView mTxt = new MyTextView(getApplicationContext()); //Use MyTextView instead of TextView where you want to apply color
mTxt.setText("Some text");
Your custom class MyTextView
would be something like:
public class MyTextView extends TextView{
public MyTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.setTextColor(Color.GREEN); //change color as per your need here.
}
}
Hope it helps.
Android: Set textcolor for programmatically created TextView
According to your xml file you need to change
txtNumber.setTextColor(getResources().getColor(R.color.blue));
to
txtNumber.setTextColor(getResources().getString(R.color.blue));
Further more you can make color.xml
file in your values
folder and in that use
<color name="mycolor">#33CCCC</color>
now just use this way
txtNumber.setTextColor(getResources().getColor(R.color.mycolor));
Set Text Color for textView Android
You need to create a set of styles in your xml (regularly in res/values/styles.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="gray">#eaeaea</color>
<color name="titlebackgroundcolor">#00abd7</color>
<color name="titlecolor">#666666</color>
<resources>
In the layout files you can call to the colors or styles:
android:textColor="@color/titlecolor"
Checkout some examples:
http://developer.android.com/guide/topics/ui/themes.html
Programmatically set text color to primary android textview
Finally I used the following code to get the primary text color of the theme -
// Get the primary text color of the theme
TypedValue typedValue = new TypedValue();
Resources.Theme theme = getActivity().getTheme();
theme.resolveAttribute(android.R.attr.textColorPrimary, typedValue, true);
TypedArray arr =
getActivity().obtainStyledAttributes(typedValue.data, new int[]{
android.R.attr.textColorPrimary});
int primaryColor = arr.getColor(0, -1);
How to set the text color of TextView in code?
You should use:
holder.text.setTextColor(Color.RED);
You can use various functions from the Color
class to get the same effect of course.
Color.parseColor
(Manual) (like LEX uses)text.setTextColor(Color.parseColor("#FFFFFF"));
Color.rgb
andColor.argb
(Manual rgb) (Manual argb) (like Ganapathy uses)holder.text.setTextColor(Color.rgb(200,0,0));
holder.text.setTextColor(Color.argb(0,200,0,0));And of course, if you want to define your color in an
XML
file, you can do this:<color name="errorColor">#f00</color>
because the
getColor()
function is deprecated1, you need to use it like so:ContextCompat.getColor(context, R.color.your_color);
You can also insert plain HEX, like so:
myTextView.setTextColor(0xAARRGGBB);
Where you have an alpha-channel first, then the color value.
Check out the complete manual of course, public class Color extends Object.
1This code used to be in here as well:
textView.setTextColor(getResources().getColor(R.color.errorColor));
This method is now deprecated in Android M. You can however use it from the contextCompat in the support library, as the example now shows.
Related Topics
Picasso V/S Imageloader V/S Fresco VS Glide VS Coil
Passing a Bundle on Startactivity()
How to Get the Device'S Imei/Esn Programmatically in Android
Android: How to Get String from Resources Using Its Name
How to Use Drawerlayout to Display Over the Actionbar/Toolbar and Under the Status Bar
Post Multipart Request With Android Sdk
Can the Android Drawable Directory Contain Subdirectories
Handler VS Asynctask VS Thread
Filter Logcat to Get Only the Messages from My Application in Android
Dexindexoverflowexception Only When Running Tests
How to Get the External Sd Card Path For Android 4.0+
Google-Services.Json For Different Productflavors
Detect Whether There Is an Internet Connection Available on Android