0% found this document useful (0 votes)
36 views9 pages

Lab 5

The document contains the XML code for a registration form layout in Android. It includes fields for username, password, first name, last name, email, date of birth, gender, address, contact number, and hobbies. The layout uses linear layouts, text views, edit texts, radio buttons, check boxes and a date picker to construct the registration form.

Uploaded by

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

Lab 5

The document contains the XML code for a registration form layout in Android. It includes fields for username, password, first name, last name, email, date of birth, gender, address, contact number, and hobbies. The layout uses linear layouts, text views, edit texts, radio buttons, check boxes and a date picker to construct the registration form.

Uploaded by

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

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

>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#F6F0F0"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F6F0F0"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp"
android:text="Registration Page"
android:background="#F5F1F6"
android:textSize="40dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="USERNAME:"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginLeft="5dp"/>
<EditText
android:id="@+id/userName"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="20dp"
android:inputType="text"
android:hint="Enter Username.."
android:layout_marginLeft="3dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASSWORD:"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginLeft="5dp" />
<EditText
android:id="@+id/passWord"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="20dp"
android:hint="Enter Password:"
android:inputType="textPassword"
android:layout_marginLeft="3dp"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CONFIRM PASSWORD:"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginLeft="5dp" />
<EditText
android:id="@+id/confirmPassword"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="20dp"
android:hint="Confirm Password:"
android:inputType="textPassword"
android:layout_marginLeft="3dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name:"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginLeft="5dp" />
<EditText
android:id="@+id/firstName"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="20dp"
android:hint="Enter First Name:"
android:inputType="text"
android:layout_marginLeft="3dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name:"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginLeft="5dp" />
<EditText
android:id="@+id/lastName"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="20dp"
android:hint="Enter First Name:"
android:inputType="text"
android:layout_marginLeft="3dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email Address:"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginLeft="5dp" />
<EditText
android:id="@+id/eMail"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="20dp"
android:hint="Enter Email Address:"
android:inputType="text"
android:layout_marginLeft="3dp"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Birthdate:"
android:textSize="20dp"
android:textStyle="bold" />

<DatePicker
android:id="@+id/datePickerDOB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp" />
</LinearLayout>

<RadioGroup
android:id="@+id/radioGroupGender"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="35dp"
android:text="Gender"
android:textSize="20dp" />

<RadioButton
android:id="@+id/Male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Male"
android:textSize="20dp" />

<RadioButton
android:id="@+id/Female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Female"
android:textSize="20dp" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADDRESS:"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginLeft="5dp" />
<EditText
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="20dp"
android:hint="Enter Address:"
android:inputType="text"
android:layout_marginLeft="3dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CONTACT NUMBER:"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginLeft="5dp" />
<EditText
android:id="@+id/contactNumber"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="20dp"
android:hint="Enter Contact Number:"
android:inputType="text"
android:layout_marginLeft="3dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hobbies:"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginLeft="5dp" />
<CheckBox
android:id="@+id/checkBoxReading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reading"
android:textSize="18dp" />

<CheckBox
android:id="@+id/checkBoxTraveling"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Traveling"
android:textSize="18dp" />

<CheckBox
android:id="@+id/checkBoxSports"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sports"
android:textSize="18dp" />

<CheckBox
android:id="@+id/checkBoxCooking"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cooking"
android:textSize="18dp" />

<CheckBox
android:id="@+id/checkBoxMusic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Music"
android:textSize="18dp" />

<CheckBox
android:id="@+id/checkBoxGardening"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gardening"
android:textSize="18dp" />

<CheckBox
android:id="@+id/checkBoxPhotography"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Photography"
android:textSize="18dp" />

<CheckBox
android:id="@+id/checkBoxArt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Art"
android:textSize="18dp" />

<CheckBox
android:id="@+id/checkBoxCoding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coding"
android:textSize="18dp" />

<CheckBox
android:id="@+id/checkBoxDancing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dancing"
android:textSize="18dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Security Questions:"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginLeft="5dp" />

<Spinner
android:id="@+id/securityQuestion1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>

<Spinner
android:id="@+id/securityQuestion2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>

<Spinner
android:id="@+id/securityQuestion3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>
</LinearLayout>
<Button
android:id="@+id/registerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBMIT"
android:layout_gravity="center"
android:layout_marginTop="20dp"/>
</LinearLayout>
</ScrollView>

package com.example.labexercise5;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

private EditText userName, password, confirmPassword, firstName, lastName,


email, address, contactNumber;
private Spinner securityQuestion1, securityQuestion2, securityQuestion3;
private DatePicker datePickerDOB;
private RadioGroup radioGroupGender;
private RadioButton radioButtonMale, radioButtonFemale;
private Button registerButton;

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

// Initialize UI components
userName = findViewById(R.id.userName);
password = findViewById(R.id.passWord);
confirmPassword = findViewById(R.id.confirmPassword);
firstName = findViewById(R.id.firstName);
lastName = findViewById(R.id.lastName);
email = findViewById(R.id.eMail);
address = findViewById(R.id.address);
contactNumber = findViewById(R.id.contactNumber);
datePickerDOB = findViewById(R.id.datePickerDOB);
radioGroupGender = findViewById(R.id.radioGroupGender);
radioButtonMale = findViewById(R.id.Male);
radioButtonFemale = findViewById(R.id.Female);
securityQuestion1 = findViewById(R.id.securityQuestion1);
securityQuestion2 = findViewById(R.id.securityQuestion2);
securityQuestion3 = findViewById(R.id.securityQuestion3);
registerButton = findViewById(R.id.registerButton);

// Set up adapters for security questions


ArrayAdapter<String> securityQuestionAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, getSecurityQuestionOptions());

securityQuestionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dro
pdown_item);
securityQuestion1.setAdapter(securityQuestionAdapter);
securityQuestion2.setAdapter(securityQuestionAdapter);
securityQuestion3.setAdapter(securityQuestionAdapter);

// Set up listeners
datePickerDOB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog();
}
});

registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerUser();
}
});
}

private void showDatePickerDialog() {


// Implement date picker dialog logic
// ...
}

private String[] getSecurityQuestionOptions() {


// Return an array of security question options
return new String[]{"Select a security question", "Pet name?", "Mother's
maiden name?", "Favorite color?", "Birthplace?", "..."};
}

private void registerUser() {


// Perform validation
if (!validateFields()) {
return;
}

// Additional validation for security questions


if
(securityQuestion1.getSelectedItem().toString().equals(securityQuestion2.getSelecte
dItem().toString())
||
securityQuestion1.getSelectedItem().toString().equals(securityQuestion3.getSelected
Item().toString())
||
securityQuestion2.getSelectedItem().toString().equals(securityQuestion3.getSelected
Item().toString())) {
showAlert("Please select different security questions.");
return;
}

// If registration is successful, show a welcome dialog


showWelcomeDialog();
}

private boolean validateFields() {


// Implement validation logic for all fields
// ...

return true; // Return false if validation fails


}

private void showWelcomeDialog() {


// Build and show the welcome dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Welcome!");
builder.setMessage("Registration successful!\n\n"
+ "First Name: " + firstName.getText().toString() + "\n"
+ "Last Name: " + lastName.getText().toString());
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("Welcome, " + firstName.getText().toString() + " " +
lastName.getText().toString() + "!");
// Navigate to the welcome activity or perform additional actions
}
});
builder.show();
}

private void showAlert(String message) {


// Build and show an alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Alert");
builder.setMessage(message);
builder.setPositiveButton("OK", null);
builder.show();
}

private void showToast(String message) {


Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}

You might also like