What is default color for text in textview?
Asked Answered
A

15

117

I set the color to red , and after that I want to set the color again back to default, but I do not know what is default color, does anyone knows ?

Alesha answered 24/6, 2011 at 13:39 Comment(0)
C
103

You can save old color and then use it to restore the original value. Here is an example:

ColorStateList oldColors =  textView.getTextColors(); //save original colors
textView.setTextColor(Color.RED);
....
textView.setTextColor(oldColors);//restore original colors

But in general default TextView text color is determined from current Theme applied to your Activity.

Connor answered 24/6, 2011 at 13:51 Comment(4)
Most correct solution. Preserve text color states (disabled, etc)Gael
From my observation, text color defined by theme is not inherited by TextView added dynamically from code. It always appears in white regardless of dark/light theme.Len
@Len Depends of the Context used. Every Constructor uses a Context, and in that Context, a Theme is set (usually default). If needed, use the TextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)Persistence
But! After for example theme changing oldColors was color for old theme - so it's not completely working solution. As alternative: You can add to the layout invisible TextView - and get default text color from it.Boys
L
140

Actually the color TextView is:

android:textColor="@android:color/tab_indicator_text"

or

#808080
Lyrist answered 16/10, 2015 at 18:11 Comment(4)
That is the default tab indicator text color. In many cases it may be the same as the default text color, but I wouldn't rely on it.Morph
Very close, but this colour is not the same.Homologize
@LukTar is right, I used photoshop and checked the color... #737373 is the textview text color from a point pixel sample (sample area size of one pixel) on a view zoomed to 1000% in android studio... i think you need to look at the app's default style to get the actual value for any particualr app though...Alphabetic
Also, it does not exists in marshmallowLopes
C
103

You can save old color and then use it to restore the original value. Here is an example:

ColorStateList oldColors =  textView.getTextColors(); //save original colors
textView.setTextColor(Color.RED);
....
textView.setTextColor(oldColors);//restore original colors

But in general default TextView text color is determined from current Theme applied to your Activity.

Connor answered 24/6, 2011 at 13:51 Comment(4)
Most correct solution. Preserve text color states (disabled, etc)Gael
From my observation, text color defined by theme is not inherited by TextView added dynamically from code. It always appears in white regardless of dark/light theme.Len
@Len Depends of the Context used. Every Constructor uses a Context, and in that Context, a Theme is set (usually default). If needed, use the TextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)Persistence
But! After for example theme changing oldColors was color for old theme - so it's not completely working solution. As alternative: You can add to the layout invisible TextView - and get default text color from it.Boys
R
41

There are some default colors defined in android.R.color

