Android Login Screen with Password – Setup a lock screen

Android Login Screen with Password first Register 2017

Create an simple android login screen with password first of all you have to register with your password. Added confirm password in registration. This simple android login page help you to protect your android application user must have to register first to use this. Next time if some one want to open this application need to be put user name and password.

 **You Can Check Also Android voice recognition no deprecation**

Android login screen source code

Login Screen MainActivity complete code.

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

public static String str_login;
public static SharedPreferences shpf;
public static SharedPreferences.Editor editor;

Button register;

String str_Password, str_RePassword;
EditText edit_Password, edit_RePassword;

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

shpf = getSharedPreferences(“myprefe”, 0);
editor = shpf.edit();

str_login = shpf.getString(“loginTest”, null);

register = (Button) findViewById(R.id.btn_register);
edit_Password = (EditText) findViewById(R.id.edit_password);
edit_RePassword = (EditText) findViewById(R.id.edit_Repassword);

register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

str_Password = edit_Password.getText().toString();
str_RePassword = edit_RePassword.getText().toString();

if (str_Password.length() == 0) {
Toast.makeText(getApplicationContext(), “Please enter your Secret Code”, Toast.LENGTH_LONG).show();
}
else if (str_RePassword.length() == 0) {
Toast.makeText(getApplicationContext(), “Please Re-enter your Secret code”, Toast.LENGTH_LONG).show();
}
else if (str_Password.contains(str_RePassword) != str_RePassword.contains(str_Password)) {
Toast.makeText(getApplicationContext(), “Confirm Secret code not matched”, Toast.LENGTH_LONG).show();
}

else {

MainActivity.editor.putString(“password”, str_RePassword);
MainActivity.editor.commit();

Toast.makeText(getApplicationContext(), “You have registered successfully”, Toast.LENGTH_LONG).show();
}
}
});
}

public void loginhere (View view) {
Intent sendToLogout = new Intent(getApplicationContext(), Login.class);
startActivity(sendToLogout);
}
}

android login page

Android login screen activity_main.xml source code

<?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:background=”#bfd6ce”
android:fillViewport=”true” >

<EditText
android:id=”@+id/edit_password”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:layout_marginTop=”69dp”
android:background=”#8bffe8″
android:ems=”12″
android:hint=”Password”
android:inputType=”textPassword”
android:padding=”10dp”
android:layout_alignParentTop=”true”
android:layout_alignStart=”@+id/edit_Repassword”>
</EditText>

<EditText
android:id=”@+id/edit_Repassword”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:layout_marginTop=”19dp”
android:background=”#97ffea”
android:ems=”12″
android:hint=”Confirm Password”
android:inputType=”textPassword”
android:padding=”10dp”
android:layout_below=”@+id/edit_password”
android:layout_centerHorizontal=”true”>
</EditText>

<Button
android:id=”@+id/btn_register”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:background=”#019eff”
android:textSize=”20sp”
android:ems=”10″
android:padding=”10dp”
android:text=”Register”
android:textColor=”#ffff00″
android:textStyle=”bold”
android:layout_centerVertical=”true”
android:layout_alignParentStart=”true” />

<Button
android:id=”@+id/logInH”
android:layout_width=”200dp”
android:layout_height=”wrap_content”
android:background=”#56b3f1″
android:onClick=”loginhere”
android:text=”LOG IN HERE”
android:textSize=”20sp”
android:layout_marginTop=”95dp”
android:layout_alignTop=”@+id/btn_register”
android:layout_centerHorizontal=”true” />

</RelativeLayout>

Android login page source code

This is the page from where you can be open separate secret page if password is correct. Create new java class name it “Login” and check you have your own package name in your project.

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends AppCompatActivity {

String str_Password, str_getPassword;
EditText edt_Password;
Button login;

@SuppressLint(“NewApi”)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);

str_getPassword = MainActivity.shpf.getString(“password”, null);
login = (Button) findViewById(R.id.btn_login);
edt_Password = (EditText) findViewById(R.id.edt_password);

login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

str_Password = edt_Password.getText().toString();

if (str_Password.length() == 0) {
Toast.makeText(getApplicationContext(), “Please enter your Password”, Toast.LENGTH_LONG).show();
}

else if (!(str_getPassword.matches(str_Password))) {
Toast.makeText(getApplicationContext(), “Password is incorrect”, Toast.LENGTH_LONG).show();
}

else if (str_getPassword.matches(str_Password)) {

Toast.makeText(getApplicationContext(), “You have successfuly login”, Toast.LENGTH_LONG).show();
// IF PASSWORD IS CORRECT THIS PAGE WILL BE OPEN.
Intent sendToLogout = new Intent(getApplicationContext(), MainActivity.class);
// YOU CAN ADD and OPEN SEPARATE LOG IN PAGE.
startActivity(sendToLogout);
}
}
});
}
}

android login screen

Android login page .xml layout source code

<?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:background=”#ffb3d9″
>

<EditText
android:id=”@+id/edt_password”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:background=”#b0f1ff”
android:ems=”12″
android:hint=”Password”
android:inputType=”textPassword”
android:padding=”10dp”
android:layout_alignBottom=”@+id/btn_login”
android:layout_centerHorizontal=”true”
android:layout_marginBottom=”84dp” />

<Button
android:id=”@+id/btn_login”
android:layout_width=”200dp”
android:layout_height=”wrap_content”
android:background=”#f95461″
android:ems=”10″
android:padding=”10dp”
android:text=”Login”
android:textSize=”20sp”
android:textColor=”#00fff7″
android:textStyle=”bold”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true”
android:layout_marginBottom=”151dp” />

</RelativeLayout>

Add a Comment

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