0% found this document useful (0 votes)
11 views5 pages

Practical No 4 A

The document provides code examples for three practical Android applications. The first program displays 'Hello World', the second shows a student's name and marks, and the third collects and displays a user's name, age, and mobile number. Each example includes the corresponding XML layout and Java code for the main activity.
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)
11 views5 pages

Practical No 4 A

The document provides code examples for three practical Android applications. The first program displays 'Hello World', the second shows a student's name and marks, and the third collects and displays a user's name, age, and mobile number. Each example includes the corresponding XML layout and Java code for the main activity.
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/ 5

Practical No 4:-

Q Write A Program to display Hello World


Activity_main.xml File
<?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:gravity="center">
<TextView
android:id="@+id/helloTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textSize="24sp"
android:textStyle="bold"/>
</LinearLayout>

MainActivity.java File
package com.example.hello;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView helloTextView = findViewById(R.id.helloTextView);
helloTextView.setText("Hello World!");
}
}
OUTPUT
Q Write A Program To Display Student Name And Marks
Activity_main.xml File
<?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:gravity="center"
android:padding="20dp">
<TextView
android:id="@+id/studentDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Details Will Appear Here"
android:textSize="20sp"
android:textStyle="bold"
android:padding="10dp"/>
</LinearLayout>

MainActivity.java File
package com.example.hello;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView studentDetails = findViewById(R.id.studentDetails);
String studentName = "Aakanksha Bhopatrao";
int marks = 84;
String displayText = "Student Name: " + studentName + "\nMarks: " + marks;
studentDetails.setText(displayText);
}
}
OUTPUT
Practical No 5:-
Q Write A Program to Place Name,Age,Mobile Number Linearly on the
Display Screen Using Linear Layout
Activity_main.xml File
<?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="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:textSize="18sp" />
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age:"
android:textSize="18sp" />
<EditText
android:id="@+id/editTextAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Age"
android:inputType="number" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile Number:"
android:textSize="18sp" />
<EditText
android:id="@+id/editTextMobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Mobile Number"
android:inputType="phone" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_marginTop="16dp" />
</LinearLayout>
MainActivity.java File
package com.example.hello;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextName, editTextAge, editTextMobile;
private Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName = findViewById(R.id.editTextName);
editTextAge = findViewById(R.id.editTextAge);
editTextMobile = findViewById(R.id.editTextMobile);
btnSubmit = findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editTextName.getText().toString();
String age = editTextAge.getText().toString();
String mobile = editTextMobile.getText().toString();
if (name.isEmpty() || age.isEmpty() || mobile.isEmpty()) {
Toast.makeText(MainActivity.this, "Please fill all fields", Toast.LENGTH_SHORT).show();
} else {
String message = "Name: " + name + "\nAge: " + age + "\nMobile: " + mobile;
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
}
});
}
}
OUTPUT

You might also like