Monday, December 18, 2023

Design android application for login activity. Write android code to check login credentials with username = "mca" and password = "android". Display appropriate toast message to the user

 activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">



    <EditText

        android:id="@+id/usernameEditText"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="50dp"

        android:hint="Username"

        android:inputType="textEmailAddress" />


    <EditText

        android:id="@+id/passwordEditText"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/usernameEditText"

        android:layout_marginTop="16dp"

        android:hint="Password"

        android:inputType="textPassword" />


    <Button

        android:id="@+id/loginButton"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@id/passwordEditText"

        android:layout_marginTop="24dp"

        android:text="Login" />

</RelativeLayout>


activitymain.java

import androidx.appcompat.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

import android.os.Bundle;


public class MainActivity extends AppCompatActivity {

    private EditText usernameEditText, passwordEditText;

    private Button loginButton;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        usernameEditText = findViewById(R.id.usernameEditText);

        passwordEditText = findViewById(R.id.passwordEditText);

        loginButton = findViewById(R.id.loginButton);


        loginButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                performLogin();

            }

        });

    }


    private void performLogin() {

        String enteredUsername = usernameEditText.getText().toString().trim();

        String enteredPassword = passwordEditText.getText().toString().trim();


        // Check login credentials

        if (enteredUsername.equals("mca") && enteredPassword.equals("android")) {

            // Successful login

            showToast("Login successful!");

        } else {

            // Incorrect credentials

            showToast("Incorrect username or password. Please try again.");

        }

    }


    private void showToast(String message) {

        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

    }}


No comments:

Post a Comment