Android Preferences ExampleAndroid shared preference is used to store and retrieve primitive information. In android, string, integer, long, number etc. are considered as primitive data type. Android Shared preferences are used to store data in key and value pair so that we can retrieve the value on the basis of key. It is widely used to get information from user such as in settings. Android Preferences ExampleLet's see a simple example of android shared preference. activity_main.xmlDrag one textview and two buttons from the pallete. File: activity_main.xml array.xmlIt is created inside res/values directory. File: array.xml prefs.xmlIt is created inside res/xml directory. File: prefs.xml Main Activity ClassFile: MainActivity.java package example.javatpoint.com.preferences; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button storeinformation = (Button) findViewById(R.id.storeinformation); Button showinformation = (Button) findViewById(R.id.showinformation); textView = (TextView) findViewById(R.id.txtPrefs); View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.storeinformation: Intent intent = new Intent(MainActivity.this,PrefsActivity.class); startActivity(intent); break; case R.id.showinformation: displaySharedPreferences(); break; default: break; } } }; storeinformation.setOnClickListener(listener); showinformation.setOnClickListener(listener); } private void displaySharedPreferences() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); String username = prefs.getString("username", "Default NickName"); String passw = prefs.getString("password", "Default Password"); boolean checkBox = prefs.getBoolean("checkBox", false); String listPrefs = prefs.getString("listpref", "Default list prefs"); StringBuilder builder = new StringBuilder(); builder.append("Username: " + username + "\n"); builder.append("Password: " + passw + "\n"); builder.append("Keep me logged in: " + String.valueOf(checkBox) + "\n"); builder.append("List preference: " + listPrefs); textView.setText(builder.toString()); } } PrefsActivity classFile: PrefsActivity.java package example.javatpoint.com.preferences; import android.os.Bundle; import android.preference.PreferenceActivity; public class PrefsActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.prefs); } } AndroidManifest.xmlFile: AndroidManifest.xml Output:Next TopicAndroid Internal Storage Example |