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
Create a custom theme for your app. Take look at the official guide.
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>
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);
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:
(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
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" />