Open top menu



In this tutorial Implement Shared preferences how to store and get value locally Database in android Application. Shared preferences very used full the state of an activity means that data will remain saved till the application is installed in the device.Shared preferences also work as sessions which are used for the automatic login process.  

Explain Some Methods-

  • clear()-Remove the all information from the editor
  • putString()- value save the value in preferences editor,put method more type for example,putFloat(),putLong(),putInt() etc.
  • getString("key",null)- via this method get value with help of key from the preferences editor. 




public class MainActivity extends Activity {

EditText edName, edphone, edemail;
Button butnSave;
public static final String MyPREFERENCES = "MyPrefsdata";

SharedPreferences sharedpreferences;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle("SHARED PREFRENCES TEST");
setContentView(R.layout.activity_main);

edName = (EditText) findViewById(R.id.editText);
edphone = (EditText) findViewById(R.id.editText2);
edemail = (EditText) findViewById(R.id.editText3);

butnSave = (Button) findViewById(R.id.button);
sharedpreferences = getSharedPreferences(MyPREFERENCES,
Context.MODE_PRIVATE);

butnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Name = edName.getText().toString();
String Phone = edphone.getText().toString();
String  Email= edemail.getText().toString();

SharedPreferences.Editor editor = sharedpreferences.edit();
// ("key", InsertValue)
editor.putString("name", Name);
editor.putString("phone", Phone);
editor.putString("email", Email);
editor.commit();
Toast.makeText(MainActivity.this, "Thanks", Toast.LENGTH_LONG)
.show();
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Android Beginnerpoint"
      android:id="@+id/textView2"
      
      android:layout_centerHorizontal="true"
      android:textSize="25dp"
      />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText"
      android:layout_below="@+id/textView2"
      android:layout_marginTop="67dp"
      android:hint="Name"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText2"
      android:layout_below="@+id/editText"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:hint="Phone" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText3"
      android:layout_below="@+id/editText2"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:hint="Email" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Save"
      android:id="@+id/button"
      android:layout_below="@+id/editText3"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="50dp" />

</RelativeLayout>






Tagged

0 comments