Android simple sharedpreferences example

Shared preferences is a way to store data in an application. Shared preferences allow you to save and retrieve data in the form of key, value pair. It is mainly used for store small data.

The App looks Like

Github: https://github.com/rincemathew/SimpleSharedPreferences

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/name"
        android:layout_margin="20dp"
        android:hint="name"
        android:textSize="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/number"
        android:layout_margin="20dp"
        android:hint="number"
        android:textSize="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/save"
            android:text="save"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <Button
            android:id="@+id/retrieve"
            android:text="retrieve"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <Button
            android:id="@+id/clear"
            android:text="clear"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <Button
            android:id="@+id/delete"
            android:text="delete"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    EditText name,number;
    Button save,retrieve,clear,delete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name=findViewById(R.id.name);
        number=findViewById(R.id.number);
        save=findViewById(R.id.save);
        retrieve=findViewById(R.id.retrieve);
        clear=findViewById(R.id.clear);
        delete=findViewById(R.id.delete);

        final String Name = "nameKey";
        final String Number = "numberKey";

       // Initialization

        final SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
        final SharedPreferences.Editor editor = pref.edit();



        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Storing Data
                String n = name.getText().toString();
                String e = number.getText().toString();

                editor.putString(Name, n);
                editor.putString(Number, e);
                editor.commit();
            }
        });
        retrieve.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Retrieving Data
                name.setText(pref.getString(Name, ""));
                number.setText(pref.getString(Number, ""));
            }
        });
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Clearing the TextView
                name.setText("");
                number.setText("");
            }
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //  Clearing or Deleting Data


//                editor.clear();
//                editor.commit();

                //or

                editor.remove("nameKey");
                editor.remove("numberKey");
                editor.commit();

            }
        });


    }
}

Keep exploring more tutorial from this site. In case of any queries make a comment. Happy coding!!!!

Add a Comment

Your email address will not be published. Required fields are marked *