[JAVA] I want to place RadioButtons in the same RadioGroup at any position on the screen.

Overview

RadioButton and RadioGroup provided by Android's standard API.

It's good so far that if you nest multiple RadioButtons in oneRadioGroup, only one of them can be selected at a time, but you can arrange the RadioButton freely. Unacceptable.

Why can't I move the RadioButton freely?

As you can see in the documentation, ʻandroid.widget.RadioGroup inherits ** ʻandroid.widget.LinearLayout **.

Everyone knows what LinearLayout is. In other words, it can only be arranged in one horizontal row or one vertical row.

why! !!

solution

So I made it. Such a class.

FreeRadioGroup.java



import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class FreeRadioGroup {

    private List<RadioButton> registeredRadioButtons = new ArrayList<>();

    public FreeRadioGroup(){

    }

    public void addChild(RadioButton rb){
        if(registeredRadioButtons.contains(rb)){
            Log.w("FreeRadioGroup", "RadioButton already registered. Abort registering");
        }else {
            registeredRadioButtons.add(rb);
            rb.setOnCheckedChangeListener(OCL);
        }
    }

    public void removeChild(RadioButton rb){
        if(!registeredRadioButtons.contains(rb)){
            Log.w("FreeRadioGroup", "RadioButton is not registered. Abort unregistering");
        }else {
            registeredRadioButtons.remove(rb);
            rb.setOnClickListener(null);
        }
    }

    public int getSelectedButtonIndex(){
        for(int i=0,n=registeredRadioButtons.size();i<n;i++){
            if(registeredRadioButtons.get(i).isChecked()){
                return i;
            }
        }
        return -1;
    }

    @Nullable
    public RadioButton getSelectedButton(){
        for(int i=0,n=registeredRadioButtons.size();i<n;i++){
            RadioButton r = registeredRadioButtons.get(i);
            if(r.isChecked()){
                return r;
            }
        }
        return null;
    }

    private final CompoundButton.OnCheckedChangeListener OCL = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(registeredRadioButtons.contains((RadioButton)compoundButton) && b){
                for(RadioButton r : registeredRadioButtons){
                    if(r.equals((RadioButton)compoundButton)){
                        continue;
                    }
                    r.setChecked(false);
                }
            }

        }
    };

}

For the time being, you can copy it in a circle and use it.

When you register a RadioButton that you want to register with ʻaddChild (RadioButton rb)`, only one of the registered ones is selected at the same time.

On the contrary, I think it is necessary at a level where I wonder if this is not officially implemented.

current problem

-If you try to register another listener that is called when the button status changes, it will be overwritten and cannot be used.

I'm not in trouble with this for what I use so far, so I'll think about it when I have time. Or if there is any good way, please let me know in the comments. OnClick can be used normally, so if that's all you need, there should be no problem.

Recommended Posts

I want to place RadioButtons in the same RadioGroup at any position on the screen.
I want to transition to the same screen in the saved state
I want to embed any TraceId in the log
I want to use screen sharing on the login screen on Ubuntu 18
I want to get the value in Ruby
I want to return the scroll position of UITableView!
I want to simplify the log output on Android
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
I want to set the conditions to be displayed in collection_check_boxes
I want to put the JDK on my Mac PC
I want to simplify the conditional if-else statement in Java
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
I want to create a chat screen for the Swift chat app!
I want to use swipeback on a screen that uses XLPagerTabStrip
How to display products by category on the same list screen
I want to add the disabled option to f.radio_button depending on the condition
I want to display the images under assets/images in the production environment
I want to remove the top margin in Grouped UITableView (swift)
[Java] I want to perform distinct with the key in the object
I want to change the value of Attribute in Selenium of Ruby
How to call multiple names at once in the same category
[Android] I want to get the listener from the button in ListView
[Ruby] I want to output only the odd-numbered characters in the character string
I want to write JSP in Emacs more easily than the default.
I want to get the IP address when connecting to Wi-Fi in Java
I want to display an error message when registering in the database
I want to display background-ground-image on heroku.
I want to RSpec even at Jest!
I want to install PHP 7.2 on Ubuntu 20.04.
I want to use @Autowired in Servlet
I want to play a GIF image on the Andorid app (Java, Kotlin)
I want to output the day of the week
[Android] Get the tapped position (coordinates) on the screen
I want to send an email in Java.
I tried to organize the session in Rails
I want to use arrow notation in Ruby
I want to var_dump the contents of the intent
I want to pass APP_HOME to logback in Gradle
rsync4j --I want to touch rsync in Java.
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
I want to truncate after the decimal point
I want to go back to a specific VC by tapping the back button on the NavigationBar!
I want to change the path after new registration after logging in with multiple devises.
I want to morphologically analyze the log in the DB and put it in the DB to classify messages 1
[Active Admin] I want to specify the scope of the collection to be displayed in select_box
[Rails] I want to display the link destination of link_to in a separate tab
# 1_JAVA I want to get the index number by specifying one character in the character string.
I want to hit the API with Rails on multiple docker-composes set up locally
I was addicted to the NoSuchMethodError in Cloud Endpoints
I want to use Combine in UIKit as well.
I want to use Clojure's convenient functions in Kotlin
I tried to organize the cases used in programming
I want to do something like "cls" in Java
[Java] I want to calculate the difference from the date
I want to use NetBeans on Mac → I can use it!
Tokoro I rewrote in the migration from Wicket 7 to 8
I want to use fish shell in Laradock too! !!
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I want to judge the range using the monthly degree