How to add Shared Prefarence in this Android Login page code

1151 views.

In android when every time I am opening app it is asking for login. How to put one time login in android, so that user can login at first time and even user closes and reopen app user must not redirect to login page?

Add Comment
1 Answer(s)
576 views.

Here we are adding values to sharedprefeences

ActivityMain.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"
 tools:context="${relativePackage}.${activityClass}" >
<TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/hello_world" />
<EditText
 android:id="@+id/editText1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_below="@+id/textView1"
 android:ems="10" >
<requestFocus />
 </EditText>
<EditText
 android:id="@+id/editText2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_below="@+id/editText1"
 android:layout_marginLeft="20dp"
 android:ems="10" 
 android:inputType="number"/>
<EditText
 android:id="@+id/editText3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/editText2"
 android:layout_below="@+id/editText2"
 android:layout_marginTop="41dp"
 android:ems="10" />
<Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText3"
 android:layout_marginLeft="20dp"
 android:layout_marginTop="29dp"
 android:layout_toRightOf="@+id/textView1"
 android:text="Button" />
</RelativeLayout>

MainActivity.Java

package com.example.sharedpreferences;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText et1,et2,et3;
 Button b;
 public static final String MyPREFERENCES = "MyPrefs";
 public static final String Name = "nameKey";
 public static final String Phone = "phoneKey";
 public static final String mail = "emailKey";
@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 et1=(EditText)findViewById(R.id.editText1);
 et2=(EditText)findViewById(R.id.editText2);
 et3=(EditText)findViewById(R.id.editText3);
 b=(Button)findViewById(R.id.button1);
 b.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 String name=et1.getText().toString();
 String num=et2.getText().toString();
 String Email=et3.getText().toString();
 SharedPreferences preferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
 SharedPreferences.Editor editor = preferences.edit();
 editor.putString(Name, name);
 editor.putString(Phone, num);
 editor.putString(mail, Email);
 editor.commit();
 Intent in=new Intent(MainActivity.this, SecondActivity.class);
 startActivity(in);
 }
 });
 }
}

Here getting values from Shared preferences

Activity_second.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"
 tools:context="${relativePackage}.${activityClass}" >
<TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/hello_world"
 android:id="@+id/tv1" />
</RelativeLayout>

 

Second.Java

package com.example.sharedpreferences;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity {
 public static final String MyPREFERENCES = "MyPrefs";
 public static final String Name = "";
 public static final String Phone = "";
 public static final String mail = "";
 TextView t;
 String n="",p="",e="";
@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_second);
 SharedPreferences preferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
 n=preferences.getString("nameKey", null);
 p=preferences.getString("phoneKey", null);
 e=preferences.getString("emailKey", null);
 System.out.println("Vales"+p);
 t=(TextView) findViewById(R.id.tv1);
 t.setText("HIIIIIII"+n+p+e);
 }
}
Answered on July 5, 2016.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.