I would like to change the RadioButton
that appears on ListPreference
dialog for a check mark or something different, or none, is there a simple way to do this?
If you want to change the whole dialog, maybe to use a replacement dialog library like this material-dialogs package, you can use this replacement ListPreference
:
import com.afollestad.materialdialogs.MaterialDialog;
public class MaterialListPreference extends ListPreference {
private MaterialDialog.Builder mBuilder;
private Context context;
public MaterialListPreference(Context context) {
super(context);
this.context = context;
}
public MaterialListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
@Override
protected void showDialog(Bundle state) {
mBuilder = new MaterialDialog.Builder(context);
mBuilder.title(getTitle());
mBuilder.icon(getDialogIcon());
mBuilder.positiveText(null);
mBuilder.negativeText(getNegativeButtonText());
mBuilder.items(getEntries());
mBuilder.itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
onClick(null, DialogInterface.BUTTON_POSITIVE);
dialog.dismiss();
if (which >= 0 && getEntryValues() != null) {
String value = getEntryValues()[which].toString();
if (callChangeListener(value))
setValue(value);
}
}
});
final View contentView = onCreateDialogView();
if (contentView != null) {
onBindDialogView(contentView);
mBuilder.customView(contentView);
}
else
mBuilder.content(getDialogMessage());
mBuilder.show();
}
}
It doesn't do much, just the bare minimum to override the dialog display and selection callback parts. YMMV very slightly if you opt for a different dialog library but not too much, they tend to be more or less direct replacements for AlertDialog
.
After analyzing the source code of ListPreference
in AndroidX I came to the conclusion that the simplest way is to write a Preference
that inherits from ListPreference
but generates it's own AlertDialog
that you can customize in whatever way you want:
import android.app.AlertDialog
import android.content.Context
import android.util.AttributeSet
import androidx.preference.ListPreference
import com.my.app.R
class MyListPreference : ListPreference {
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(
context,
attrs,
defStyleAttr
)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?) : super(context)
override fun onClick() {
AlertDialog.Builder(context).setSingleChoiceItems(R.array.app_language, getValueIndex())
{ dialog, index->
if(callChangeListener(entryValues[index].toString())){
setValueIndex(index)
}
dialog.dismiss()
}
.setNegativeButton(R.string.cancel) { dialog, _ -> dialog.dismiss() }
.show()
}
private fun getValueIndex() = context.resources.getStringArray(R.array.app_language).indexOf(value)
}
That way you can use your own style, title, icon and whatever else you can configure on an AlertDialog
.
Try the following links hope it may help you out
http://developer.android.com/guide/topics/ui/settings.html
Example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With