Android Login Based on Hashmap Store & Retrieve

Android Hashmap Shared Preferences :

Every app which requires users to login will have to make sure they make the UI much flexible to the user so that they can have a hassle free login experience so that it is a add on to the quality of the app.

So lets begin this Android Login based on Hashmap Store & Retrieve, Shared Preferences tutorial.

You can refer android studio login example

1.Facebook login android

2. android login example with mysql database, php

3. android studio login and register 

http://www.androidcoding.in/2016/04/22/android-tutorial-login/

 

We have seen few apps giving a pin to login instead of username and password mainly in banking sector, and few more because it makes user login much simpler.In our next android studio login activity tutorial  we will be dealing with pin based login but for this tutorial  we will be dealing with autocomplete user selection.

What ever the style of login we need to take care of user authentication, i,e, the most important factor for an app.

 

If you are new for android autocompletetextview  visit

http://www.androidcoding.in/autocomplete/

 

LoginPojo

Create a pojo class for components

private String Username;
private String Password;

 

And generate getter and setter

 

public class LoginPojo

{

    private String Username;
    private String Password;

    public String getUsername() {
        return Username;
    }

    public void setUsername(String username) {
        Username = username;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String password) {
        Password = password;
    }
}

Activity_login.Xml

Design a login panel by adding a

1)  android autocompletetextview –> for username

<AutoCompleteTextView
 android:id="@+id/username"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:hint="Username"
 android:padding="10dp"
 android:textColor="@android:color/black"
 android:textColorHint="@android:color/darker_gray" />

 

2) android edittext –> for password

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:hint="Password"
    android:inputType="textPassword"
    android:padding="10dp"
    android:textColor="@android:color/black"
    android:textColorHint="@android:color/darker_gray" />

3) android checkbox –> for remember me functionality

<CheckBox
    android:id="@+id/rememberCheck"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="Remember Me" />

 

4) Android Buttons for sign-in and sign-up

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="30dp"
    android:background="#fc668e"
    tools:context="com.androidcoding.storearraylist.LoginActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@android:color/white"
        android:padding="30dp">

    <AutoCompleteTextView
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Username"
        android:padding="10dp"
        android:textColor="@android:color/black"
        android:textColorHint="@android:color/darker_gray" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="Password"
        android:inputType="textPassword"
        android:padding="10dp"
        android:textColor="@android:color/black"
        android:textColorHint="@android:color/darker_gray" />

    <CheckBox
        android:id="@+id/rememberCheck"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Remember Me" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="signin"
            android:text="Sign-in" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="signup"
            android:text="Sign-up" />


    </LinearLayout>

    </LinearLayout>

</LinearLayout>

 

LoginActivity

Now lets start coding for the view

we are using android shared preferences(save data) and the showing them as suggestion in autocomplete textview.

 

Initialize all the components which we have specified in design.

Then in sign-up we will be checking if Remember me is checked or not

if checked

if (rememberCheck.isChecked()) {

    sharedPreferences = getSharedPreferences(key, MODE_PRIVATE);
    edit = sharedPreferences.edit();
    edit.putString(username, password);
    edit.commit();
}

 

Then as the user tries to sign-in load user data if exists using shared preferences as,

private void loaduserdata() {

    sharedPreferences = this.getSharedPreferences(key, MODE_PRIVATE);
    edit = sharedPreferences.edit();
    Map map = sharedPreferences.getAll();

    Set keySet = map.keySet();
    Iterator iterator = keySet.iterator();

    while (iterator.hasNext()) {
        LoginPojo credlist = new LoginPojo();
        String key = (String) iterator.next();
        String value = (String) map.get(key);

        credlist.setUsername(key);
        credlist.setPassword(value);

        this.credlist.add(credlist);
    }
    for (int i = 0; i < credlist.size(); i++) {
        LoginPojo bean = credlist.get(i);
    }

    List userList = getusers(credlist);

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, userList);
    username.setAdapter(adapter);
    username.setThreshold(1);
    username.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> view, View arg1, int position, long arg3) {
            password.setText(credlist.get(position).getPassword());
        }
    });
}

private List getusers(ArrayList<LoginPojo> users) {
    List list = new ArrayList();
    for (LoginPojo c : users) {
        list.add(c.getUsername());
    }
    return list;

}

 

And now if user credentials is not remembered according to user wish we need to check whether user is logged in or not.

If user is registered move to next screen as a result of successful login.

 

for (LoginPojo d : credlist) {

    if (d.getUsername() != null && d.getUsername().contains(username.getText().toString())) {

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

    }

 

 

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class LoginActivity extends AppCompatActivity {

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor edit;
    private AutoCompleteTextView username;
    EditText password;
    CheckBox rememberCheck;
    ArrayList<LoginPojo> credlist = new ArrayList<LoginPojo>();
    private ArrayAdapter<String> adapter;
    public static String key = "cred";


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

        username = (AutoCompleteTextView) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        rememberCheck = (CheckBox) findViewById(R.id.rememberCheck);

        loaduserdata();


    }

    public void signin(View view) {

        for (LoginPojo d : credlist) {

            if (d.getUsername() != null && d.getUsername().contains(username.getText().toString())) {

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

            } 
        }


    }

    public void signup(View view) {

        String username = this.username.getText().toString();
        String password = this.password.getText().toString();

        if (rememberCheck.isChecked()) {

            sharedPreferences = getSharedPreferences(key, MODE_PRIVATE);
            edit = sharedPreferences.edit();
            edit.putString(username, password);
            edit.commit();
        }

        Toast.makeText(this, "Signed-Up Successfully", Toast.LENGTH_SHORT).show();

    }


    private void loaduserdata() {

        sharedPreferences = this.getSharedPreferences(key, MODE_PRIVATE);
        edit = sharedPreferences.edit();
        Map map = sharedPreferences.getAll();

        Set keySet = map.keySet();
        Iterator iterator = keySet.iterator();

        while (iterator.hasNext()) {
            LoginPojo credlist = new LoginPojo();
            String key = (String) iterator.next();
            String value = (String) map.get(key);

            credlist.setUsername(key);
            credlist.setPassword(value);

            this.credlist.add(credlist);
        }
        for (int i = 0; i < credlist.size(); i++) {
            LoginPojo bean = credlist.get(i);
        }

        List userList = getusers(credlist);

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, userList);
        username.setAdapter(adapter);
        username.setThreshold(1);
        username.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> view, View arg1, int position, long arg3) {
                password.setText(credlist.get(position).getPassword());
            }
        });
    }

    private List getusers(ArrayList<LoginPojo> users) {
        List list = new ArrayList();
        for (LoginPojo c : users) {
            list.add(c.getUsername());
        }
        return list;

    }


}

 

Activity_home.Xml

once the user logged in successfully we are showing a list of users logged in you make change functionality of this page accordingly.

 

<?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"
    android:orientation="vertical">

    <TextView
        android:id="@+id/signedusers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Signed Up Users"
        android:padding="10dp"
        android:textSize="18dp"
        android:gravity="center"
        android:layout_marginTop="1dp"
        android:background="@color/colorPrimary"
        android:textColor="@android:color/white"/>

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/signedusers"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        android:textColor="@android:color/white"
        android:text="Logout"/>


</RelativeLayout>

 

HomeActivity

Initialize listview and shared preferences and fetch the list of users registered.

sharedPreferences = this.getSharedPreferences(key, MODE_PRIVATE);
edit = sharedPreferences.edit();
Map map = sharedPreferences.getAll();

Set keySet = map.keySet();
Iterator iterator = keySet.iterator();

while (iterator.hasNext())
{
    LoginPojo credlist =new LoginPojo();
    String key = (String) iterator.next();
    String value = (String)map.get(key);

    credlist.setUsername(key);
    credlist.setPassword(value);

    this.credlist.add(credlist);
}
for (int i = 0; i < credlist.size(); i++)
{
    LoginPojo bean= credlist.get(i);
}

List userList = getusers(credlist);

 

Set the listview to adapter

adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, userList);
listView.setAdapter(adapter);

 

 

 

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Abhishek on 10/15/2017.
 */

public class HomeActivity extends AppCompatActivity{

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor edit;
    public static String key = "cred";
    ListView listView;
    private ArrayAdapter<String> adapter;
    ArrayList<LoginPojo> credlist =new ArrayList<LoginPojo>();

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

        listView = (ListView) findViewById(R.id.listView);

        sharedPreferences = this.getSharedPreferences(key, MODE_PRIVATE);
        edit = sharedPreferences.edit();
        Map map = sharedPreferences.getAll();

        Set keySet = map.keySet();
        Iterator iterator = keySet.iterator();

        while (iterator.hasNext())
        {
            LoginPojo credlist =new LoginPojo();
            String key = (String) iterator.next();
            String value = (String)map.get(key);

            credlist.setUsername(key);
            credlist.setPassword(value);

            this.credlist.add(credlist);
        }
        for (int i = 0; i < credlist.size(); i++)
        {
            LoginPojo bean= credlist.get(i);
        }

        List userList = getusers(credlist);

        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, userList);
        listView.setAdapter(adapter);

    }

    private List getusers(ArrayList<LoginPojo> users) {
        List list = new ArrayList();
        for (LoginPojo c : users) {
            list.add(c.getUsername());
        }
        return list;

    }

 

Output :

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

How to extract filename from Uri?

Now, we can extract filename with and without extension :) You will convert your bitmap to uri and get the real path of your file. Now w...