1. Are you ready for the Galaxy S20? Here is everything we know so far!

How to change text color of Android's preferences?

Discussion in 'Android Development' started by vovs, Feb 9, 2011.

  1. vovs

    vovs Newbie
    Thread Starter

    Hi, All!
    I want to change the look of my Android app's preference screen to white background and dark text color...
    I try:
    Variant 1:
    In my Activity i set
    Code (Text):
    1. class SettingsActivity extends PreferenceActivity
    2. @Override
    3.     public void onCreate(Bundle savedInstanceState) {
    4.         super.onCreate(savedInstanceState);
    5.         setTheme(android.R.style.Theme_Light);
    6.         addPreferencesFromResource(R.layout.settings);
    7. ........
    Variant 2:
    Here is my AndroidManifest.xml:
    Code (Text):
    1. <activity android:name=".view.SettingsActivity"
    2.     android:theme="@android:style/Theme.Light"></activity>
    Variant 3:
    i create my custom theme:

    here is my SettingsActivity.java:
    Code (Text):
    1. public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
    3.     /** Called when the activity is first created. */
    4.     @Override
    5.     public void onCreate(Bundle savedInstanceState) {
    6.         super.onCreate(savedInstanceState);
    7.         addPreferencesFromResource(R.layout.settings);
    8. .....
    Here is my AndroidManifest.xml:

    Code (Text):
    1. <activity android:name=".view.SettingsActivity"
    2.     android:theme="@android:style/MyTheme"></activity>
    Here is my style.xml:
    Code (Text):
    1. <style name="MyTheme" >
    2.     <item name="@android:textAppearance">@style/MyDefaultTextAppearance</item>
    3.   </style>
    5.     <style name="MyDefaultTextAppearance" parent="@android:style/TextAppearance">
    6.         <item name="android:textSize">12sp</item>
    7.         <item name="android:textColor">#000000</item>
    8.         <item name="android:textStyle">bold</item>
    9.       </style>

    In all these variants I get white background but text color is too white ((
    How can i change text color to black?


    1. Download the Forums for Android™ app!


  2. ab0mbs

    ab0mbs Lurker

    I'm new to android and have not had much experience with xml files but I do know that if you create a TextView in a layout and define something like this I know the text changes to the color.
    Code (Text):
    2.   <TextView
    3.     android:id="@+id/yourid"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="wrap_content"
    6.     androidtext="@string/yourtext"
    7.     android:textColor="#0000EE"
    8.     />
  3. vovs

    vovs Newbie
    Thread Starter

    Thanks, ab0mbs,
    but it is not right way for me.
    I don't have TextViews in my settings.xml:

    Code (Text):
    1. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    3.     <PreferenceCategory
    4.         android:title="User Details">
    6.         <Preference
    7.             android:title="@string/my_details_title"
    8.             android:key="MyDetailsPref"    />
    9.        <EditTextPreference
    10.             android:name="Status"
    11.             android:defaultValue=""
    12.             android:title="Status"
    13.             android:key="StatusEditTextPref" />
    14.         <Preference
    15.             android:title="ID Share"
    16.             android:key="IDSharePref" />
    17.          <PreferenceScreen
    18.             android:key="AdressBookPrefSceen"
    19.             android:title="Adress Book Search">
    20.             <PreferenceCategory
    21.                 android:title="Search For Contact">
    22.                 <CheckBoxPreference
    23.                     android:title="Allow Access"
    24.                     android:defaultValue="true"
    25.                     android:key="AllowAccessCheckBoxPref"    />
    26. .......
  4. Tapirboy

    Tapirboy Newbie

    Sorry for asking the obvious, but have you tried leaving your preference 'unstyled'?

    It should give your dark text by default on pale background in your preferences taken you are using the light theme in your manifest on the application element.

    Code (Text):
    1. <application ... android:theme:"@android:style/Theme.Light" ..>
    2. ...
    3. </application>
  5. vovs

    vovs Newbie
    Thread Starter

    If I try leave my preference 'unstyled' - I get white text on dark background.

    But I need setting Theme.Light only for SettingsActivity, not for all Application...
  6. thomasg

    thomasg Lurker

    hey guy, did you found a solution for that? I have the same probleme and I'm not able to fix it!

  7. steve

    steve Developer

    vovs and anyone else,

    Variant 3 is one of the best ways to handle custom styled preferences. PreferenceActivity is kind of picky and do not handle all styling like regular Activities

    So.. why does your Variant 3 not work? Looking over the code quickly I think the only KEY aspect you are missing is setting the text color. Rather then "#000000" use "#FF000000".

    So the line I believe you will need to fix should look like this:

    Code (Text):
    2. <item name="android:textColor">#FF000000</item>
    ...in short, FF is the alpha of the color definition.

    API Reference

Share This Page