0% found this document useful (0 votes)
3 views1 page

LabPrepttt 2

The document contains an XML layout for an Android application with various UI components including a TextView, CheckBox, RadioGroup, Spinner, and Switch. The MainActivity class initializes these components and sets up an event listener for a button that displays the state of the UI elements in a TextView. The Spinner is populated with three items, and the button click updates the TextView with the current selections and states of the components.
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)
3 views1 page

LabPrepttt 2

The document contains an XML layout for an Android application with various UI components including a TextView, CheckBox, RadioGroup, Spinner, and Switch. The MainActivity class initializes these components and sets up an event listener for a button that displays the state of the UI elements in a TextView. The Spinner is populated with three items, and the button click updates the TextView with the current selections and states of the components.
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/ 1

android:text="Display Selections"

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spinner"
tools:layout_editor_absoluteX="0dp" />

<TextView
android:id="@+id/resultTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/displayButton"
tools:layout_editor_absoluteX="0dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

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

class MainActivity : AppCompatActivity() {

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

val checkBox: CheckBox = findViewById(R.id.checkBox)


val radioGroup: RadioGroup = findViewById(R.id.radiogroup)
val spinner: Spinner = findViewById(R.id.spinner)
val switchToggle: Switch = findViewById(R.id.switch1)
val btn: Button = findViewById(R.id.btn)
val resultTextView: TextView = findViewById(R.id.tv)

val items = arrayOf("item1", "item2", "item3")


val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, items)
spinner.adapter = adapter
btn.setOnClickListener{
val c = if(checkBox.isChecked) "Checked" else "Not Checked"
val RR = radioGroup.checkedRadioButtonId
val R = findViewById<RadioButton>(RR).text.toString()
val sp = spinner.selectedItem.toString()
val sw = if(switchToggle.isChecked) "On" else "Off"

resultTextView.text = " C => $c \nR => $R \nsp => $sp \nsw => $sw"
}

}
}

You might also like