We are currently migrating to Androidx namespace with our Android app project. However I noticed that not only the namespace seems to have changed. For DialogPreference also some interfaces which were using before are now missing
For example the following methods seem to be missing: onBindDialogView, showDialog, onDialogClosed.
Since we use some of these methods to influence the default behavior of the dialog, it is unclear to me how I should realize this functionality now. For example we are validating the input before closing the dialog, we are saving the value in a database instead of the sharedpreferences and adding some dynamic elements to the dialog.
Has anyone else already encountered this problem and found a solution? Did I miss anything in the documentation? Is there another concept that we can / should use?
It would be possible to use Fragments instead of DialogPreference but for small amounts of content (e.g. a list of tree items, where the user can choose from) this seems to be a lot of overhead for me...
Starting from androidx source files, I've migrated custom classes based on old DialogPreference to new androidx.preference.DialogPreference with the following procedure:
The old custom dialog class (e.g. CustomDialogPreference) based on legacy DialogPreference should be split into two separate classes:
In the main fragment handling preferences based on PreferenceFragmentCompat the onDisplayPreferenceDialog method should be overridden to show the custom dialog, e.g.:
private static final String DIALOG_FRAGMENT_TAG = "CustomPreference"; @Override public void onDisplayPreferenceDialog(Preference preference) { if (getParentFragmentManager().findFragmentByTag(DIALOG_FRAGMENT_TAG) != null) { return; } if (preference instanceof CustomPreference) { final DialogFragment f = CustomDialog.newInstance(preference.getKey()); f.setTargetFragment(this, 0); f.show(getParentFragmentManager(), DIALOG_FRAGMENT_TAG); } else { super.onDisplayPreferenceDialog(preference); } }
Instead of using DialogPreference
, you can write your own custom Preference
with an AlertDialog
. This may be a workaround for those who don't want to deal with the DialogPreference
and PreferenceDialogFragmentCompat
.
import android.content.Context; import androidx.appcompat.app.AlertDialog; import androidx.preference.Preference; public class CustomDialogPreference extends Preference { private final Context context; public CustomDialogPreference(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } @Override protected void onClick() { //what happens when clicked on the preference AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("TITLE") .setMessage("message") .setPositiveButton("OK", (dialog, which) -> { String preferenceKey = getKey(); //You can update the SharedPreference with the key //.... }) .setNegativeButton("CANCEL", (dialog, which) -> { //.... }) .create().show(); } }
onClick()
and getKey()
methods belong to the Preference
class. The context
object comes with the constructor and so on..
The key can be defined, as other preferences, in xml file or programmatically in the PreferenceFragment.
<com.myApp.CustomDialogPreference android:key="my_preference_key" android:summary="..." android:title="..." />
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