Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

XML Code:

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


<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:id="@+id/header_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create your Google Account"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/first_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="First Name"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/header_text" />

<EditText
android:id="@+id/last_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Last Name"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/first_name" />

<EditText
android:id="@+id/username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Username"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/last_name" />

<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Password"
android:inputType="textPassword"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/username" />

<EditText
android:id="@+id/confirm_password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Confirm Password"
android:inputType="textPassword"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password" />

<Button
android:id="@+id/register_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Register"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/confirm_password" />

</androidx.constraintlayout.widget.ConstraintLayout>
Java Code:
package com.example.myregistration;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText firstNameEditText, lastNameEditText, usernameEditText,


passwordEditText, confirmPasswordEditText;

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

firstNameEditText = findViewById(R.id.first_name);
lastNameEditText = findViewById(R.id.last_name);
usernameEditText = findViewById(R.id.username);
passwordEditText = findViewById(R.id.password);
confirmPasswordEditText = findViewById(R.id.confirm_password);

Button registerButton = findViewById(R.id.register_button);


registerButton.setOnClickListener(v -> register());
}

private void register() {


String firstName = firstNameEditText.getText().toString();
String lastName = lastNameEditText.getText().toString();
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
String confirmPassword = confirmPasswordEditText.getText().toString();

// Perform validation checks here


if (firstName.isEmpty() || lastName.isEmpty() || username.isEmpty() ||
password.isEmpty() || confirmPassword.isEmpty()) {
Toast.makeText(this, "Please fill in all fields.",
Toast.LENGTH_SHORT).show();
return;
}

if (!password.equals(confirmPassword)) {
Toast.makeText(this, "Passwords do not match.", Toast.LENGTH_SHORT).show();
return;
}

// Registration successful
String welcomeMessage = "Welcome to Google, " + firstName + " " + lastName + "!";
Toast.makeText(this, welcomeMessage, Toast.LENGTH_LONG).show();
}
}

Output:

You might also like