0% found this document useful (0 votes)
16 views7 pages

Slip 3

The document contains XML and Java code for two Android applications. The first application displays a college name and allows users to change its color with a button. The second application collects student details through a form and displays them on a new screen after submission.

Uploaded by

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

Slip 3

The document contains XML and Java code for two Android applications. The first application displays a college name and allows users to change its color with a button. The second application collects student details through a form and displays them on a new screen after submission.

Uploaded by

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

Q1 =================

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">

<!-- College Name (TextView) -->


<TextView
android:id="@+id/collegeName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My College Name"
android:textSize="24sp"
android:textStyle="bold"
android:padding="20dp"
android:textColor="#000000" />

<!-- Push Button to Change Color -->


<Button
android:id="@+id/changeColorButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Color"
android:layout_marginTop="20dp"/>

</LinearLayout>

java----------------------

package com.example.collegeapp; // Change this based on your package name

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

private TextView collegeName;


private Button changeColorButton;
private boolean isColorChanged = false;

@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 -----------------------------

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


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

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

<CheckBox android:id="@+id/sports" android:text="Sports"


android:layout_width="match_parent"
android:layout_height="wrap_content" />
<CheckBox android:id="@+id/reading" android:text="Reading"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<CheckBox android:id="@+id/music" android:text="Music"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<EditText android:id="@+id/marks" android:hint="Enter Marks"


android:inputType="number" android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<Button android:id="@+id/submitBtn" android:text="Submit"


android:layout_width="match_parent" android:layout_height="wrap_content"/>

</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 {

private EditText name, surname, studentClass, marks;


private RadioGroup genderGroup;
private CheckBox sports, reading, music;
private Button submitBtn;

@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);

// Submit Button Click Listener


submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get input values
String studentName = name.getText().toString();
String studentSurname = surname.getText().toString();
String studentClassValue = studentClass.getText().toString();
String studentMarks = marks.getText().toString();

// Get selected gender


int selectedGenderId = genderGroup.getCheckedRadioButtonId();
RadioButton selectedGender = findViewById(selectedGenderId);
String gender = selectedGender != null ?
selectedGender.getText().toString() : "Not Selected";

// Get selected hobbies


String hobbies = "";
if (sports.isChecked()) hobbies += "Sports ";
if (reading.isChecked()) hobbies += "Reading ";
if (music.isChecked()) hobbies += "Music ";

// Pass data to new activity


Intent intent = new Intent(MainActivity.this, Display.class);
intent.putExtra("name", studentName);
intent.putExtra("surname", studentSurname);
intent.putExtra("class", studentClassValue);
intent.putExtra("gender", gender);
intent.putExtra("hobbies", hobbies);
intent.putExtra("marks", studentMarks);
startActivity(intent);
}
});
}
}
activity_display.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: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;

public class Display extends Activity {

private TextView displayName, displaySurname, displayClass, displayGender,


displayHobbies, displayMarks;

@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);

// Receive data from intent


Bundle extras = getIntent().getExtras();
if (extras != null) {
displayName.setText(extras.getString("name"));
displaySurname.setText(extras.getString("surname"));
displayClass.setText(extras.getString("class"));
displayGender.setText(extras.getString("gender"));
displayHobbies.setText(extras.getString("hobbies"));
displayMarks.setText(extras.getString("marks"));
}
}
}

You might also like