Creating Login page in Android Studio
- Learning Android
- May 22, 2020
- 2 min read
Updated: May 27, 2020
In this tutorial, I will tell you how to create an example of simple Login Page in Android Studio which takes a preset user name "user" and password "123456". If the both entries are matched with the correct values, a transition to another activity (say home page) takes place. Otherwise it rejects by creating a Toast message "Invalid Credentials".
First, we need to edit activity_main.xml for designing the main page to display two edittext boxes for taking user input for username and password.
The code snippet for activity_main.XML is given below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/login_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Name"
android:layout_marginTop="200dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:textSize="24dp"
/>
<EditText
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:inputType="textPersonName"
android:layout_below="@+id/login_text"
/>
<TextView
android:id="@+id/password_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Password"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:textSize="24dp"
android:layout_below="@+id/login"
/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:inputType="textPersonName"
android:layout_below="@+id/password_text"
/>
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_below="@+id/password"
android:textSize="24dp"
/>
</RelativeLayout>Next we will access the user input in the Java code and match them with inbuilt strings defined for username and password i.e. "user" and "123456"The code snippet for MainActivity.Java is given below:
package com.learnandroid.loginactivity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText loginText, passwordText;
Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginText = (EditText) findViewById(R.id.login);
passwordText= (EditText) findViewById(R.id.password);
btnLogin = (Button) findViewById(R.id.buttonLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = loginText.getText().toString();
String password = passwordText.getText().toString();
if(username.equals("user") && (password.equals("123456")))
{
Toast.makeText(MainActivity.this, "Welcome", Toast.LENGTH_SHORT).show();
Intent intent= new Intent(getApplicationContext(),NewActivity.class);
startActivity(intent);
}else
{
Toast.makeText(MainActivity.this, "Invalid Credentials", Toast.LENGTH_SHORT).show();
}
} });
}}As soon as the values are matched, an activity transition using Intent takes place with a 'welcome' Toast message. Otherwise the login attempt is rejected with a Toast message as 'Invalid Credentials'. Watch the video below:
Watch our youtube channel page for more video tutorials


Comments