TY Exp. No: 9

Practical No. 9

Title

Design and Implementation of Registration and Feedback Form using LinearLayout and ConstraintLayout with Custom Toast in Android


Aim

To design and develop an Android application using LinearLayout and ConstraintLayout to create a Registration and Feedback Form, collect user input, and display reusable and styled user feedback using a Custom Toast message, implemented using Java.


Practical Significance

This practical introduces reusable and styled user feedback using Custom Toasts and organizes UI elements using LinearLayout and ConstraintLayout, collects user registration details and feedback, and displays acknowledgment using a custom Toast message.


Industry / Employer Expected Outcomes

  • Implementation of user feedback through custom alert mechanisms

  • Ability to design user-centric forms

  • Understanding of structured UI layout management


Course Level Learning Outcome

CO3: Develop Android applications using UI components and layouts.


Laboratory Learning Outcome

LLO 9.1: Build Android applications using LinearLayout and ConstraintLayout.


Relevant Affective Domain Outcome

Encourages students to think from the user’s perspective while designing forms and feedback systems.


Relevant Theoretical Background

Layouts Used

  • LinearLayout: Arranges UI components vertically or horizontally.

  • ConstraintLayout: Allows flexible positioning of UI elements using constraints.

UI Controls Used

  • TextView

  • EditText

  • RadioButton

  • RadioGroup

  • Button

  • Toast

Custom Toast

A Custom Toast uses a separate XML layout to provide styled and reusable feedback messages.


Software Requirements

  • Android Studio

  • Android SDK

  • Java Development Kit (JDK)


Project Directory Structure

app/
 └── src/
     └── main/
         ├── java/com/example/registrationfeedback/
         │    └── MainActivity.java
         ├── res/
         │    ├── layout/
         │    │    ├── activity_main.xml
         │    │    └── custom_toast.xml
         │    └── values/
         │         └── strings.xml

Program Code


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Sanjay Ghodawat Polytechnic"
        android:textStyle="bold"
        android:textSize="18sp"
        android:gravity="center"
        android:paddingBottom="8dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Registration & Feedback Form"
        android:textStyle="bold"
        android:textSize="20sp"
        android:gravity="center"
        android:paddingBottom="16dp" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/etName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="Enter Name"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

        <EditText
            android:id="@+id/etEmail"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="Enter Email"
            android:inputType="textEmailAddress"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@id/etName"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

        <RadioGroup
            android:id="@+id/radioGroupGender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="12dp"
            app:layout_constraintTop_toBottomOf="@id/etEmail"
            app:layout_constraintStart_toStartOf="parent">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Male" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female" />
        </RadioGroup>

        <EditText
            android:id="@+id/etFeedback"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="Enter your feedback"
            android:inputType="textMultiLine"
            android:minLines="3"
            android:gravity="top|start"
            android:layout_marginTop="12dp"
            app:layout_constraintTop_toBottomOf="@id/radioGroupGender"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

        <Button
            android:id="@+id/btnSubmit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Submit"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toBottomOf="@id/etFeedback"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</LinearLayout>

custom_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="12dp"
    android:background="@android:color/holo_green_dark">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sanjay Ghodawat Polytechnic"
        android:textStyle="bold"
        android:textSize="14sp"
        android:textColor="@android:color/white"
        android:paddingBottom="6dp" />

    <TextView
        android:id="@+id/txtToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Thank you for your feedback"
        android:textStyle="bold"
        android:textSize="16sp"
        android:textColor="@android:color/white" />

</LinearLayout>

MainActivity.java

package com.example.registrationfeedback;

import android.os.Bundle;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    EditText etName, etEmail, etFeedback;
    RadioGroup radioGroupGender;
    Button btnSubmit;

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

        etName = findViewById(R.id.etName);
        etEmail = findViewById(R.id.etEmail);
        etFeedback = findViewById(R.id.etFeedback);
        radioGroupGender = findViewById(R.id.radioGroupGender);
        btnSubmit = findViewById(R.id.btnSubmit);

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                validateAndShowToast();
            }
        });
    }

    private void validateAndShowToast() {

        String name = etName.getText().toString().trim();
        String email = etEmail.getText().toString().trim();
        String feedback = etFeedback.getText().toString().trim();
        int genderId = radioGroupGender.getCheckedRadioButtonId();

        if (name.isEmpty()) {
            etName.setError("Name required");
            return;
        }

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

        if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            etEmail.setError("Enter valid email");
            return;
        }

        if (genderId == -1) {
            Toast.makeText(this, "Please select gender", Toast.LENGTH_SHORT).show();
            return;
        }

        if (feedback.isEmpty()) {
            etFeedback.setError("Feedback required");
            return;
        }

        showCustomToast();
    }

    private void showCustomToast() {

        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.custom_toast, null);

        Toast toast = new Toast(this);
        toast.setView(view);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }
}

Result

The application successfully collects registration details and user feedback, validates input, and displays acknowledgment using a Custom Toast message.


Conclusion

Thus, the Android application using LinearLayout, ConstraintLayout, and Custom Toast has been successfully implemented using Java, fulfilling all specified learning outcomes and practical objectives.



Popular posts from this blog