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

Practical 10

The document describes a program to implement a login window using UI controls in Android. It includes the layout code activity_main.xml which defines the structure and components of the login form including text fields for email and password and a login button. It also includes the Java code for the MainActivity class which finds the UI components, adds an onclick listener to the login button to validate the credentials and display a toast message on login.

Uploaded by

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

Practical 10

The document describes a program to implement a login window using UI controls in Android. It includes the layout code activity_main.xml which defines the structure and components of the login form including text fields for email and password and a login button. It also includes the Java code for the MainActivity class which finds the UI components, adds an onclick listener to the login button to validate the credentials and display a toast message on login.

Uploaded by

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

PRACTICAL 10

PROGRAM TO IMPLEMENT LOGIN WINDOW USING UI CONTROLS

The layout of the MainActivity is defined as follows. activity_main.xml

<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login Form"
android:textSize="24sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="24dp"/>

<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
android:layout_marginBottom="16dp"/>

<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:layout_marginBottom="16dp"/>

<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginBottom="16dp"/>

</LinearLayout>

The MainActivity.java is defined below.

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 etEmail, etPassword;


Button btnLogin;

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

// Get the email, password, and login button views


etEmail = findViewById(R.id.etEmail);
etPassword = findViewById(R.id.etPassword);
btnLogin = findViewById(R.id.btnLogin);

// Set a click listener for the login button


btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = etEmail.getText().toString().trim();
String password = etPassword.getText().toString().trim();

if (email.isEmpty()) {
etEmail.setError("Email is required!");
etEmail.requestFocus();
return;
}

if (password.isEmpty()) {
etPassword.setError("Password is required!");
etPassword.requestFocus();
return;
}

// Replace this with your own login logic


// For example, you can check the email and password against a database or API
// If the credentials are valid, you can start a new activity or show a success message
// Otherwise, you can show an error message
Toast.makeText(getApplicationContext(), "Email: " + email + ", Password: " + password,
Toast.LENGTH_SHORT).show();
}
});
}
}

You might also like