TimePicker in PreferenceScreen

There is no TimePreference built into Android. However, creating your own is fairly easy. Here’s one I did: import android.content.Context; import android.content.res.TypedArray; import android.preference.DialogPreference; import android.util.AttributeSet; import android.view.View; import android.widget.TimePicker; public class TimePreference extends DialogPreference { private int lastHour=0; private int lastMinute=0; private TimePicker picker=null; public static int getHour(String time) { String[] pieces=time.split(“:”); return(Integer.parseInt(pieces[0])); } … Read more

How do I display the current value of an Android Preference in the Preference summary?

There are ways to make this a more generic solution, if that suits your needs. For example, if you want to generically have all list preferences show their choice as summary, you could have this for your onSharedPreferenceChanged implementation: public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Preference pref = findPreference(key); if (pref instanceof ListPreference) { … Read more

Difference between getDefaultSharedPreferences and getSharedPreferences

getDefaultSharedPreferences will use a default name like “com.example.something_preferences”, but getSharedPreferences will require a name. getDefaultSharedPreferences in fact uses Context.getSharedPreferences (below is directly from the Android source): public static SharedPreferences getDefaultSharedPreferences(Context context) { return context.getSharedPreferences(getDefaultSharedPreferencesName(context), getDefaultSharedPreferencesMode()); } private static String getDefaultSharedPreferencesName(Context context) { return context.getPackageName() + “_preferences”; } private static int getDefaultSharedPreferencesMode() { return Context.MODE_PRIVATE; }

Is it possible to add an array or object to SharedPreferences on Android

Regardless of the API level, Check String arrays and Object arrays in SharedPreferences SAVE ARRAY public boolean saveArray(String[] array, String arrayName, Context mContext) { SharedPreferences prefs = mContext.getSharedPreferences(“preferencename”, 0); SharedPreferences.Editor editor = prefs.edit(); editor.putInt(arrayName +”_size”, array.length); for(int i=0;i<array.length;i++) editor.putString(arrayName + “_” + i, array[i]); return editor.commit(); } LOAD ARRAY public String[] loadArray(String arrayName, Context mContext) … Read more