0

I have a radio group with 3 radio buttons. When I clicked radio buttons, animation is changing but oncheckedchangelistener doesn't called. none of logs are printing, so I think it must be the listener but I can't see anything wrong. I followed couple of examples but everything is almost same. Here is my code:


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
      

        viewModel.myTracksShort.observe(viewLifecycleOwner) { state ->
            when (state) {
                is DataState.Success -> {
                    shortList = state.data.items!!
                    if (checkedRadioButton == 0){
                        mostListenedAdapter.setTracksList(shortList)
                    }
                    setScreenToSuccess()
                }
                is DataState.Fail -> {
                }
            }
        }
        viewModel.myTracksMedium.observe(viewLifecycleOwner) { state ->
            when (state) {
                is DataState.Success -> {
                    mediumList = state.data.items!!
                    setScreenToSuccess()
                }
                is DataState.Fail -> {
                }
            }
        }
        viewModel.myTracksLong.observe(viewLifecycleOwner) { state ->
            when (state) {
                is DataState.Success -> {
                    longList = state.data.items!!
                    setScreenToSuccess()
                }
                is DataState.Fail -> {
                }
            }
        }
        binding.rg.setOnCheckedChangeListener {
                group, checkedId ->
            Log.d(TAG, "onViewCreated: $checkedId")
            when (checkedId) {
                R.id.rb1 -> {
                    Log.d("TAG", "onViewCreated: short")
                    if (this::shortList.isInitialized) {
                        mostListenedAdapter.setTracksList(shortList)
                        checkedRadioButton = 0
                    }
                }
                R.id.rb2  -> {
                    if (this::mediumList.isInitialized) {
                        Log.d("TAG", "onViewCreated: m")
                        mostListenedAdapter.setTracksList(mediumList)
                        checkedRadioButton = 1
                    }
                }
                R.id.rb3 -> {
                    if (this::longList.isInitialized) {
                        Log.d("TAG", "onViewCreated: l")
                        mostListenedAdapter.setTracksList(longList)
                        checkedRadioButton = 2
                    }
                }
                else -> Log.d(TAG, "onViewCreated: else")
            }
        }
   

And my XML file:

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:id="@+id/rg"
            android:weightSum="9"
            android:checkedButton="@id/rb1"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:background="@color/colorPrimary">
            <RadioButton
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:id="@+id/rb1"
                android:layout_weight="3"
                android:text="@string/short_term"
                android:gravity="center"
                android:background="@drawable/radio_flat_selector"
                android:button="@android:color/transparent"
                android:textColor="@color/radio_flat_text_selector"/>
            <RadioButton
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:id="@+id/rb2"
                android:gravity="center"
                android:layout_weight="3"
                android:text="@string/mid_term"
                android:background="@drawable/radio_flat_selector"
                android:button="@android:color/transparent"
                android:textColor="@color/radio_flat_text_selector"/>
            <RadioButton
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:id="@+id/rb3"
                android:gravity="center"
                android:layout_weight="3"
                android:text="@string/long_term"
                android:background="@drawable/radio_flat_selector"
                android:button="@android:color/transparent"
                android:textColor="@color/radio_flat_text_selector"/>
        </RadioGroup>
Jahnavi Paliwal
  • 1,721
  • 1
  • 12
  • 20
besodev
  • 26
  • 5

1 Answers1

0

for anyone who counters the same error: I renamed my Radio Group's id as radioGroup and it solved.

besodev
  • 26
  • 5