Mobile Application Development Practical 28

Question 1
Write a program to create the login form with necessary validations like length of username and password, empty text fields, count of unsuccessful login attempts. Display the login uccessful /Unsuccessful toastmessage.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="50dp"
        android:orientation="vertical">

        <EditText
            android:id="@+id/uname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter Username" />

        <EditText
            android:id="@+id/pass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPassword"
            android:hint="Enter Password" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="30dp"
            android:text="Login" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical28;
import androidx.appcompat.app.AppCompatActivity;
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 uname, pass;
    Button b;
    int count=3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        uname = findViewById(R.id.uname);
        pass = findViewById(R.id.pass);
        b = findViewById(R.id.button);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username=uname.getText().toString();
                String password=pass.getText().toString();
                if(username.isEmpty()||password.isEmpty()) {
                    Toast.makeText(getApplicationContext(), "Username or password can not be blank", Toast.LENGTH_LONG).show();
                }
                else if (username.startsWith(" ")||password.startsWith(" ")) {
                    Toast.makeText(getApplicationContext(), "Username or password can not start with space", Toast.LENGTH_LONG).show();
                }
                else if(username.length()<5||password.length()<5){
                    Toast.makeText(getApplicationContext(),"Username or password is too short",Toast.LENGTH_LONG).show();
                }
                else if(username.equals("admin")&&password.equals("Admin123"))
                    Toast.makeText(getApplicationContext(),"Login successful",Toast.LENGTH_LONG).show();
                else
                {
                    count--;
                    if(count==0)
                    {
                        Toast.makeText(getApplicationContext(), "You have exceeded maximum number of login attempts", Toast.LENGTH_LONG).show();
                        b.setClickable(false);
                        uname.setEnabled(false);
                        pass.setEnabled(false);
                    }
                    else
                        Toast.makeText(getApplicationContext(),"Invalid login details\nYou have "+count+" attempts left",Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}
Output
Screenshot_20230326_165129
Screenshot_20230326_165146
Screenshot_20230326_165253
Screenshot_20230326_165216

Leave a Comment

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