User experience matters—a lot. Imagine this: a user logs into your app, closes it, and the next time they open it, they’re logged out. Annoying, right?

Luckily, Android gives us an easy way to store lightweight data using SharedPreferences. In this blog, we’ll build a basic login screen and save the login state so the user doesn’t have to enter their credentials every time.

Let’s dive in!

What is SharedPreferences?

SharedPreferences is a simple key-value storage system provided by Android for saving primitive data types like String, int, boolean, etc. It’s ideal for storing settings or small amounts of user data—like login status.

Step 1: Setup Your Layout

Create a simple login screen in activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="24dp"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/editTextUsername"
android:hint="Username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
android:id="@+id/editTextPassword"
android:hint="Password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/buttonLogin"
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

Step 2: Create the Login Logic

In MainActivity.java, add the login logic and use SharedPreferences to remember the user.

public class MainActivity extends AppCompatActivity {

EditText editTextUsername, editTextPassword;
Button buttonLogin;
SharedPreferences sharedPreferences;

public static final String PREF_NAME = "loginPrefs";
public static final String KEY_USERNAME = "username";
public static final String KEY_IS_LOGGED_IN = "isLoggedIn";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

sharedPreferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);

// Check if user is already logged in
if (sharedPreferences.getBoolean(KEY_IS_LOGGED_IN, false)) {
goToHome();
return;
}

setContentView(R.layout.activity_main);

editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
buttonLogin = findViewById(R.id.buttonLogin);

buttonLogin.setOnClickListener(v -> {
String username = editTextUsername.getText().toString();
String password = editTextPassword.getText().toString();

if (username.equals("admin") && password.equals("1234")) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(KEY_USERNAME, username);
editor.putBoolean(KEY_IS_LOGGED_IN, true);
editor.apply();

Toast.makeText(this, "Login successful", Toast.LENGTH_SHORT).show();
goToHome();
} else {
Toast.makeText(this, "Invalid credentials", Toast.LENGTH_SHORT).show();
}
});
}

private void goToHome() {
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(intent);
finish();
}
}

Step 3: Create the Home Screen

Create a simple HomeActivity.java that welcomes the user:

public class HomeActivity extends AppCompatActivity {

TextView welcomeText;
Button logoutButton;
SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

welcomeText = findViewById(R.id.textViewWelcome);
logoutButton = findViewById(R.id.buttonLogout);
sharedPreferences = getSharedPreferences(MainActivity.PREF_NAME, MODE_PRIVATE);

String username = sharedPreferences.getString(MainActivity.KEY_USERNAME, "User");
welcomeText.setText("Welcome, " + username);

logoutButton.setOnClickListener(v -> {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear(); // Or editor.remove(KEY_IS_LOGGED_IN);
editor.apply();

Intent intent = new Intent(HomeActivity.this, MainActivity.class);
startActivity(intent);
finish();
});
}
}

And the layout file activity_home.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:padding="24dp"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textViewWelcome"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/buttonLogout"
android:text="Logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"/>
</LinearLayout>

Final Thoughts

With just a few lines of code, you’ve implemented a basic but functional login system with auto-login and logout capabilities using SharedPreferences.

This approach is perfect for apps that don’t require strict backend validation or for storing session flags. For more secure authentication, you can combine this with Firebase Auth or encrypted storage in real-world apps.

Leave a Reply

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