Best option to store username and password in android app

Yes, this is tricky on Android. You don’t want to store the plaintext password in the preferences, because anyone with a rooted device will basically be displaying their password to the world. On the flip side, you can’t use an encrypted password, because you’d have to store your encryption/decryption key somewhere on the device, again … Read more

Initialize preferences from XML in the main Activity [duplicate]

In onCreate() of your main Activity just call the PreferenceManager.setDefaultValues() method. PreferenceManager.setDefaultValues(this, R.xml.preference, false); This will read your preference.xml file and set the default values defined there. Setting the readAgain argument to false means this will only set the default values if this method has never been called in the past so you don’t need … Read more

Android: Creating custom preference

The Android Developer page only shows how to make a DialogFragment. It’s still possible to customise the appearance of a Preference item though. In your XML you have to declare the root element as android:id=”@android:id/widget_frame, and then declare TextView as android:title and android:summary. You can then declare other elements you want to appear in the … Read more

Single page PreferenceActivity w/no headers/fragments?

New preferred way is to show a single PreferenceFragment as the main content of any activity. It doesn’t need to be PreferenceActivity. See the APIs demo sample public class FragmentPreferences extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Display the fragment as the main content. getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragment()).commit(); } public static class … Read more

How to set the Default Value of a ListPreference

You don’t need to programmatically handle the default value of ListPreferences. You can do this in xml setting file. Below is an example <string-array name=”opts”> <item>red</item> <item>green</item> <item>blue</item> </string-array> <string-array name=”opts_values”> <item>1</item> <item>2</item> <item>3</item> </string-array> … <ListPreference android:title=”Colour select” android:summary=”Select your favourite” android:key=”colour” android:entries=”@array/opts” android:entryValues=”@array/opts_values” android:defaultValue=”2″ /> here I selected 2 as a default value. … Read more

Where to store Android preference keys?

I’ve found that it’s possible to store keys in strings.xml and refer to them from preferences.xml just like all other values android:key=”@string/preference_enable”. In code you can refer to key by typing getString(R.string.preference_enable) You can mark the string to not be translated using a <xliff:g> tag. See Localization Checklist <string name=”preference_enable”><xliff:g id=”preference_key”>enable</xliff:g></string>

How to detect if changes were made in the preferences?

Do SharedPreferences.OnSharedPreferenceChangeListener spChanged = new SharedPreferences.OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { // your stuff here } }; In your PreferenceActivity, ie make it a member of your PreferenceActivity class and then do registerOnSharedPreferenceChangeListener(spChanged) in the PreferenceActivity.onCreate() method. That’s what I do and I never have a problem. Else it’s your conditional … Read more

Misbehavior when trying to store a string set using SharedPreferences

This “problem” is documented on SharedPreferences.getStringSet. The SharedPreferences.getStringSet returns a reference of the stored HashSet object inside the SharedPreferences. When you add elements to this object, they are added in fact inside the SharedPreferences. That is ok, but the problem comes when you try to store it: Android compares the modified HashSet that you are … Read more

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)