So you have a FooDialogPreference class which extends DialogPreference, and one of the things you’d like to do to it, is to change the Dialog’s title.
DialogPreference happens to be a bit of a bitch, you can’t just call getDialog() and start playing with its features before it is shown because the Dialog object is only created when DialogPreference.showDialog()is called.
However, there’s ONE entry point where you can make your magic happen.
If we look at the code of
from the geniuses at the Android project, some gentle soul added a call to
onPrepareDialogBuilder(mBuilder); which is a method you’re meant to override.
Override this method, and then with the builder object you’ve received you will be able to call:
However, there’s a couple of things you need to do:
- Instantiate your myCustomTitleView object on the implementation of your onBindDialogView, and keep the reference as a private property of your FooDialogPreference class.
Make sure to detach it from it’s parent ViewGroup, otherwise you will get an IllegalStateException from android.view.ViewGroup.addViewInner() letting you know this view you’re trying to add, cannot have a parent.
This is what I had to do:
[pastacode lang=”java” manual=”final%20ViewGroup%20parent%20%3D%20(ViewGroup)%20myCustomTitleView.getParent()%3B%0Aparent.removeView(myCustomTitleView)%3B%0Abuilder.setCustomTitle(myCustomTitleView)%3B” message=”” highlight=”” provider=”manual”/]
That’s all folks!