- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
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".
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();
}
} });
}}
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>
That's all, run the code...
Comments
Post a Comment