114

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 ?

Lukap
  • 31,523
  • 64
  • 157
  • 244

15 Answers15

137

Actually the color TextView is:

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

or

#808080
Alex Zaraos
  • 6,443
  • 2
  • 26
  • 21
  • 5
    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. – k2col Oct 20 '15 at 19:24
  • 8
    Very close, but this colour is not the same. – LukaszTaraszka Apr 16 '17 at 12:47
  • 2
    @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... – me_ Sep 11 '18 at 07:04
  • Also, it does not exists in `marshmallow` – Jagar Jun 14 '22 at 07:13
102

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.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • Most correct solution. Preserve text color states (disabled, etc) – dasar Jan 11 '17 at 12:02
  • 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. – shiouming Dec 20 '17 at 23:54
  • 2
    @shiouming 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)](https://developer.android.com/reference/android/widget/TextView.html#TextView(android.content.Context,%20android.util.AttributeSet,%20int,%20int)) – Bonatti Sep 26 '18 at 13:50
  • 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. – Vitaly Aug 12 '21 at 06:19
41

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

int c = getResources().getColor(android.R.color.primary_text_dark);
Daiwik Daarun
  • 3,804
  • 7
  • 33
  • 60
davenpcj
  • 12,508
  • 5
  • 40
  • 37
  • 4
    It should be `int c = ...` instead of `Color c = ...` – Kevin Cruijssen Jun 10 '14 at 09:57
  • 7
    As of API level23, `getResources().getColor(int id)` is now deprecated (see [link](https://developer.android.com/reference/android/content/res/Resources.html#getColor%28int%29)). You can either use `getResources().getColor (int id, Resources.Theme theme)` or `ContextCompat.getColor(contex, android.R.color.primary_text_dark)` – InfectedPacket Dec 30 '15 at 18:56
  • 1
    primary_text_dark is now deprecated but no idea why :( – Impulse The Fox Aug 12 '20 at 20:18
17

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();
Bondax
  • 3,143
  • 28
  • 35
  • This looks like it will properly select the colour based on theme, and will update for instance, if the app is in night mode. – Brill Pappin Aug 26 '15 at 15:47
  • To get the TypedArray for the current Theme call it without the Theme arg: TypedArray a = getTheme().obtainStyledAttributes(attrs); – petrsyn Mar 09 '16 at 08:59
  • 1
    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. – Lorne Laliberte Oct 03 '16 at 22:58
11

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

Patrick
  • 33,984
  • 10
  • 106
  • 126
  • 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](https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/color/) and looking up the base color reference in [this .xml file](https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/colors.xml). – Alex Gittemeier May 07 '19 at 03:34
9

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

textPrimaryColor = #000000

and

textColorPrimaryDark = #757575
4

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

dave o grady
  • 788
  • 8
  • 12
4

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());
Chuck
  • 1,110
  • 3
  • 15
  • 22
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

Salam El-Banna
  • 3,784
  • 1
  • 22
  • 34
0

hey you can try this

ColorStateList colorStateList = textView.getTextColors();
String hexColor = String.format("#%06X", (0xFFFFFF & colorStateList.getDefaultColor()));
Vivek Pratap Singh
  • 1,564
  • 16
  • 26
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

quealegriamasalegre
  • 2,887
  • 1
  • 13
  • 35
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)
}
Arst
  • 3,098
  • 1
  • 35
  • 42
0

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

C Nick
  • 2,500
  • 2
  • 17
  • 21
  • 12
    wow hardcoded stuff, you know. Was this the value for all Android versions on all devices and with all selectable themes?! Joking, You know... :) – Bondax Nov 20 '12 at 14:51
  • 1
    So the default text color in android is pink? I think it is more inline with #060001 http://developer.android.com/reference/android/R.color.html#primary_text_dark – Patrick Feb 13 '15 at 17:10
  • But I believe that default text color is green – Vitaly Aug 12 '21 at 05:06
0

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();
    }
Anshul Tyagi
  • 2,076
  • 4
  • 34
  • 65
Vitaly
  • 334
  • 4
  • 14
-1

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

piotrpo
  • 12,398
  • 7
  • 42
  • 58