Recipe Changing the UI Based on Stored Data

The DataStorage activity of the previous recipe can be extended to check the shared preferences when loading, altering the behavior accordingly. In this recipe, if a username and password is already saved in the SharedPreferences file, a login page is displayed. After a successful login, the activity can successfully continue. If no login information is on file, the activity continues directly.

The main.xml layout file can be modified to be a login page, as shown in Listing 9.5. This uses two EditText objects for username and password, as covered in Chapter 4, "User Interface Layout."

Listing 9.5 res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"


<EditText android:id="@+id/userText"



<TextView android:layout_width="fill_parent"



<EditText android:id="@+id/passwordText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:password="true"

<Button android:id="@+id/loginButton"






The main activity DataStorage, as shown in Listing 9.6, is modified to first read the username and password data from the SharedPreferences instance. If these data are not set, the application launches the MyPreferences activity (Listing 9.2) directly to set the preferences. If these data are set, then the application displays the login layout main.xml shown in Figure 9.2.The button has an onClickListener that verifies whether the login information matches the username and password from the SharedPreferences file.A successful login enables the application to continue on, which in this case, just launches the MyPreferences activity. Any login attempt shows a Toast message of success or failure for illustration purposes.

Listing 9.6 src/com/cookbook/datastorage/ package com.cookbook.datastorage;


import android.content.Intent;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.preference.PreferenceManager;

import android.view.View;

import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText;

import android.widget.Toast;

public class Datastorage extends Activity { SharedPreferences myprefs; EditText userET, passwordET;

Button loginBT; ^Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

myprefs = PreferenceManager.getDefaultSharedPreferences(this); final String username = myprefs.getString("username", null); final String password = myprefs.getString("password", null); if (username ! = null && password ! = null){ setContentView(R.layout.main);

userET = (EditText)findViewByld(; passwordET = (EditText)findViewByld(; loginBT = (Button)findViewByld(; loginBT.setOnClickListener(new OnClickListener() { public void onClick(View v) { try {

if(username.equals(userET.getText().toString()) && password.equals(

passwordET.getText().toString())) { Toast.makeText(DataStorage.this, "login passed!!", Toast.LENGTH_SHORT).show(); Intent i = new Intent(DataStorage.this, myPreferences.class);

Toast.makeText(DataStorage.this, "login failed!!", Toast.LENGTH_SHORT).show();

} catch (Exception e) { e.printStackTrace();

Intent i = new Intent(this, MyPreferences.class); startActivity(i);

Figure 9.2 The login screen described by Listing 9.5.
Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment