Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Practical 2b

The document contains an XML layout for an Android application with interactive UI elements including a checkbox, radio buttons, a button, and a spinner. The accompanying Kotlin code in the MainActivity class handles user interactions, updating text views based on the user's selections and actions. It demonstrates basic event handling for each UI component, providing feedback to the user on their choices.
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)
7 views3 pages

Practical 2b

The document contains an XML layout for an Android application with interactive UI elements including a checkbox, radio buttons, a button, and a spinner. The accompanying Kotlin code in the MainActivity class handles user interactions, updating text views based on the user's selections and actions. It demonstrates basic event handling for each UI component, providing feedback to the user on their choices.
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

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

<!-- Checkbox -->


<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Me" />

<TextView
android:id="@+id/checkboxResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Checkbox Status" />

<!-- Radio Buttons -->


<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dp">

<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />

<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
</RadioGroup>

<TextView
android:id="@+id/radioResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Selected Option" />

<!-- Button -->


<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_marginTop="16dp" />

<TextView
android:id="@+id/buttonResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Button Status" />

<!-- Spinner -->


<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp" />

<TextView
android:id="@+id/spinnerResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Selected Spinner Item" />

</LinearLayout>

package com.example.eventinteractiondemo

import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Checkbox interaction
val checkbox = findViewById<CheckBox>(R.id.checkbox)
val checkboxResult = findViewById<TextView>(R.id.checkboxResult)
checkbox.setOnCheckedChangeListener { _, isChecked ->
checkboxResult.text = if (isChecked) "Checkbox is Checked" else "Checkbox is
Unchecked"
}

// RadioButton interaction
val radioGroup = findViewById<RadioGroup>(R.id.radioGroup)
val radioResult = findViewById<TextView>(R.id.radioResult)
radioGroup.setOnCheckedChangeListener { _, checkedId ->
val radioButton = findViewById<RadioButton>(checkedId)
radioResult.text = "Selected: ${radioButton.text}"
}

// Button interaction
val button = findViewById<Button>(R.id.button)
val buttonResult = findViewById<TextView>(R.id.buttonResult)
button.setOnClickListener {
buttonResult.text = "Button Clicked!"
}

// Spinner interaction
val spinner = findViewById<Spinner>(R.id.spinner)
val spinnerResult = findViewById<TextView>(R.id.spinnerResult)
val items = arrayOf("Option 1", "Option 2", "Option 3", "Option 4")
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, items)
spinner.adapter = adapter
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id:
Long) {
spinnerResult.text = "Selected: ${items[position]}"
}

override fun onNothingSelected(parent: AdapterView<*>?) {


spinnerResult.text = "No selection made"
}
}
}
}

You might also like