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

Practical No. 7

The document describes an XML layout file and Java code for a login screen. The XML defines the user interface with text views, edit texts and a button. The Java code handles button clicks and validates the entered username and password.

Uploaded by

shahidhakim0992
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Practical No. 7

The document describes an XML layout file and Java code for a login screen. The XML defines the user interface with text views, edit texts and a button. The Java code handles button clicks and validates the entered username and password.

Uploaded by

shahidhakim0992
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

XML FILE:

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


<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="40dp"
tools:context=".MainActivity">

<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Username:"
android:layout_alignParentTop="true"
android:textSize="20sp"/>

<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textview1"
android:layout_marginBottom="16dp"
android:hint="Username"
android:textSize="20sp" />

<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Password:"
android:layout_below="@+id/editTextUsername"
android:textSize="20sp"/>

<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textview2"
android:layout_marginBottom="16dp"
android:hint="Password"
android:inputType="textPassword"
android:textSize="20sp" />

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

</RelativeLayout>
JAVA FILE:

package com.example.timepass;
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 editTextUsername, editTextPassword;


Button buttonLogin;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
buttonLogin = findViewById(R.id.buttonLogin);
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username =
editTextUsername.getText().toString().trim();
String password =
editTextPassword.getText().toString().trim();
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter
username and password", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Login successful",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

You might also like