• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to change text color of Android's preferences?

vovs

Newbie
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:
class SettingsActivity extends PreferenceActivity
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(android.R.style.Theme_Light);
        addPreferencesFromResource(R.layout.settings);
........

Variant 2:
Here is my AndroidManifest.xml:
Code:
<activity android:name=".view.SettingsActivity"
	android:theme="@android:style/Theme.Light"></activity>

Variant 3:
i create my custom theme:

here is my SettingsActivity.java:
Code:
public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.settings);
.....

Here is my AndroidManifest.xml:

Code:
<activity android:name=".view.SettingsActivity"
	android:theme="@android:style/MyTheme"></activity>

Here is my style.xml:
Code:
<style name="MyTheme" >
    <item name="@android:textAppearance">@style/MyDefaultTextAppearance</item>
  </style>
	
	<style name="MyDefaultTextAppearance" parent="@android:style/TextAppearance">
	    <item name="android:textSize">12sp</item>
	    <item name="android:textColor">#000000</item>
	    <item name="android:textStyle">bold</item>
	  </style>


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

screen.png
 
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:
  <TextView
  	android:id="@+id/yourid"
  	android:layout_width="fill_parent"
  	android:layout_height="wrap_content"
  	androidtext="@string/yourtext"
  	android:textColor="#0000EE"
  	/>
 
Thanks, ab0mbs,
but it is not right way for me.
I don't have TextViews in my settings.xml:

Code:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
    <PreferenceCategory
        android:title="User Details">
        
        <Preference
            android:title="@string/my_details_title"
            android:key="MyDetailsPref"    />
       <EditTextPreference
            android:name="Status"
            android:defaultValue=""
            android:title="Status"
            android:key="StatusEditTextPref" />
        <Preference
            android:title="ID Share"
            android:key="IDSharePref" />
         <PreferenceScreen 
            android:key="AdressBookPrefSceen" 
            android:title="Adress Book Search">
            <PreferenceCategory
                android:title="Search For Contact"> 
                <CheckBoxPreference 
                    android:title="Allow Access" 
                    android:defaultValue="true"
                    android:key="AllowAccessCheckBoxPref"    />
.......
 
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:
<application ... android:theme:"@android:style/Theme.Light" ..>
...
</application>
 
2Tapirboy
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...
 
Variant 3:
i create my custom theme:

here is my SettingsActivity.java:
Code:
public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.settings);
.....

Here is my AndroidManifest.xml:

Code:
<activity android:name=".view.SettingsActivity"
	android:theme="@android:style/MyTheme"></activity>

Here is my style.xml:
Code:
<style name="MyTheme" >
    <item name="@android:textAppearance">@style/MyDefaultTextAppearance</item>
  </style>
	
	<style name="MyDefaultTextAppearance" parent="@android:style/TextAppearance">
	    <item name="android:textSize">12sp</item>
	    <item name="android:textColor">#000000</item>
	    <item name="android:textStyle">bold</item>
	  </style>


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

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:
<item name="android:textColor">#FF000000</item>

...in short, FF is the alpha of the color definition.

API Reference
The Color class defines methods for creating and converting color ints. Colors are represented as packed ints, made up of 4 bytes: alpha, red, green, blue. The values are unpremultiplied, meaning any transparency is stored solely in the alpha component, and not in the color components. The components are stored as follows (alpha << 24) | (red << 16) | (green << 8) | blue. Each component ranges between 0..255 with 0 meaning no contribution for that component, and 255 meaning 100% contribution. Thus opaque-black would be 0xFF000000 (100% opaque but no contributions from red, green, or blue), and opaque-white would be 0xFFFFFFFF
 
Back
Top Bottom