0% found this document useful (0 votes)
4 views

Bhendii Chapati

The document contains Java code for an Android application that implements a simple login functionality with predefined credentials. It includes the MainActivity class that handles user input for username and password, and displays a toast message based on the login success or failure. Additionally, it provides XML layout files for the login interface and another activity, defining the user interface elements such as EditTexts and Buttons.

Uploaded by

jagtaptanay80
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Bhendii Chapati

The document contains Java code for an Android application that implements a simple login functionality with predefined credentials. It includes the MainActivity class that handles user input for username and password, and displays a toast message based on the login success or failure. Additionally, it provides XML layout files for the login interface and another activity, defining the user interface elements such as EditTexts and Buttons.

Uploaded by

jagtaptanay80
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical No.

27

Java File :

package com.example.pract;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText usernameInput, passwordInput;


Button loginButton;

// Predefined credentials for demonstration purposes


String correctUsername = "tanay";
String correctPassword = "uiaiuiai";

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

// Initialize the input fields and button


usernameInput = findViewById(R.id.username);
passwordInput = findViewById(R.id.password);
loginButton = findViewById(R.id.login_button);

// Set OnClickListener for the login button


loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the username and password input
String enteredUsername = usernameInput.getText().toString();
String enteredPassword = passwordInput.getText().toString();

// Check if the entered username and password are correct


if (enteredUsername.equals(correctUsername) &&
enteredPassword.equals(correctPassword)) {
// Login successful
Toast.makeText(MainActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
} else {
// Login unsuccessful
Toast.makeText(MainActivity.this, "Login Unsuccessful", Toast.LENGTH_SHORT).show();
}
}
});
}
}

main_activity.xml File :

<?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">

<EditText
android:id="@+id/nameInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="Enter Name"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/ageInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="Enter Age"
android:inputType="number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nameInput" />
<Button
android:id="@+id/insertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Insert Data"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ageInput" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity_login.xml :

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Username"
android:inputType="text"
android:padding="10dp" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:inputType="textPassword"
android:padding="10dp"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="20dp"/>

</LinearLayout>
Output :

You might also like