android sharedpreferences

Android Sharedpreferences Example

Android Sharedpreferences Example

SharedPreferences is an Interface in android. SharedPreferences is use as like as session in android. We can save data in SharedPreferences and use or update throughout the application. In this tutorial take an Activity class named SharedActivity and respective layout named activity_shared.xml and Util class where the SharedPreferences code has written.

 

Watch Video How works


Now Check Full Source Code.

 

SharedActivity.java


 

package com.androidfromhome.sharedpreference;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

 

public class SharedActivity extends Activity implements OnClickListener {

       private EditText et_password;

       private EditText et_user;

       private Button btn_add;

       private Util util;

       @Override

       protected void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.activity_shared);

              util=new Util(SharedActivity.this);

              et_password=(EditText)findViewById(R.id.et_password);

              et_user=(EditText)findViewById(R.id.et_user);

              btn_add=(Button)findViewById(R.id.btn_add);

              btn_add.setOnClickListener(this);

              if (util.getLoginStatus()) {

                     et_password.setText(util.getUserPassword());

                     et_user.setText(util.getCustomerId());

                     btn_add.setText("Destroy Shared Preferences");

                     getAlert();

 

              }

       }


       @Override

       public void onClick(View v) {

              // TODO Auto-generated method stub

              switch (v.getId()) {

              case R.id.btn_add:

                     if (util.getLoginStatus()) {

 

                           util.setInvalidateShared();

 

                           btn_add.setText("Add To Shared Preferences");  

 

                     }else{

 

                           String user_id=et_user.getText().toString();

                           String user_password=et_password.getText().toString();

                           if (!user_id.equalsIgnoreCase("")&&!user_password.equalsIgnoreCase("")) {

 

                                  util.setLoginData(user_id, user_password);

 

                                  btn_add.setText("Destroy Shared Preferences");

 

                                  if (util.getLoginStatus()) {

                                         getAlert();

                                  }

                           }

                     }

 

 

                     break;

 

              default:

                     break;

              }

 

       }

 

public void   getAlert(){

       new AlertDialog.Builder(SharedActivity.this)

     .setIcon(android.R.drawable.ic_dialog_alert)

     .setMessage("Destroy Shared Preferences?")

       .setPositiveButton("Destroy",new android.content.DialogInterface.OnClickListener(){

       public void onClick(DialogInterface dialog, int which)

       {

                     util.setInvalidateShared();

                      btn_add.setText("Add To Shared Preferences");   }

       })

       .setNegativeButton("Cancel",new android.content.DialogInterface.OnClickListener(){

       public void onClick(DialogInterface dialog, int which)

       {       }

}).show();

}

 

}

 


 

 

 

Util.java


 

package com.androidfromhome.sharedpreference;

 

import android.content.Context;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

public class Util {

          Context ctx;

          int SHARED_MODE = 0;

          private SharedPreferences shared_preferance;

          private Editor share_editor;

          private static final String SHARED_NAME = "android_from_home";

          private static final String USER_ID = "user_id";

          private static final String USER_PASSWORD = "user_password";

          private static final String LOGIN_STATUS = "login_status";

          public Util(Context ctx){

                   this.ctx=ctx;

                   shared_preferance = ctx.getSharedPreferences(SHARED_NAME, SHARED_MODE);

                   share_editor=shared_preferance.edit();

          }

           public void setLoginData(String user_id,String user_password){

                    share_editor.putBoolean(LOGIN_STATUS, true);

                   share_editor.putString(USER_ID, user_id);

                   share_editor.putString(USER_PASSWORD, user_password);

                   share_editor.commit();

          }

           public String getCustomerId(){

                   return shared_preferance.getString(USER_ID, "");

          }

 

          public boolean getLoginStatus(){

                   return shared_preferance.getBoolean(LOGIN_STATUS, false);

          }

          public String getUserPassword(){

                   return shared_preferance.getString(USER_PASSWORD, "");

          }

 

          public void setInvalidateShared(){

                    share_editor.clear();

                   share_editor.commit();

                                      }

}

 

 

 Now Check xml Code

 

activity_shared.xml


 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   xmlns:tools="http://schemas.android.com/tools"

   android:id="@+id/LinearLayout1"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   android:orientation="vertical"

   tools:context="com.androidfromhome.sharedpreference.SharedActivity" >

 

   <LinearLayout

       android:layout_width="match_parent"

       android:layout_height="match_parent"

       android:layout_gravity="center"

       android:gravity="center"

       android:orientation="vertical" >

 

       <LinearLayout

           android:layout_width="match_parent"

           android:layout_height="wrap_content"

           android:padding="10dp" >

 

           <TextView

               android:id="@+id/TextView01"

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:text="User ID:     "

               android:textColor="#000000"

               android:textSize="20dp" />

 

           <EditText

               android:id="@+id/et_user"

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:layout_margin="2dp"

               android:layout_weight="1"

               android:background="#dfdfdf"

               android:padding="10dp" />

       </LinearLayout>

 

       <LinearLayout

           android:layout_width="match_parent"

           android:layout_height="wrap_content"

           android:padding="10dp" >

 

           <TextView

               android:id="@+id/textView1"

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:text="Password:"

               android:textColor="#000000"

               android:textSize="20dp" />

 

           <EditText

               android:id="@+id/et_password"

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:layout_margin="2dp"

               android:layout_weight="1"

               android:background="#dfdfdf"

               android:padding="10dp" >

 

               <requestFocus />

           </EditText>

 

       </LinearLayout>

 

       <Button

           android:id="@+id/btn_add"

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="Add To Shared Preferences" />

   </LinearLayout>

</LinearLayout>

 


 

Related Post:-

Android custom calendar

Font Style For Android

Sqlite database example in android

Android CSV Reader

 

Leave a Reply

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

Get Widget