Skip to content

Instantly share code, notes, and snippets.

@SelvinPL
Last active March 2, 2022 15:41
Show Gist options
  • Select an option

  • Save SelvinPL/5436d4b5a72a5aab055c88c76721073c to your computer and use it in GitHub Desktop.

Select an option

Save SelvinPL/5436d4b5a72a5aab055c88c76721073c to your computer and use it in GitHub Desktop.
ConstraintRadioGroup
/*
* Copyright (C) 2019 Sam Lu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//code style "fix" from Selvin
//documentation/usage and original source https://github.com/samlu/ConstraintRadioGroup
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import androidx.annotation.IdRes;
import androidx.constraintlayout.widget.ConstraintHelper;
import androidx.constraintlayout.widget.ConstraintLayout;
import java.util.ArrayList;
public class ConstraintRadioGroup extends ConstraintHelper {
private final ArrayList<RadioButton> mRadioButtons = new ArrayList<>();
private boolean mSkipCheckingViewsRecursively = false;
private int mCurrentSelectedViewId = -1;
private int mSelectedViewIdBeforePreLayout = -1;
private OnCheckedChangeListener mListenerOnCheckedChange;
private final CompoundButton.OnCheckedChangeListener mListenerRadioButton = (buttonView, isChecked) -> {
if (mSkipCheckingViewsRecursively)
return;
if (mCurrentSelectedViewId != -1) {
// uncheck the checked button
mSkipCheckingViewsRecursively = true;
for (RadioButton v : mRadioButtons) {
if (v.getId() == mCurrentSelectedViewId) {
v.setChecked(false);
break;
}
}
mSkipCheckingViewsRecursively = false;
}
setCurrentSelectedViewId(buttonView.getId());
};
public ConstraintRadioGroup(Context context) {
super(context);
}
public ConstraintRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ConstraintRadioGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void init(AttributeSet attrs) {
super.init(attrs);
mUseViewMeasure = false;
}
@Override
public void updatePreLayout(ConstraintLayout container) {
super.updatePreLayout(container);
for (int i = 0; i < mCount; i++) {
int nId = mIds[i];
View v = container.getViewById(nId);
if (v instanceof RadioButton) {
mRadioButtons.add((RadioButton) v);
((RadioButton) v).setOnCheckedChangeListener(mListenerRadioButton);
}
}
if (mSelectedViewIdBeforePreLayout != -1)
check(mSelectedViewIdBeforePreLayout);
}
@Override
public void updatePostLayout(ConstraintLayout container) {
ViewGroup.LayoutParams params = getLayoutParams();
params.width = 0;
params.height = 0;
super.updatePostLayout(container);
}
public void setEnabled(boolean enabled) {
}
public void check(int radioButtonId) {
final boolean isBeforePreLayout = mRadioButtons.isEmpty();
mSelectedViewIdBeforePreLayout = (isBeforePreLayout ? radioButtonId : -1);
boolean found = false;
mSkipCheckingViewsRecursively = true;
for (RadioButton v : mRadioButtons) {
if (v.getId() == radioButtonId) {
v.setChecked(true);
found = true;
} else
v.setChecked(false);
}
mSkipCheckingViewsRecursively = false;
if (!isBeforePreLayout)
setCurrentSelectedViewId(found ? radioButtonId : -1);
}
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
mListenerOnCheckedChange = listener;
}
private void setCurrentSelectedViewId(@IdRes int radioButtonId) {
if (mCurrentSelectedViewId != radioButtonId) {
mCurrentSelectedViewId = radioButtonId;
if (mListenerOnCheckedChange != null)
mListenerOnCheckedChange.onCheckedChanged(this, radioButtonId);
}
}
public interface OnCheckedChangeListener {
void onCheckedChanged(ConstraintRadioGroup group, @IdRes int checkedId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment