Practical No 10
Q1. Write a program to create a login form for a social networking site.
Xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/username_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:layout_margin="16dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/username_input">
<EditText
android:id="@+id/password_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/toggle_visibility"
android:layout_marginEnd="16dp" />
<Button
android:id="@+id/toggle_visibility"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<Button
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_centerInParent="true" />
</RelativeLayout>
Java file
package com.example.madprac10;
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;
import com.example.madprac10.R; // Use your app's R class
public class MainActivity extends AppCompatActivity {
private EditText usernameInput, passwordInput;
private Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize UI elements
usernameInput = findViewById(R.id.username_input);
passwordInput = findViewById(R.id.password_input);
loginButton = findViewById(R.id.login_button);
// Set up click listener for the login button
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameInput.getText().toString().trim();
String password = passwordInput.getText().toString().trim();
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(MainActivity.this, "Please fill in all fields",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Welcome, " + username,
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Output:
Q2. Write a program to create a login form for student registration system.
Xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="#FFFFFF">
<!-- Title -->
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Registration Login"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp" />
<!-- Username Label -->
<TextView
android:id="@+id/username_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username:"
android:textSize="16sp"
android:layout_marginTop="80dp"
android:layout_alignParentStart="true" />
<!-- Username Input -->
<EditText
android:id="@+id/username_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/username_label"
android:layout_marginTop="10dp"
android:hint="Enter your username"
android:inputType="text" />
<!-- Password Label -->
<TextView
android:id="@+id/password_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
android:textSize="16sp"
android:layout_below="@id/username_input"
android:layout_marginTop="20dp"
android:layout_alignParentStart="true" />
<!-- Password Input -->
<EditText
android:id="@+id/password_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/password_label"
android:layout_marginTop="10dp"
android:hint="Enter your password"
android:inputType="textPassword" />
<!-- Login Button -->
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textColor="#FFFFFF"
android:background="#3F51B5"
android:layout_below="@id/password_input"
android:layout_marginTop="30dp" />
</RelativeLayout>
Java file
package com.example.madprac10;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.madprac10.R;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText usernameInput, passwordInput;
private Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize UI components
usernameInput = findViewById(R.id.username_input);
passwordInput = findViewById(R.id.password_input);
loginButton = findViewById(R.id.login_button);
// Set click listener for the login button
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get input values
String username = usernameInput.getText().toString().trim();
String password = passwordInput.getText().toString().trim();
// Validate inputs
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(MainActivity.this, "Please fill in all fields",
Toast.LENGTH_SHORT).show();
} else if (username.equals("Pratik") && password.equals("@pratikkk")) {
// Updated validation with username and password
Toast.makeText(MainActivity.this, "Login Successful",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Invalid Username or Password",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Output: