65

I want to define a default text color for my android app.

I have a base activity class, that all activities are extended from it and I thought this might be a good place to define the colors.

If not what is a better solution? Maybe styles?

Trouble is this, all is new to me, so feel free to advise me and provide code snippets and explanations as well.

This is what my base class looks like. As you can see it's pretty empty

package com.ccslocal.mobile.quiz.jls;

import android.app.Activity;
import android.os.Bundle;

public class BaseActivity extends Activity {
    //set up app preferences here
}
Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112
jamesc
  • 12,423
  • 15
  • 74
  • 113

6 Answers6

97

As was mentioned in denis.solonenko's answer, the correct approach would be to modify your theme.

Where you define your theme (in your themes.xml or styles.xml file), you'll want to add something like this:

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
    ...
    <item name="android:textColor">#FF00FF</item>
    ...
</style>

Then make sure that the theme is applied to your Activity or Application in the manifest:

<application
    ...
    android:theme="@style/AppTheme"
    .... 
    >

You can also define:

  • textColor - The default text color of any given view
  • textColorPrimary - The default text color for enabled buttons and Large Textviews
  • textColorSecondary - The default text color for Medium and Small Textviews
  • textColorTertiary - ?

(Source TextColor vs TextColorPrimary vs TextColorSecondary)

Keep in mind that many other things may override these predefined colors, such as applied styles or definitions in different resource folders.

See here for a full list of theme items: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml

Community
  • 1
  • 1
TheIT
  • 11,919
  • 4
  • 64
  • 56
  • 6
    This helped me a lot, though I used `name="android:textColor"` instead of `name="android:textColorPrimary"`. – jahu Aug 23 '14 at 19:47
18

Create a custom theme for your app. Take look at the official guide.

denis.solonenko
  • 11,645
  • 2
  • 28
  • 23
  • 1
    Is there a more generic solution? It seems odd that in an OO language I have to visit all my text views to apply a default style. – jamesc Aug 03 '11 at 04:47
  • @jamesw I believe this question was addressed to Labeeb P :) theme is the generic solution – denis.solonenko Aug 03 '11 at 04:56
  • Thanks for the reply, The question was directed at you. I can't find a way of setting the font colour in a style that does not involve visiting all the text views to tell them to use a style. What am I missing? – jamesc Aug 03 '11 at 05:04
  • @jamesw Please take a look at the "Apply a theme to an Activity or application" section. You can define your custom theme which "extends" from the standard one and override some particular attributes like default text color in your case. Then configure this theme in AndroidManifest.xml – denis.solonenko Aug 03 '11 at 05:14
15

Yes you are right you can make that using style. Or you can use TextView.getTextColors().getDefaultColor() for set default text color. Actually I never used this but I think it may be help you.

For style

<style name="TextColor">
    <item name="android:textColor">#00FF00</item>
</style>      

Then in layout file

<TextView  style="@style/TextColor" />
HpTerm
  • 8,151
  • 12
  • 51
  • 67
anddev
  • 3,144
  • 12
  • 39
  • 70
  • 5
    This is a lot more troublesome compared to simply setting the `android:textColor` (either XML or Java code) for all the views. Anw, your approach requires repeated effort for every single view! – ericn Mar 26 '14 at 09:00
  • 9
    No, this is a much better solution because if you want to change the color, you only have to do it once, not for each TextView. – Anubian Noob Jul 16 '15 at 23:36
  • 1
    what if at any point within the code you need to change the text color programmatically so it affects all the textviews, any idea on how this can be done – Mofor Emmanuel Nov 06 '19 at 09:04
10

Set your default color in your res/values/colors.xml like this

<color name="defaultTextColor">#ffffff</color>

So this color to all your texts

android:textColor="@color/defaultTextColor"

or

textView.setTextColor(R.color.defaultTextColor);
Rajul
  • 5,906
  • 2
  • 22
  • 26
Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112
7
  • Create style for TextView:

    <style name="TextViewTheme">
        <item name="android:textColor">@android:color/white</item>
    </style>
    
  • Apply it in style for App:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textViewStyle">@style/TextViewTheme</item>
    </style>
    
  • And remember to change style in AndroidManifest.xml:

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        ...
    </application>
    
Wojtek
  • 1,210
  • 3
  • 18
  • 23
0

@android:color/white

yes you can change default color of react native app only add this line in styles.xml file of android src

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 12 '22 at 12:59