0% found this document useful (0 votes)
19 views3 pages

Practical No 7

The document contains an XML layout file and Java code for an Android application that collects personal information from users. The XML file defines the user interface which includes text views and edit text fields for entering a name, date of birth, city, email, and contact number. The Java code finds the views, adds an onClick listener to the submit button to get the entered values and display them in a Toast message or show an error if any fields are empty.

Uploaded by

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

Practical No 7

The document contains an XML layout file and Java code for an Android application that collects personal information from users. The XML file defines the user interface which includes text views and edit text fields for entering a name, date of birth, city, email, and contact number. The Java code finds the views, adds an onClick listener to the submit button to get the entered values and display them in a Toast message or show an error if any fields are empty.

Uploaded by

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

Practical No 7

Xml File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="10dp"
android:gravity="center">

<TextView
android:id="@+id/tvInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Personal Information of Student"
android:textSize="20sp"
android:gravity="center_horizontal"
android:textStyle="bold"
android:layout_marginTop="20dp"

android:textColor="@android:color/holo_red_light"/>
<EditText android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter you name"
android:ems="10"
android:inputType="textPersonName"
android:textSize="18sp"
android:layout_marginTop="50dp"
android:layout_below="@+id/tvInfo"/>
<EditText android:id="@+id/dob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Date of Birth"
android:ems="10"
android:inputType="date"
android:textSize="18sp"
android:layout_below="@+id/name"
android:layout_marginTop="25dp"/>
<EditText android:id="@+id/city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your City"
android:ems="10"
android:inputType="textCapCharacters"
android:textSize="18sp"
android:layout_below="@+id/dob"
android:layout_marginTop="25dp"/>
<EditText android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Email ID"
android:ems="10"
android:inputType="textEmailAddress"
android:textSize="18sp"
android:layout_below="@+id/city"
android:layout_marginTop="25dp"/>
<EditText android:id="@+id/contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Contact number"
android:ems="10"
android:inputType="date"
android:textSize="18sp"
android:layout_below="@+id/email"
android:layout_marginTop="25dp"/>
<Button android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/contact"
android:layout_marginTop="50dp"
android:text="Submit"
android:textSize="18sp"
android:onClick="displayData"
tools:ignore="OnClick" />
</RelativeLayout>

Java File
package com.example.pract7;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText name, dob, city, email, contact;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
dob = findViewById(R.id.dob);
city = findViewById(R.id.city);
email = findViewById(R.id.email);
contact = findViewById(R.id.contact);
submit = findViewById(R.id.btnSubmit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n = name.getText().toString();
String d = dob.getText().toString();
String ci = city.getText().toString();
String e = email.getText().toString();
String c = contact.getText().toString();
if(n.isEmpty() || d.isEmpty() || ci.isEmpty() || e.isEmpty() ||
c.isEmpty())
{

Toast.makeText(getApplicationContext(),
"Please enter all data",
Toast.LENGTH_SHORT).show();
}
else
{

Toast.makeText(getApplicationContext(), n +
"\n" + d + "\n" + ci + "\n" + e + "\n" + c,
Toast.LENGTH_SHORT).show();
}
}
});
}
}

Output

You might also like