Login and Logout useing SharedPrefrence.
Shared Preference is lightweight data storage mechanism for particular activity or application.
Step 1: creating xml file first of create two xml file Home.xml and main.xml like this. main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="1" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="5dp" android:text="Login:" /> <EditText android:id="@+id/login" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:text android:typeface="sans" > <requestFocus /> </EditText> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="Password:" /> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:input android:text android:typeface="sans" /> <Button android:id="@+id/loginbutton" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:text="Sign In" /> </LinearLayout> home.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="1" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="This is home screen!" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> Step 2: Creating java file in this file create java file (1)Login.java package harshid.sharedpref; public class Login extends Activity { public static final String PREFS_NAME = "LoginPrefs"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /* * Check if we successfully logged in before. * If we did, redirect to home page */ SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); if (settings.getString("logged", "").toString().equals("logged")) { Intent intent = new Intent(Login.this, Home.class); startActivity(intent); } Button b = (Button) findViewById(R.id.loginbutton); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { EditText username = (EditText) findViewById(R.id.login); EditText password = (EditText) findViewById(R.id.password); if(username.getText().toString().equals("harshid") && password.getText().toString().equals("harshid")) { //make SharedPreferences object SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("logged", "logged"); editor.commit(); Toast.makeText(getApplicationContext(), "Successfull Login", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(Login.this, Home.class); startActivity(intent); } } }); } } (2) home.java in this java file option menu is there if user click on logout button permentally clear sharedprefrence in android. public class Home extends Activity { public static final String PREFS_NAME = "LoginPrefs"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.home); } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater Inflater = getMenuInflater(); Inflater.inflate(R.menu.menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.logout) { SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.remove("logged"); editor.commit(); finish(); } return super.onOptionsItemSelected(item); } } |