Skip to content

Instantly share code, notes, and snippets.

@hrules6872
Last active March 17, 2016 15:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrules6872/042b7936c34940f844b6 to your computer and use it in GitHub Desktop.
Save hrules6872/042b7936c34940f844b6 to your computer and use it in GitHub Desktop.
AdvancedRadioGroup
public class AdvancedRadioGroup extends LinearLayout {
private int radioButtonCheckedId;
private boolean working;
private RadioGroup.OnCheckedChangeListener listener;
public AdvancedRadioGroup(Context context) {
this(context, null);
}
public AdvancedRadioGroup(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AdvancedRadioGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public AdvancedRadioGroup(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
final TypedArray attributes =
context.obtainStyledAttributes(attrs, R.styleable.AdvancedRadioGroup);
radioButtonCheckedId =
attributes.getResourceId(R.styleable.AdvancedRadioGroup_checkedButton, View.NO_ID);
attributes.recycle();
working = false;
super.setOnHierarchyChangeListener(hierarchyChangeListener);
}
private final CompoundButton.OnCheckedChangeListener checkedChangeListener =
new CompoundButton.OnCheckedChangeListener() {
@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked && !working) {
int checkedId = buttonView.getId();
if (listener != null) {
listener.onCheckedChanged(null, checkedId);
}
check(checkedId);
}
}
};
private final OnHierarchyChangeListener hierarchyChangeListener =
new OnHierarchyChangeListener() {
@Override public void onChildViewAdded(View parent, View child) {
ArrayList<View> views = getAllChildren(parent);
for (View view : views) {
if (view instanceof RadioButton) {
if (view.getId() == radioButtonCheckedId) {
((RadioButton) view).setChecked(true);
}
((RadioButton) view).setOnCheckedChangeListener(checkedChangeListener);
}
}
}
@Override public void onChildViewRemoved(View parent, View child) {
}
};
public @IdRes int getCheckedRadioButtonId() {
return radioButtonCheckedId;
}
public void check(@IdRes int radioButtonResId) {
this.radioButtonCheckedId = radioButtonResId;
working = true;
ArrayList<View> views = getAllChildren(this);
for (View view : views) {
if (view instanceof RadioButton) {
((RadioButton) view).setChecked(view.getId() == radioButtonResId);
}
}
working = false;
}
public void check(@NonNull RadioButton radioButton) {
check(radioButton.getId());
}
public void clearCheck() {
ArrayList<View> views = getAllChildren(this);
for (View view : views) {
if (view instanceof RadioButton) {
((RadioButton) view).setChecked(false);
}
}
}
public void setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener) {
this.listener = listener;
}
private ArrayList<View> getAllChildren(View view) {
ArrayList<View> views = new ArrayList<>();
if (!(view instanceof ViewGroup)) {
views.add(view);
return views;
}
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
views.add(view);
views.addAll(getAllChildren(child));
}
return views;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment