April 30, 2016

In this tutorial I’m going to show you the usage of Shared Preference. In many applications we have seen that there is a login view like facebook. After login the users are not prompted that registering screen again and again, there are many ways to do it. But in this tutorial I’m going to show you with the help of Shared Preference.

Below is the program where I’m implementing two screens, in one screen there will be one input box to enter the Username and a button. On hitting that button the the username will be printed on the second page. Now whenever user will open the app, he will be prompted the second page, not the login page.

LoginActivity as a Launcher—————————————————————————————–


package com.sharedprefrence;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
 * Created by Techno Blogger on 29/4/16.
 */
public class LoginActivity extends AppCompatActivity {
    private EditText _userName;
    private Button _button;
    public static final String PREFS_NAME = "LoginPrefs";
    private String inputUser;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        _userName = (EditText) findViewById(R.id.edt_username);
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        if (settings.getString("logged", "").toString().equals("logged")) {
            Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
            startActivity(intent);
            finish();
        }
        _button = (Button) findViewById(R.id.button);
        _button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputUser = _userName.getText().toString();
                SharedPreferences sharedPreferences = getSharedPreferences("MyName", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("UserName", inputUser);
                editor.apply();
                Log.e("Input is : ", inputUser);
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor1 = settings.edit();
                editor1.putString("logged", "logged");
                editor1.apply();
                Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                startActivity(intent);
                finish(); 
            }
        });
    }
}

activity_login.xml————————————————


<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".LoginActivity">
    <EditText
        android:id="@+id/edt_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:hint="Username" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="36dp"
        android:text="Login" />
</RelativeLayout>

HomeActivity——————————————————


package com.sharedprefrence;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
/**
 * Created by Techno Blogger on 29/4/16.
 */
public class HomeActivity extends Activity {
    String _myContact;
    TextView _textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        SharedPreferences sharedPref =  getSharedPreferences("MyName", Context.MODE_PRIVATE);
        _myContact = sharedPref.getString("UserName", "");
        _textView = (TextView) findViewById(R.id.textView);
        _textView.setText(_myContact);
    }
}

activity_home.xml————————————————-


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:id="@+id/textBanner"
        android:textSize="50sp"
        android:layout_marginTop="50dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="New Text" />
</RelativeLayout>

When will you run this application you will be prompted a screen where you will be prompted to enter your name. After hitting the send button that name will be printed on the second page. Once the user enter his/her name, he/she will never be prompted that screen again. Try it and have fun.

Happy Coding.