Skip to content

Instantly share code, notes, and snippets.

@DesigningKnights
Last active May 23, 2018 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DesigningKnights/52ade949a2f5618c04bbf028fedbd104 to your computer and use it in GitHub Desktop.
Save DesigningKnights/52ade949a2f5618c04bbf028fedbd104 to your computer and use it in GitHub Desktop.
An "unbound" radio group. It acts like a radio group, but allows you to place your radio buttons anywhere you wish in your layout. It keeps track of which buttons should act as a group.
<!-- this is a sample couple of radio buttons for a layout
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="tag"
android:layout_weight="1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="tag"
android:layout_weight="1" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="tag"
android:layout_weight="1" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="tag"
android:layout_weight="1" />
package com.designingknights.unboundradiogroup;
import android.app.Activity;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
/**
* Created by Timothy Winters on 5/23/2018.
*/
public class UnboundRadioGroup implements RadioButton.OnClickListener
{
private ViewGroup viewGroup;
private String groupTag;
private int lastChecked = -1;
private Activity activity;
private OnClickListener clickListener;
/**
* Instantiates a new Custom radio group.
*
* @param activity the activity
* @param viewGroupID the view group id
*/
public UnboundRadioGroup(@NonNull Activity activity, int viewGroupID)
{
this.activity = activity;
this.viewGroup = (ViewGroup) activity.findViewById(viewGroupID);
init();
}
/**
* Instantiates a new Custom radio group.
*
* @param activity the activity
* @param viewGroup the view group
*/
public UnboundRadioGroup(@NonNull Activity activity, ViewGroup viewGroup)
{
this.activity = activity;
this.viewGroup = viewGroup;
init();
}
private void init()
{
clickListener = null;
// buildGroup(viewGroup);
}
/**
* Create group by tag.
*
* @param groupTag the group tag
*/
public void createGroupByTag(String groupTag)
{
this.groupTag = groupTag;
buildGroup(viewGroup);
}
private void buildGroup(@NonNull ViewGroup viewGroup)
{
int count = viewGroup.getChildCount();
for (int i = 0; i < count; i++)
{
View view = viewGroup.getChildAt(i);
if (view instanceof ViewGroup)
{
buildGroup((ViewGroup) view);
}
else if (view instanceof RadioButton)
{
RadioButton radioButton = (RadioButton) view;
if (radioButton.getTag().equals(groupTag))
{
radioButton.setOnClickListener(this);
}
}
}
}
/**
* Add.
*
* @param radioButton the radio button
*/
public void add(@NonNull RadioButton radioButton)
{
radioButton.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
int checked = v.getId();
if (checked != lastChecked)
{
((RadioButton) v).setChecked(true);
if (clickListener != null) clickListener.OnClick((RadioButton) v);
if (lastChecked != -1)
{
((RadioButton) activity.findViewById(lastChecked)).setChecked(false);
}
lastChecked = checked;
}
}
/**
* Sets on click listener.
*
* @param clickListener the click listener
*/
public void setOnClickListener(OnClickListener clickListener)
{
this.clickListener = clickListener;
}
/**
* The interface On click listener.
*/
public interface OnClickListener
{
/**
* On click.
*
* @param radioButton the radio button
*/
public void OnClick(RadioButton radioButton);
}
}
/* If you don't want to use an anonymous inner class, you can use implements
public class MainActivity extends AppCompatActivity implements UnboundRadioGroup.OnClickListener
*/
// There are a few ways to create a group.
// Find the root Viewgroup
ViewGroup viewGroup = (ViewGroup) findViewById(android.R.id.content);
// create a radio group with the root viewgroup
UnboundRadioGroup unboundRadioGroup1 = new UnboundRadioGroup(this, viewGroup);
// don't forget to set a click listener for the group. Using implements in this case
unboundRadioGroup1.setOnClickListener(this);
// or create a radio group with the id of the root viewgroup, whichever you prefer.
UnboundRadioGroup unboundRadioGroup2 = new UnboundRadioGroup(this, android.R.id.content);
// add your click listener using an inner class
unboundRadioGroup2.setOnClickListener(new UnboundRadioGroup.OnClickListener()
{
@Override
public void OnClick(RadioButton radioButton)
{
Log.i("radioButton", radioButton.getTag().toString());
}
});
// This method manually adds buttons to your group
unboundRadioGroup1.add((RadioButton) findViewById(R.id.radioButton1));
unboundRadioGroup1.add((RadioButton) findViewById(R.id.radioButton2));
/* and this method automatically adds buttons to your group based on the android:tag property in the XML
Note that you should NOT use this method if you need the tags elsewhere in your code. However, if you are not going to
need the tags, you can set the tags of multiple radio buttons to the same name, and then this method will create a group from them
*/
unboundRadioGroup2.createGroupByTag("tag");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment