How to set text color in android app for all text?

Android

Android Problem Overview


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
}

Android Solutions


Solution 1 - Android

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 https://stackoverflow.com/questions/39070040/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

Solution 2 - Android

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

Solution 3 - Android

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" />

Solution 4 - Android

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

Solution 5 - Android

  • 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>
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjamescView Question on Stackoverflow
Solution 1 - AndroidTheITView Answer on Stackoverflow
Solution 2 - Androiddenis.solonenkoView Answer on Stackoverflow
Solution 3 - AndroidanddevView Answer on Stackoverflow
Solution 4 - AndroidLabeeb PanampullanView Answer on Stackoverflow
Solution 5 - AndroidWojtekView Answer on Stackoverflow