Search code examples
androidcolor-picker

AmbilWarna - Preferences - Set Color


This is my first try with AmbilWarna(Android Color Picker), so please bear with me.

I am trying to use the Preferences to set color to my textview. Here is what i have done:

Settings.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <PreferenceCategory android:title="@string/prefs_color_heading" />

    <yuku.ambilwarna.widget.AmbilWarnaPreference
        android:defaultValue="0xff000000"
        android:key="display_foreground_color"
        android:title="Pick a color for text"
        app:supportsAlpha="true" />

    <!--  -->

    <yuku.ambilwarna.widget.AmbilWarnaPreference
        android:defaultValue="0xfffff380"
        android:key="display_background_color"
        android:title="Pick a color for background"
        app:supportsAlpha="true" />

</PreferenceScreen>

SettingsFragment:

public class SettingsFragment extends PreferenceFragment implements
        OnPreferenceClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);

        addPreferencesFromResource(R.xml.settings);

        findPreference(Const.Prefs.DISPLAY_FOREGROUND_COLOR)
                .setOnPreferenceClickListener(this);
        findPreference(Const.Prefs.DISPLAY_BACKGROUND_COLOR)
                .setOnPreferenceClickListener(this);

    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        if (preference.getKey().equals(Const.Prefs.DISPLAY_FOREGROUND_COLOR)) {
            return true;
        } else if (preference.getKey().equals(
                Const.Prefs.DISPLAY_BACKGROUND_COLOR)) {
            return true;
        }

        return false;
    }
}

When i click on the preference item, for example, "Pick a color for text", I get a color picker dialog to choose the color from. I select the color, and the color is returned as an "int". Now my problem is, i don't understand how to set this returned int value to my textview.

Here is the int returned from Log:

09-26 11:50:14.440: D/AVCP(24815): Foreground Color: -5954010

How do i set this int to my textview?


Solution

  • You can set it directly to the textview...

    int foreground_color = prefs.getInt(
                    Const.Prefs.DISPLAY_FOREGROUND_COLOR, 0);
    
    tvExpression = (TextView) findViewById(R.id.textView_expression);
            tvExpression.setTextColor(foreground_color);