public ToggleGroup(Context context) { super(context); setOrientation(HORIZONTAL); init(); }
private void checkMulti(@IdRes int id) { if (id == NO_ID) removeAllChecked(); else if (mCheckedIds.contains(id)) { setCheckedStateForView(id, false); removeCheckedId(id); } else { setCheckedStateForView(id, true); addCheckedId(id); } // If this group has dividers, request a redraw if (hasDivider()) requestLayout(); }
/** * <p>Clears the selection. When the selection is cleared, no radio button * in this group is selected and {@link #getCheckedId()} returns * null.</p> * * @see #check(int) * @see #getCheckedId() */ public void clearChecked() { check(View.NO_ID); }
@Override protected void onDraw(Canvas canvas) { if (getDividerDrawable() == null) { return; } if (getOrientation() == VERTICAL) { drawDividersVertical(canvas); } else { drawDividersHorizontal(canvas); } }
setOrientation(index); } else { final TypedArray aa = getContext().obtainStyledAttributes(COLOR_BACKGROUND_ATTR); final int themeColorBackground = aa.getColor(0, 0); aa.recycle(); ? getResources().getColor(R.color.toggleGroup_light_background) : getResources().getColor(R.color.toggleGroup_dark_background)); addCheckedId(value); // We set this even if not exclusive to help with designer init();
private void checkExclusive(@IdRes int id) { int currentCheck = getExclusiveCheckedId(); // If no id, then clear if (id == NO_ID) { removeAllChecked(); } else if (id == currentCheck) { // If we've been sent an already checked item if(mAllowUnselected) { // If we don't allow unselected we simply do nothing in this method. setCheckedStateForView(id, false); removeCheckedId(id); } } else { // Unchecked item selected if (currentCheck != NO_ID) { // Uncheck existing selection setCheckedStateForView(currentCheck, false); } setCheckedStateForView(id, true); addCheckedId(id); } }
void drawDividersHorizontal(Canvas canvas) { final int count = getChildCount(); for (int i = 0; i < count; i++) { final CompoundButton child = (CompoundButton) getChildAt(i); if (hasDividerBeforeChildAt(i)) { final LinearLayoutCompat.LayoutParams lp = (LinearLayoutCompat.LayoutParams) child.getLayoutParams(); final int left = child.getLeft() - lp.leftMargin - mDividerWidth; drawVerticalDivider(canvas, left); } } }
void drawDividersVertical(Canvas canvas) { final int count = getChildCount(); for (int i = 0; i < count; i++) { final CompoundButton child = (CompoundButton) getChildAt(i); if (hasDividerBeforeChildAt(i)) { final LinearLayoutCompat.LayoutParams lp = (LinearLayoutCompat.LayoutParams) child.getLayoutParams(); final int top = child.getTop() - lp.topMargin - mDividerHeight; drawHorizontalDivider(canvas, top); } } }
/** * {@inheritDoc} */ @Override protected void onFinishInflate() { super.onFinishInflate(); // checks the appropriate radio button as requested in the XML file int initialCheck = getExclusiveCheckedId(); if (initialCheck != View.NO_ID) { mProtectFromCheckedChange = true; setCheckedStateForView(initialCheck, true); mProtectFromCheckedChange = false; addCheckedId(initialCheck); } }
@Override public void onCheckedChanged(ToggleGroup group, @IdRes int[] checkedId) { int orientation = checkedId[0] == R.id.buttonHorizontal ? LinearLayoutCompat.HORIZONTAL : LinearLayoutCompat.VERTICAL; alignment.setOrientation(orientation); format.setOrientation(orientation); } });
/** * <p>Sets the selection to the radio button whose identifier is passed in * parameter. Using -1 as the selection identifier clears the selection; * such an operation is equivalent to invoking {@link #clearChecked()}.</p> * * @param id the unique id of the radio button to select in this group * * @see #getCheckedId() * @see #clearChecked() */ public void check(@IdRes int id) { if (mExclusive) checkExclusive(id); else checkMulti(id); }
private void setCheckedStateForView(int viewId, boolean checked) { View checkedView = findViewById(viewId); if (checkedView instanceof CompoundButton) { ((CompoundButton) checkedView).setChecked(checked); } }
@Override public void addView(View child, int index, ViewGroup.LayoutParams params) { if (child instanceof CompoundButton) { final CompoundButton button = (CompoundButton) child; if (button.isChecked()) { mProtectFromCheckedChange = true; int currentCheck = getExclusiveCheckedId(); if (mExclusive && currentCheck != View.NO_ID) { setCheckedStateForView(currentCheck, false); } mProtectFromCheckedChange = false; addCheckedId(button.getId()); } } super.addView(child, index, params); }