How To Create Static Login Activity In Android ( Android Studio)

Step 1: Open The Software Which is Android Studio.

Step 2: Select The -> +Start a new android studio project.

Step 3: Select The Empty Activity And Click Next,

Select Empty Activity and Click Next

Step 4: Type The Android Application Name as “StaticLogin” and select the Minimum SDK level which is “API 15: ANDROID 4.0.3 (IceCreamSandwich). Click on Finish button.

Design an Activity as shown below.

<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:padding="20dp"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Email:"
        android:id="@+id/tvEmail"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textColor="#020202" />
    <EditText
        android:id="@+id/inputEmail"
        android:layout_below="@+id/tvEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    <TextView
        android:id="@+id/tvPassword"
        android:layout_below="@+id/inputEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Password:"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textColor="#010101" />
    <EditText
        android:id="@+id/inputPassword"
        android:layout_below="@+id/tvPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    <Button
        android:id="@+id/btnLogin"
        android:layout_below="@+id/inputPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

Step 5: Select File -> New -> Activity -> Empty Activity. And specify Name of activity as “SecondAcitvity” and click on Finish Button.
It will create second activity in your Application.

Step 6: Open MainActivity.java file and write down the following code to authenticate the user and open “SecondAcitvity” From MainActivity.

package com.socialcodia.staticlogin; //your app pakage name
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 {
    private EditText inputEmail, inputPassword;
    private Button btnLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inputEmail = findViewById(R.id.inpuEmail);
        inputPassword = findViewById(R.id.inputPassword);
        btnLogin = findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = inputEmail.getText().toString().trim();
                String password = inputPassword.getText().toString().trim();
                if (email.isEmpty())
                {
                    Toast.makeText(getApplicationContext(), "Enter Email Address", Toast.LENGTH_SHORT).show();
                }
                else if (password.isEmpty())
                {
                    Toast.makeText(getApplicationContext(), "Enter Password", Toast.LENGTH_SHORT).show();
                }
                else if (email.equals("admin") && password.equals("admin"))
                {
                    Toast.makeText(getApplicationContext(), "Login Successful", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
                    startActivity(intent);
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Enter Email Address", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

Step 7: Run The Application


Comments

One response to “How To Create Static Login Activity In Android ( Android Studio)”

  1. Excellent Content.

Leave a Reply

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