Download as pdf or txt
Download as pdf or txt
You are on page 1of 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