Home > Software engineering >  How to check which radio button is selected in kotlin?
How to check which radio button is selected in kotlin?

Time:09-23

I'm trying to make a todo app and I made the radio group as a radio button with priority high, medium and low. However, a different error appeared each time. I am trying to print the priorities in the form of string to the room database according to this clicked radio button, for example, if the user clicked the high priority radio button, it will be written high in the priority column in the room database. How can I do that.

AddNoteFragment

  private var fragmentAddNoteBinding:FragmentAddNoteBinding? = null
    private lateinit var viewModel:AddNoteViewModel

    private val sdf = SimpleDateFormat("yyyy-MM-dd")
    private val currentDate = sdf.format(Date())
   

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ViewModelProvider(requireActivity()).get(AddNoteViewModel::class.java)

        val binding =FragmentAddNoteBinding.bind(view)
        fragmentAddNoteBinding = binding


        binding.AddNoteFragmentBtn.setOnClickListener{

            viewModel.makeNote(
                binding.AddNoteFragmentTxtTitle.text.toString(),
                binding.AddNoteFragmentTxtNote.text.toString(),
                currentDate
                // priority <-- here I want to send priority as string like this


            )
        }


    }

AddNoteViewModel

@HiltViewModel
class AddNoteViewModel @Inject constructor(
    private val repository: INoteRepository,

): ViewModel() {



    private fun insertNote(note: Note) = viewModelScope.launch{
        repository.Insert(note)
    }


    fun makeNote(title:String,note:String,currentDate:String){ //here I will take the priority as a parameter and create a new note and save it to room
        if(title.isEmpty() || note.isEmpty()){
            println("Enter titel,note,priority")
            return
        }


        val note = Note(note,title,currentDate)
        insertNote(note)

    }



}

My radio group xml

    <RadioGroup

        android:id="@ id/AddNoteFragmentRadioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:orientation="horizontal">

        <RadioButton
            android:id="@ id/AddNoteFragmentHighPriorityRadioBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"></RadioButton>

        <RadioButton
            android:id="@ id/AddNoteFragmentMediumPriorityRadioBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"></RadioButton>

        <RadioButton
            android:id="@ id/AddNoteFragmentLowPriorityRadioBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"></RadioButton>
    </RadioGroup>

CodePudding user response:

Use function of radio group view:

getCheckedRadioButtonId()

ex:

  val rbId = binding.AddNoteFragmentRadioGroup.getCheckedRadioButtonId()
when (rbId) {
    R.id.AddNoteFragmentHighPriorityRadioBtn->print("1")
    R.id.AddNoteFragmentMediumPriorityRadioBtn->print("2")
    R.id.AddNoteFragmentLowPriorityRadioBtn->print("3")
}

CodePudding user response:

If you have many of the radio groups and buttons in your app, try out this approach You utilise tag in the xml layout of the radio button

create an extension function for RadioGroup

fun RadioGroup.getCheckedRadioButton(): RadioButton? {
    var checkedRadioButton: RadioButton? = null
    this.children.forEach {
        if((it as RadioButton).isChecked)
            checkedRadioButton = it
    }
    return checkedRadioButton
}

and then you can use that in your code as follow:

val tag = binding.AddNoteFragmentRadioGroup.getCheckedRadioButton()?.tag ?: "no_prio"

your xml will look like this

<RadioGroup
        android:id="@ id/AddNoteFragmentRadioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:orientation="horizontal">

        <RadioButton
            android:id="@ id/AddNoteFragmentHighPriorityRadioBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tag="high"
            android:layout_margin="8dp"></RadioButton>

        <RadioButton
            android:id="@ id/AddNoteFragmentMediumPriorityRadioBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tag="medium"
            android:layout_margin="8dp"></RadioButton>

        <RadioButton
            android:id="@ id/AddNoteFragmentLowPriorityRadioBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tag="low"
            android:layout_margin="8dp"></RadioButton>
    </RadioGroup>

now that in any other layout .. You will be able to easily call the extension method anywhere you need and only change the tag in your xml layout

  • Related