You are on page 1of 4

Experiment 04 – Login Window

Aim: To write a program to design login window using UI controls in Android Studio.

Theory:

● Widgets in Android:

There are a lot of android widgets such as Button, EditText, AutoCompleteTextView,


ToggleButton, DatePicker, TimePicker, ProgressBar etc.

● Android Button

This is used for event handling when clicked.

● Android Toast

Displays information for the short duration of time.

● Custom Toast

We are able to customize the toast, such as we can display image on the toast

● ToggleButton

It has two states ON/OFF.

● CheckBox

This is a type of two state button either checked or unchecked. There can be a lot of usage of
checkboxes. For example, it can be used to know the hobby of the user, activate/deactivate the
specific action etc.

● AlertDialog

AlertDialog displays an alert dialog containing the message with OK and Cancel buttons.

● Spinner

Spinner displays the multiple options, but only one can be selected at a time.

● AutoCompleteTextView

Android AutoCompleteTextView completes the word based on the reserved words, so no need to
write all the characters of the word. Android AutoCompleteTextView is an editable text field, it
displays a list of suggestions in a drop down menu from which user can select only one
suggestion or value.

● RatingBar

RatingBar displays the rating bar.

● DatePicker
Datepicker displays the datepicker dialog that can be used to pick the date.

● TimePicker

TimePicker displays the timepicker dialog that can be used to pick the time.

● ProgressBar

ProgressBar displays progress task.

Implementation:
package com.example.loginscreenexample;

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 {

private EditText editTextUsername, editTextPassword;


private Button buttonLogin;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize UI elements
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
buttonLogin = findViewById(R.id.buttonLogin);

// Set a click listener for the login button


buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Retrieve entered username and password
String username = editTextUsername.getText().toString();
String password = editTextPassword.getText().toString();

// Implement authentication logic here


if (username.equals("Admin") && password.equals("123")) {
// Successful login
Toast.makeText(MainActivity.this, "Login successful", Toast.LENGTH_SHORT).show();
} else {
// Failed login
Toast.makeText(MainActivity.this, "Invalid username or password",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

<?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:gravity="center_vertical"
android:orientation="vertical"
android:padding="16dp">

<!-- Username EditText -->

<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="Username"
android:inputType="text" />

<!-- Password EditText -->


<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="Password"
android:inputType="textPassword" />

<!-- Login Button -->


<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />

</LinearLayout>
Output:

Conclusion: We were able to implement login window using UI controls in Android Studio

For Faculty Use


Correction Formative Timely
Parameters Assessment Attendance /
[40%] completion of
Learning
Practical [ 40%]
Attitude
[20%]
Marks
Obtained

You might also like