Skip to content

07 Using custom views

Michael Rapp edited this page Dec 27, 2017 · 1 revision

If the class MaterialDialog.Builder should be used to create a "Material Design"-styled custom dialog, the following methods can be used to replace the dialog's child views, which are used to show its title, message, buttons or content (the ListView, which is used to show the list items).

7.1 Using a custom view to display the title

setCustomTitle(View):Builder
setCustomTitle(int):Builder

The setCustomTitle-methods cause the child view, which is used to show the dialog's title and icon, to be replaced by a specific custom view. If the custom view contains a TextView with the id android.R.id.title, the dialog's title will be shown by using this view. Otherwise, the dialog's title will not be shown and the custom can be used for any other purpose.

7.2 Using a custom view to display the message

setCustomMessage(View):Builder
setCustomMessage(int):Builder

The same applies on the setCustomMessage-methods, which allow to replace the view, which is used to show the dialog's message. If the custom view contains a TextView with the id android.R.id.message, the dialog's message will be shown by using this view.

7.3 Using a custom view to display the buttons

setCustomButtonBar(View):Builder
setCustomButtonBar(int):Builder

The setCustomButtonBar-methods allow to replace the buttons of the dialog. The view should contain three buttons of the type Button. The positive button corresponds to the id android.R.id.button1, the negative button corresponds to the id android.R.id.button2 and the neutral button corresponds to the id android.R.id.button3. If one of the buttons is missing, its functionality is lost.

7.4 Using a custom view to display the content

setView(int):Builder
setView(View):Builder

When the setView-methods are used to specify a custom content view by passing the view itself or the resource id of the corresponding layout resource, the ListView, which is used to show the dialog's list items, will be replaced. If the custom view contains a ListView with the id android.R.id.list, this view will be used to show the dialog's list items. Otherwise, the items cannot be shown.

The following screenshots illustrates how a dialog, whose title and content views have been replaced by custom views, may appear: