Slip 3
Slip 3
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:gravity="center"
android:padding="20dp">
</LinearLayout>
java----------------------
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Linking UI Elements
collegeName = findViewById(R.id.collegeName);
changeColorButton = findViewById(R.id.changeColorButton);
// Button Click Listener to Change Color
changeColorButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isColorChanged) {
collegeName.setTextColor(Color.BLACK); // Original color
} else {
collegeName.setTextColor(Color.RED); // New color
}
isColorChanged = !isColorChanged; // Toggle color state
}
});
}
}
<TextView
android:id="@+id/collegeName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My College Name"
android:textSize="28sp"
android:textStyle="italic"
android:fontFamily="serif"
android:padding="20dp"
android:textColor="#000000" />
Q2 ===========================
xml -----------------------------
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:text="Enter Student Details"
android:textSize="20sp" android:textStyle="bold"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<EditText
android:id="@+id/name"
android:hint="Enter Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/surname"
android:hint="Enter Surname"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/studentclass"
android:hint="Enter Class"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RadioGroup
android:id="@+id/genderGroup" android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/male" android:text="Male"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/female" android:text="Female"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RadioGroup>
</LinearLayout>
</ScrollView>
main.java --------------------------
package com.example.studentapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Link UI components
name = findViewById(R.id.name);
surname = findViewById(R.id.surname);
studentClass = findViewById(R.id.studentclass);
marks = findViewById(R.id.marks);
genderGroup = findViewById(R.id.genderGroup);
sports = findViewById(R.id.sports);
reading = findViewById(R.id.reading);
music = findViewById(R.id.music);
submitBtn = findViewById(R.id.submitBtn);
<TextView
android:text="Student Details"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:text="Name:"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/displayName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:text="Surname:"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/displaySurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:text="Class:"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/displayClass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:text="Gender:"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/displayGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:text="Hobbies:"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/displayHobbies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:text="Marks:"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/displayMarks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
</LinearLayout>
DisplayActivity.java--------------------------------
package com.example.studentapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
// Link UI elements
displayName = findViewById(R.id.displayName);
displaySurname = findViewById(R.id.displaySurname);
displayClass = findViewById(R.id.displayClass);
displayGender = findViewById(R.id.displayGender);
displayHobbies = findViewById(R.id.displayHobbies);
displayMarks = findViewById(R.id.displayMarks);