Code example for EditTextPreference

Methods: getOnPreferenceChangeListenersetOnPreferenceChangeListener, getEditText, getText, onPreferenceChange

-1
public class MyPrefScreen extends PreferenceActivity {
	static final LogCategory log = new LogCategory("MyPrefScreen");
	 
	void initNumberEdit(String key,int min,int max,int def,int desc_string_id){
		EditTextPreference pref = (EditTextPreference)findPreference(key);
		EditText et = pref.getEditText();
		et.setHorizontallyScrolling(true);
		et.setSingleLine(true);
		et.setInputType(InputType.TYPE_CLASS_NUMBER);
 
		final int _min = min;
		final int _max = max;
		final int _def = def;
		final int _desc_string_id = desc_string_id;
		 
		pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override 
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				if(!MyApp.checkInteger(newValue,_min,_max)){
					Toast.makeText(MyPrefScreen.this,R.string.number_range_error,Toast.LENGTH_SHORT).show();
					return false; 
				}else{ 
					preference.setSummary(String.format(
							getResources().getString(_desc_string_id)
							,MyApp.parseInt(newValue.toString(),-_def,_min,_max)
						)); 
					return true; 
				} 
			} 
		}); 
		 
		pref.getOnPreferenceChangeListener().onPreferenceChange(pref,pref.getText());
	} 
	 
	@Override 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);