0% found this document useful (0 votes)
11 views

XML Version

This document contains code for an Android app that allows a user to select a radio button option for their major and displays their selection in a toast message. It includes code for the MainActivity class that handles radio button selection and button clicks. It also includes the XML layout file containing the radio buttons and button. Finally, it includes the strings resource file containing text labels.

Uploaded by

farjrishopee15
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

XML Version

This document contains code for an Android app that allows a user to select a radio button option for their major and displays their selection in a toast message. It includes code for the MainActivity class that handles radio button selection and button clicks. It also includes the XML layout file containing the radio buttons and button. Finally, it includes the strings resource file containing text labels.

Uploaded by

farjrishopee15
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.example.

andika_project3

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.CompoundButton
import android.widget.RadioButton
import android.widget.Toast
import com.example.andika_project3.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity(),


CompoundButton.OnCheckedChangeListener {
private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.ne.setOnCheckedChangeListener(this)
binding.es.setOnCheckedChangeListener(this)
binding.btnPilih.setOnClickListener(View.OnClickListener {
val buttonId: Int =
binding.radioGroupPeminatan.checkedRadioButtonId
if (buttonId == View.NO_ID) {
Toast.makeText(this@MainActivity, "Nothing selected",
Toast.LENGTH_SHORT).show()
} else {
val selectedRadioButton =
findViewById<RadioButton>(buttonId)
Toast.makeText(this@MainActivity,selectedRadioButton.text,
Toast.LENGTH_SHORT).show()
}
})
}
override fun onCheckedChanged(compoundButton: CompoundButton,
isChecked: Boolean) {
// if (compoundButton is RadioButton && isChecked) {
//
Toast.makeText(this@MainActivity,compoundButton.getText().toString() ,Toast
.LENGTH_SHORT).show()
// }
}
fun onRadioButtonClicked(view: View) {}
}

2.

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RadioGroup
android:id="@+id/radioGroupPeminatan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/ne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/radio_NE"/>
<RadioButton
android:id="@+id/es"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/radio_ES"/>
<RadioButton
android:id="@+id/sf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/radio_SF"/>
<RadioButton
android:id="@+id/iot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/radio_IOT"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="30dp"
android:text="@string/pilih"
android:id="@+id/btnPilih"
/>
</LinearLayout>

3
<resources>
<string name="app_name">Andika_Project3</string>
<string name="radio_NE">Network Engineering</string>
<string name="radio_ES">Embedded Systems</string>
<string name="radio_SF">Software</string>
<string name="radio_IOT">IOT</string>
<string name="pilih">Pilih Peminatan</string>
</resources>

You might also like