1

I want to disable the clicks on the RadioGroup placed inside the Relative Layout! In a way that my only Relative Layout should be clickable. I want this on some certain condition my XML is as follow:

   <RelativeLayout
            android:id="@+id/shouldNotPlayLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="40dp"
            android:layout_marginLeft="44dp"
            android:layout_marginRight="44dp"
            android:layout_marginTop="40dp">

            <RadioGroup
                android:id="@+id/radio_group"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:saveEnabled="false">

                <RadioButton
                    android:id="@+id/radio_option_a"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/radio_option_selector"
                    android:button="@null"
                    android:padding="15dp"
                    android:textColor="#161743"
                    android:textSize="14sp"
                    android:textStyle="normal"
                    tools:text="First option"/>

                <RadioButton
                    android:id="@+id/radio_option_b"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/radio_option_selector"
                    android:button="@null"
                    android:padding="15dp"
                    android:textColor="#161743"
                    android:textSize="14sp"
                    android:textStyle="normal"
                    tools:text="First option"/>

                <RadioButton
                    android:id="@+id/radio_option_c"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/radio_option_selector"
                    android:button="@null"
                    android:padding="15dp"
                    android:textColor="#161743"
                    android:textSize="14sp"
                    android:textStyle="normal"
                    tools:text="First option"/>
            </RadioGroup>
        </RelativeLayout>

I am stopping the clicks on the radio group as:

        binding.questionLayout.radioOptionA.setEnabled(false);
        binding.questionLayout.radioOptionB.setEnabled(false);
        binding.questionLayout.radioOptionC.setEnabled(false);

        binding.questionLayout.radioOptionA.setFocusable(false);
        binding.questionLayout.radioOptionB.setFocusable(false);
        binding.questionLayout.radioOptionC.setFocusable(false);

        binding.questionLayout.shouldNotPlayLayout.requestFocus();
        binding.questionLayout.shouldNotPlayLayout.setClickable(true);
        binding.questionLayout.shouldNotPlayLayout.setFocusable(true); 

The Problem I am facing is that sometimes any of these three radio button can still be clickable and the user can click any radio button. But I want to disable the click on the radio group/button and get the click done on the relative layout!


Can somebody please figure any blunders done by me?

Onik
  • 19,396
  • 14
  • 68
  • 91
Java Nerd
  • 137
  • 1
  • 4
  • 17

3 Answers3

1

Add binding.questionLayout.radio_group.setEnabled(false)

morenoadan22
  • 234
  • 1
  • 9
1

I have made a demo with your requirement & here's what you need,

private void manageEnableState(boolean isEnable) {
    radioOptionA.setEnabled(isEnable);
    radioOptionB.setEnabled(isEnable);
    radioOptionC.setEnabled(isEnable);

    radioOptionA.setFocusable(isEnable);
    radioOptionB.setFocusable(isEnable);
    radioOptionC.setFocusable(isEnable);

    shouldNotPlayLayout.setClickable(!isEnable);
    shouldNotPlayLayout.setFocusable(!isEnable);
}

Where isEnable is used to set your RadioButton enable state..

buzzingsilently
  • 1,546
  • 3
  • 12
  • 18
0

Please I giving what you have done but then also please try following the way it will help,

private void enableDisbleRadioGroup(RadioGroup groupId, boolean isEnable) {
        for (int i = 0; i < groupId.getChildCount(); i++) {
            groupId.getChildAt(i).setEnabled(isEnable);
        }
    }

In above method pass your radio group id and enable disable mode what you want,

Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39