0% found this document useful (0 votes)
2 views

practical 7.2

The document contains an XML layout file and a Java activity for an Android application that collects student information. The layout includes fields for entering a student's name, age, and course, along with a submit button and a result display area. The Java code handles the button click event to validate input and display the entered information or prompt the user to fill in all fields.

Uploaded by

Pruthesh Upadhye
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)
2 views

practical 7.2

The document contains an XML layout file and a Java activity for an Android application that collects student information. The layout includes fields for entering a student's name, age, and course, along with a submit button and a result display area. The Java code handles the button click event to validate input and display the entered information or prompt the user to fill in all fields.

Uploaded by

Pruthesh Upadhye
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

7.2 activity_main.

xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">

<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Student Information"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center"/>

<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"/>

<EditText
android:id="@+id/etAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Age"
android:inputType="number"/>

<EditText
android:id="@+id/etCourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Course"/>

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

<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp"
android:textStyle="bold"
android:paddingTop="10dp"/>
</LinearLayout>
7.2 MainActivity.java

package com.example.practical7;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText etName, etAge, etCourse;


private Button btnSubmit;
private TextView tvResult;

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

// Initializing views
etName = findViewById(R.id.etName);
etAge = findViewById(R.id.etAge);
etCourse = findViewById(R.id.etCourse);
btnSubmit = findViewById(R.id.btnSubmit);
tvResult = findViewById(R.id.tvResult);

// Set button click listener


btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = etName.getText().toString().trim();
String age = etAge.getText().toString().trim();
String course = etCourse.getText().toString().trim();

// Check if fields are empty


if (name.isEmpty() || age.isEmpty() || course.isEmpty()) {
tvResult.setText("Please enter all details!");
} else {
// Display student information
String result = "Student Information:\nName: " + name +
"\nAge: " + age + "\nCourse: " + course;
tvResult.setText(result);
}
}
});
}
}
7.2 Output

You might also like