Boiler Plate Android EditTextPreference Implementation Example

Create a new directory from root of project (xml) and add a new xml file (other_prefs):

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen android:title="Other Preferences"
	android:key="Other_prefscreen" xmlns:android="http://schemas.android.com/apk/res/android">
	<EditTextPreference android:title="Carrier Caption"
		android:key="pref_lockscreen_caption" android:summary="The text to display at top of the lockscreen"
		android:defaultValue="Warm TwoPointThree" android:dialogTitle="Lockscreen caption" />
</PreferenceScreen>

Create a new Activity under /src (OtherActivity.java)

package com.roman.tweaks.activities;

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

import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.provider.Settings;

public class OtherActivity extends PreferenceActivity implements
		OnPreferenceChangeListener {

	public static final String TAG = "OtherActivity";

	// String keys for preference lookup
	private static final String PREF_CARRIER_CAPTION = "pref_lockscreen_caption";

	private EditTextPreference mLockscreenCaptionPref;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		addPreferencesFromResource(R.xml.other_prefs);

		// get UI object references
		PreferenceScreen prefSet = getPreferenceScreen();

		mLockscreenCaptionPref = (EditTextPreference) prefSet
				.findPreference(PREF_CARRIER_CAPTION);

		mLockscreenCaptionPref.setOnPreferenceChangeListener(this);
	}

	
	public boolean onPreferenceChange(Preference preference, Object newValue) {

		if (preference == mLockscreenCaptionPref) {

			String inputCarrierText = String.valueOf((String) newValue);
			Settings.System.putString(getContentResolver(),
					"tweaks_lockscreen_Caption", inputCarrierText);
			
			// Perform functions on preference change
			if (ShellInterface.isSuAvailable()) {
				ShellInterface.runCommand("echo '"+inputCarrierText+"' > /system/customize/lock_carrier.txt");
			}
			return true;

		}

		return false;
	}

}

Used in Warm Tweaks. See LockScreensActivity.java

Advertisements

One thought on “Boiler Plate Android EditTextPreference 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