I have one text box in SignUp layout and one text box in logIn layout. Now how to validate text box value using shared preferences in android?
Asked
Active
Viewed 175 times
-3
-
can't get you. explain in brief. – Shadow Jun 01 '16 at 10:43
-
I want first time user register with four digit number then redirect on login page. In login page user put same four digit number if its match then user can access otherwise they can't access. – Mohammad Arman Jun 01 '16 at 10:50
-
Possible duplicate of [how to directly pass value of a variable from 1st activty to 3rd activity using putextra?](http://stackoverflow.com/questions/12178793/how-to-directly-pass-value-of-a-variable-from-1st-activty-to-3rd-activity-using) – Bonatti Jun 01 '16 at 11:24
-
First try to understand preference – Aditya Vyas-Lakhan Jun 01 '16 at 12:18
2 Answers
1
Simple..
Considering signupText
, loginText
as object.
- Store sign up text in preference. I have used a separate class for preference. For more how to write preference in separate class and use it means, please refer here for preference..
store signupText in preference.
mPreferenceData.storeInPreference("signup",
signupText.getText().toString().trim()));
in login page
String signUpText = mPreferenceData.getStoreInPreference("signup");
//now you will have data in signupText
button.setOnClickListener(new View.OnClickListener(){
if(loginText.getText().toString().trim().equalsIgnoreCase(signUpText)){
// now loginText whatever data that matches with signUpText will make to login successful
} else{
//failure
}
});
-
I am not getting. I have two activity MainActivity.java(SignUp) and LogIn.java can u explain me in details? – Mohammad Arman Jun 01 '16 at 11:07
-
in mainactivity, that input text store in preference.. then in login page, get the preference and check with equalsIgnoreCase – Shadow Jun 01 '16 at 11:09
0
To compare values in different Actitvities first you need to store value in Preferences, To how to store values in preference Check Android User Session Management using Shared Preferences then you can compare values.
package com.androidhive.sessions;
import java.util.HashMap;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class SessionManager {
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "AndroidHivePref";
// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
// User name (make variable public to access from outside)
public static final String KEY_NAME = "name";
// Email address (make variable public to access from outside)
public static final String KEY_EMAIL = "email";
// Constructor
public SessionManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
/**
* Create login session
* */
public void createLoginSession(String name, String email){
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
// Storing name in pref
editor.putString(KEY_NAME, name);
// Storing email in pref
editor.putString(KEY_EMAIL, email);
// commit changes
editor.commit();
}
/**
* Check login method wil check user login status
* If false it will redirect user to login page
* Else won't do anything
* */
public void checkLogin(){
// Check login status
if(!this.isLoggedIn()){
// user is not logged in redirect him to Login Activity
Intent i = new Intent(_context, LoginActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
}
/**
* Get stored session data
* */
public HashMap<String, String> getUserDetails(){
HashMap<String, String> user = new HashMap<String, String>();
// user name
user.put(KEY_NAME, pref.getString(KEY_NAME, null));
// user email id
user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
// return user
return user;
}
/**
* Clear session details
* */
public void logoutUser(){
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, LoginActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
/**
* Quick check for login
* **/
// Get Login State
public boolean isLoggedIn(){
return pref.getBoolean(IS_LOGIN, false);
}
}

Aditya Vyas-Lakhan
- 13,409
- 16
- 61
- 96