How to check which radio button is selected in kotlin?

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>

>Solution :

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

Leave a Reply