int c = getResources().getColor(android.R.color.primary_text_dark);
Regimen answered 2/5, 2012 at 22:49 Comment(3)
It should be int c = ... instead of Color c = ...Moralez
As of API level23, getResources().getColor(int id) is now deprecated (see link). You can either use getResources().getColor (int id, Resources.Theme theme) or ContextCompat.getColor(contex, android.R.color.primary_text_dark)Lager
primary_text_dark is now deprecated but no idea why :(Pugilism
G
18

Get these values from attributes:

int[] attrs = new int[] { android.R.attr.textColorSecondary };
TypedArray a = getTheme().obtainStyledAttributes(R.style.AppTheme, attrs);
DEFAULT_TEXT_COLOR = a.getColor(0, Color.RED);
a.recycle();
Grenadines answered 20/11, 2012 at 14:52 Comment(3)
This looks like it will properly select the colour based on theme, and will update for instance, if the app is in night mode.Hypo
To get the TypedArray for the current Theme call it without the Theme arg: TypedArray a = getTheme().obtainStyledAttributes(attrs);Frowzy
This will only get an appropriate color if the theme is actually using textColorSecondary. Since it is possible to override this in a theme or style, this is not a very accurate way of determining what the default text color for a particular view will actually be. Note also that individual views can now be themed, so the theme associated with the view's context should be used instead of assuming the activity's theme is in effect for all of its views.Aby
G
12

There are defaults in the theme that Android uses if you don't specifiy a text color. It may be different colors in various Android UIs (e.g. HTC Sense, Samsung TouchWiz, etc). Android has a _dark and _light theme, so the defaults are different for these (but nearly black in both of them in vanilla android). It is however good practice to define your primary text color yourself for to provide a consistent style throughout the devices.

In code:

getResources().getColor(android.R.color.primary_text_dark);
getResources().getColor(android.R.color.primary_text_light);

In xml:

android:color="@android:color/primary_text_dark"
android:color="@android:color/primary_text_light"

As reference in vanilla Android the dark theme text color is #060001 and the in the light theme it's #060003 since API v1. See the android style class here

Gonzales answered 13/2, 2015 at 17:19 Comment(1)
The colors referenced in your last link are actually not colors at all, they are just android.R values that are used to look up the color. You can look up the default colors by finding the color in this directory and looking up the base color reference in this .xml file.Newsboy
F
9

I know it is old but according to my own theme editor with default light theme, default

textPrimaryColor = #000000

and

textColorPrimaryDark = #757575
Fructiferous answered 6/10, 2018 at 12:47 Comment(0)
T
5

I used a color picker on the textview and got this #757575

Tullusus answered 14/2, 2020 at 1:39 Comment(0)
H
5

It may not be possible in all situations, but why not simply use the value of a different random TextView that exists in the same Activity and that carries the colour you are looking for?

txtOk.setTextColor(txtSomeOtherText.getCurrentTextColor());
Heroworship answered 5/3, 2020 at 9:12 Comment(1)
this is the safest wayLopes
U
2

The color of text inside a TextView is totally dependent on your theme. The easiest way to know it:

  1. Add a TextView to any xml file
  2. Select the TextView
  3. Click on Split view
  4. Open the Attributes tab and scroll to the color section.

enter image description here

As you can see, according to my theme it is: @android:color/secondary_text_material_light

Uri answered 21/3, 2022 at 1:13 Comment(2)
Do you know how to change it to primary_text?Shanklin
@Shanklin you can do so by changing it in the themeUri
B
1

There are some default colours which get defined in the Themes of app. Below is the code snippet which you can use to get the current default color programmatically.

protected int getDefaultTextColor(){
        TextView textView = new TextView(getContext());
        return textView.getCurrentTextColor();
    }
Boys answered 18/11, 2021 at 6:33 Comment(0)
D
0

I believe the default color integer value is 16711935 (0x00FF00FF).

Doubleedged answered 24/6, 2011 at 13:53 Comment(3)
wow hardcoded stuff, you know. Was this the value for all Android versions on all devices and with all selectable themes?! Joking, You know... :)Grenadines
So the default text color in android is pink? I think it is more inline with #060001 developer.android.com/reference/android/…Gonzales
But I believe that default text color is greenBoys
T
0

hey you can try this

ColorStateList colorStateList = textView.getTextColors();
String hexColor = String.format("#%06X", (0xFFFFFF & colorStateList.getDefaultColor()));
Trinetta answered 31/1, 2019 at 9:34 Comment(0)
V
0

I found that android:textColor="@android:color/secondary_text_dark" provides a closer result to the default TextView color than android:textColor="@android:color/tab_indicator_text". I suppose you have to switch between secondary_text_dark/light depending on the Theme you are using

Vetter answered 21/4, 2019 at 4:46 Comment(0)
B
0

You could use TextView.setTag/getTag to store original color before making changes. I would suggest to create an unique id resource in ids.xml to differentiate other tags if you have.

before setting to other colors:

if (textView.getTag(R.id.txt_default_color) == null) {
    textView.setTag(R.id.txt_default_color, textView.currentTextColor)
}

Changing back:

textView.getTag(R.id.txt_default_color) as? Int then {
    textView.setTextColor(this)
}
Bumblebee answered 30/7, 2020 at 12:26 Comment(0)
B
-1

There is no default color. It means that every device can have own.

Bywoods answered 24/6, 2011 at 13:52 Comment(2)
No there are default colors, just that every android distribution can overwrite themGonzales
@for3st Do you know of cases that they do change it?Grainger

© 2022 - 2024 — McMap. All rights reserved.