Skip to content

Instantly share code, notes, and snippets.

@danialgoodwin
Created August 8, 2016 23:40
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 danialgoodwin/b17d948c831694c220d42857965eab36 to your computer and use it in GitHub Desktop.
Save danialgoodwin/b17d948c831694c220d42857965eab36 to your computer and use it in GitHub Desktop.
Android Custom Preferences
/**
* Copyright (c) 2016 Danial Goodwin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.danialgoodwin.android.preference;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.preference.CheckBoxPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
/** A CheckBoxPreference with the image changed from checkbox to radio button.
* One limitation is that it uses the default framework radio button, which oddly shows a pre-Holo
* design on my Nexus 6 Android 6.0.1. With a little more work, a material design radio button can
* be added instead.
*/
public class RadioButtonPreference extends CheckBoxPreference {
@SuppressWarnings("unused")
public RadioButtonPreference(Context context) {
super(context);
}
@SuppressWarnings("unused")
public RadioButtonPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@SuppressWarnings("unused")
public RadioButtonPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@SuppressWarnings("unused")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RadioButtonPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
changeCheckBoxToRadioButton((ViewGroup) view);
}
private void changeCheckBoxToRadioButton(ViewGroup viewGroup) {
if (viewGroup == null) { return; }
int count = viewGroup.getChildCount();
for (int n = 0; n < count; n++) {
View childView = viewGroup.getChildAt(n);
if (childView instanceof CheckBox) {
final CheckBox v = (CheckBox) childView;
v.setButtonDrawable(android.R.drawable.btn_radio);
// No-go: It shows both RadioButton and CheckBox
// v.setBackgroundResource(android.R.drawable.btn_radio);
return;
} else if (childView instanceof ViewGroup){
changeCheckBoxToRadioButton((ViewGroup) childView);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment