0% found this document useful (0 votes)
15 views6 pages

Ex 4

The document outlines the steps to create an Android application with a user input screen that includes fields for User Name, Password, Address, Gender, Age, Date of Birth, and State. It provides detailed instructions for setting up the XML layout and the Java logic to handle user input and display the collected data upon submission. The application also includes a Date Picker for selecting the Date of Birth and a Spinner for selecting the State.

Uploaded by

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

Ex 4

The document outlines the steps to create an Android application with a user input screen that includes fields for User Name, Password, Address, Gender, Age, Date of Birth, and State. It provides detailed instructions for setting up the XML layout and the Java logic to handle user input and display the collected data upon submission. The application also includes a Date Picker for selecting the Date of Birth and a Spinner for selecting the State.

Uploaded by

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

EXPERIMENT NO-4

AIM: Create a screen that has input boxes for User Name, Password, Address, Gender(radio
buttonsfor male and female), Age (numeric), Date of Birth (Date Picket), State (Spinner) and a
Submit button. On clicking the submit button, print all the data below the Submit Button (use
any layout)..
Step 1: Create a New Project

1. Open Android Studio.


2. Create a new project with an Empty Views Activity.

3. Name the project, e.g., UserDetailsApp.


Step 2: XML Layout
1. Open the res/layout/activity_main.xml file.
2. Replace the content with the following code:

activity_main.xml file

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

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

<!-- User Name -->


<EditText
android:id="@+id/editTextUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="User Name"
android:inputType="text"
android:layout_marginBottom="8dp"/>

<!-- Password -->


<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:layout_marginBottom="8dp"/>

<!-- Address -->


<EditText
android:id="@+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address"
android:inputType="text"
android:layout_marginBottom="8dp"/>

<!-- Gender -->


<RadioGroup
android:id="@+id/radioGroupGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"/>

<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"/>
</RadioGroup>

<!-- Age -->


<EditText
android:id="@+id/editTextAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:inputType="number"
android:layout_marginBottom="8dp"/>

<!-- Date of Birth -->


<EditText
android:id="@+id/editTextDob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Date of Birth (click to pick)"
android:focusable="false"
android:layout_marginBottom="8dp"/>

<!-- State -->


<Spinner
android:id="@+id/spinnerState"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"/>

<!-- Submit Button -->


<Button
android:id="@+id/buttonSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"/>

<!-- TextView for displaying result -->


<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text=""
android:textSize="16sp"
android:textColor="@android:color/black"/>
</LinearLayout>
</ScrollView>

Step 3: Add Logic in MainActivity.java

package com.example.userdetailsapp;

import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

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

// References to UI elements
EditText editTextUserName = findViewById(R.id.editTextUserName);
EditText editTextPassword = findViewById(R.id.editTextPassword);
EditText editTextAddress = findViewById(R.id.editTextAddress);
RadioGroup radioGroupGender = findViewById(R.id.radioGroupGender);
EditText editTextAge = findViewById(R.id.editTextAge);
EditText editTextDob = findViewById(R.id.editTextDob);
Spinner spinnerState = findViewById(R.id.spinnerState);
Button buttonSubmit = findViewById(R.id.buttonSubmit);
TextView textViewResult = findViewById(R.id.textViewResult);

// Setup spinner options


String[] states = {"Select State", "California", "India", "New York", "Florida", "Illinois"};
ArrayAdapter<String> adapter = new ArrayAdapter< >(this,
android.R.layout.simple_spinner_dropdown_item, states);
spinnerState.setAdapter(adapter);

// Setup Date Picker for DOB


editTextDob.setOnClickListener(v -> {
Calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePicker = new DatePickerDialog(MainActivity.this, (view,


selectedYear, selectedMonth, selectedDay) -> {
String dob = selectedDay + "/" + (selectedMonth + 1) + "/" + selectedYear;
editTextDob.setText(dob);
}, year, month, day);
datePicker.show();
});

// Handle Submit Button click


buttonSubmit.setOnClickListener(v -> {
String userName = editTextUserName.getText().toString();
String password = editTextPassword.getText().toString();
String address = editTextAddress.getText().toString();
String age = editTextAge.getText().toString();
String dob = editTextDob.getText().toString();
String state = spinnerState.getSelectedItem().toString();

// Get selected gender


int selectedGenderId = radioGroupGender.getCheckedRadioButtonId();
String gender = selectedGenderId == R.id.radioMale ? "Male" : selectedGenderId ==
R.id.radio
Female ? "Female" : "Not Selected";
// Display collected data
String result = "User Details:\n" +
"Name: " + userName + "\n" +
"Password: " + password + "\n" +
"Address: " + address + "\n" +
"Gender: " + gender + "\n" +
"Age: " + age + "\n" +
"Date of Birth: " + dob + "\n" +
"State: " + state;

textViewResult.setText(result);
});
}
}
Step 3: Run and Test

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy