Boiler Plate Android ListPreference Implementation Example

/values/arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="pref_capacitive_backlight_entries">        
        <item>Off</item>
        <item>Dim</item>
        <item>Full On (Default)</item>
    </string-array>
    <string-array name="pref_capacitive_backlight_values">        
        <item>0</item>
        <item>2</item>
        <item>20</item>
    </string-array>
</resources>

/xml/misc_prefs.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen android:key="sense_pref_root"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="Misc Tweaks">
       	<ListPreference android:entries="@array/pref_capacitive_backlight_entries" android:title="Capacitive Backlight" android:key="pref_capacitive_backlight" android:entryValues="@array/pref_capacitive_backlight_values" android:summary="Brightness of backlight behind bottom capacitive buttons" android:defaultValue="20" />
    </PreferenceCategory>
</PreferenceScreen>

MiscActivity.java


package com.roman.tweaks.activities;

import com.roman.tweaks.R;
import com.roman.tweaks.ShellInterface;


import android.content.Context;

import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import android.util.Log;

public class MiscActivity extends PreferenceActivity implements OnPreferenceChangeListener {
    String pref;

    Context mContext;

    
    private static final String PREF_CAPACITIVE_BACKLIGHT = "pref_capacitive_backlight";

    
    ListPreference mCapacitiveBacklight;
    
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this.getApplicationContext();
        addPreferencesFromResource(R.xml.misc_prefs);
        PreferenceScreen prefs = getPreferenceScreen();

   
        mCapacitiveBacklight = (ListPreference) prefs.findPreference(PREF_CAPACITIVE_BACKLIGHT);  
        mCapacitiveBacklight.setOnPreferenceChangeListener(this);          

    }    

    

    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (preference == mCapacitiveBacklight) {
            int valCapacitiveBacklight = Integer.valueOf((String) newValue);

            Settings.System.putInt(getContentResolver(), "tweaks_capacitive_backlight",
            		valCapacitiveBacklight);
            
            if (ShellInterface.isSuAvailable()) {
				ShellInterface.runCommand("echo '"+valCapacitiveBacklight+"' > /sys/devices/platform/leds-pm8058/leds/button-backlight/currents");
			}     
            
            
            return true;
        } 

        return false;
    }
}

AndroidManifest.xml Additions:

 <activity android:label="Tweaks - Misc Options" android:name=".activities.MiscActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
Advertisements

3 thoughts on “Boiler Plate Android ListPreference Implementation Example

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s