Reputation: 35
I'm currently working on my college project. In the app, I have an activity with a fragment. The fragment contains a layout which has nested linear layouts with multiple customized radio buttons in it. I can't put one single radio group to enable the single selection for the buttons because of the linear layouts making it to not work as intended.
So what I did is I made a radio group for the 5 sections and for me to able to select only one item from all the buttons, I tried to use the clearCheck() to clear the selection on other radio groups programmatically.
fragment_parking_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ParkingLayoutFragment"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="275dp"
android:layout_height="100dp"
android:background="@color/colorPrimary"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FACILITY"
android:textColor="#fff"/>
</LinearLayout>
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Entrance/Exit"
android:textSize="12dp"
android:textColor="#31a81c"
android:textAlignment="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginEnd="50dp"
android:orientation="horizontal">
<RadioGroup
android:id="@+id/radio_group_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:background="@color/colorPrimary">
<RadioButton
android:id="@+id/rbtn_slot_1"
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"
android:layout_marginTop="5dp"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
</RadioGroup>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="60dp"
android:layout_height="40dp"
android:gravity="center_horizontal"
android:layout_marginStart="50dp"
android:background="@color/colorPrimary">
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="40dp"
android:orientation="vertical"
android:gravity="bottom">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/ic_stairs"
android:layout_margin="5dp"
/>
<LinearLayout
android:layout_width="40dp"
android:layout_height="5dp"
android:gravity="bottom"
android:background="@color/colorPrimary"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="40dp"
android:layout_marginTop="30dp"
android:padding="5dp"
android:background="@color/colorPrimary">
<RadioGroup
android:id="@+id/radio_group_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginEnd="8dp">
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
</RadioGroup>
<RadioGroup
android:id="@+id/radio_group_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioGroup
android:id="@+id/radio_group_5"
android:layout_width="75dp"
android:layout_height="80dp"
android:gravity="end"
android:orientation="horizontal">
<RadioButton
android:layout_width="15dp"
android:layout_height="40dp"
android:background="@drawable/custom_radio_pwd"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="15dp"
android:layout_height="40dp"
android:background="@drawable/custom_radio_pwd"
android:button="@android:color/transparent"/>
<LinearLayout
android:layout_width="15dp"
android:layout_height="80dp"
android:background="@color/colorPrimary"/>
</RadioGroup>
<RadioGroup
android:id="@+id/radio_group_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_gravity="end"
android:orientation="vertical"
android:background="@color/colorPrimary"
android:gravity="center"
android:padding="5dp">
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:layout_marginTop="3dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
<RadioButton
android:layout_width="40dp"
android:layout_height="13dp"
android:background="@drawable/custom_radio"
android:button="@android:color/transparent"/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
Here is a part of the code in the fragment
public class ParkingLayoutFragment extends Fragment {
RadioGroup radio_group_1, radio_group_2;
public ParkingLayoutFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_parking_layout, container, false);
radio_group_1 = view.findViewById(R.id.radio_group_1);
radio_group_2 = view.findViewById(R.id.radio_group_2);
radio_group_1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
radio_group_2.clearCheck();
}
});
radio_group_2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
radio_group_1.clearCheck();
}
});
// Inflate the layout for this fragment
return view;
}
}
As you can see I have set an onCheckedChangeListener to the 2 radio group so when I select an item on the first radio group, the second radio group will be cleared out and vice versa. But when I run the app and tried to select an item the app just freezes and restarts to the main activity.
Here is a part of the error in the logcat which looks like a loop
at com.grawlix.parkingally.ParkingLayoutFragment$1.onCheckedChanged(ParkingLayoutFragment.java:39)
at android.widget.RadioGroup.setCheckedId(RadioGroup.java:173)
at android.widget.RadioGroup.check(RadioGroup.java:167)
at android.widget.RadioGroup.clearCheck(RadioGroup.java:209)
at com.grawlix.parkingally.ParkingLayoutFragment$2.onCheckedChanged(ParkingLayoutFragment.java:46)
at android.widget.RadioGroup.setCheckedId(RadioGroup.java:173)
at android.widget.RadioGroup.check(RadioGroup.java:167)
at android.widget.RadioGroup.clearCheck(RadioGroup.java:209)
at com.grawlix.parkingally.ParkingLayoutFragment$1.onCheckedChanged(ParkingLayoutFragment.java:39)
at android.widget.RadioGroup.setCheckedId(RadioGroup.java:173)
at android.widget.RadioGroup.check(RadioGroup.java:167)
at android.widget.RadioGroup.clearCheck(RadioGroup.java:209)
at com.grawlix.parkingally.ParkingLayoutFragment$2.onCheckedChanged(ParkingLayoutFragment.java:46)
at android.widget.RadioGroup.setCheckedId(RadioGroup.java:173)
at android.widget.RadioGroup.check(RadioGroup.java:167)
at android.widget.RadioGroup.clearCheck(RadioGroup.java:209)
read: unexpected EOF!
Hope anyone knows what's up with this. Any idea is appreciated and sorry for my English.
Upvotes: 0
Views: 205
Reputation: 8853
Try below code, rg
is your first radio group and rg1
is second radio group, You will be currently facing OOM error with your code. clearChecks()
will trigger setOnCheckedChangeListener()
you need to check if view returned is null or not.
rg = findViewById(R.id.rg);
rg1 = findViewById(R.id.rg1);
rg.setOnCheckedChangeListener((radioGroup, i) -> {
RadioButton rb = findViewById(i);
if (rb != null && rb.isChecked()) {
rg1.clearCheck();
}
});
rg1.setOnCheckedChangeListener((radioGroup, i) -> {
RadioButton rb = findViewById(i);
if (rb != null && rb.isChecked()) {
rg.clearCheck();
}
});
Upvotes: 1