Getting default padding for AlertDialog

AndroidAndroid Alertdialog

Android Problem Overview


I need to make a AlertDialog with a custom view. The message of a AlertDialog has a default padding but when I set a view it has no padding, I want to get the same padding as the default as the message. I'm using a style that extends Holo theme (if this is relevant).

AlertDialog.Builder builder = new AlertDialog.Builder(PlaylistListView.this.getContext());
builder.setTitle("Title");
builder.setView(inflate(context, R.layout.music_player_create_dialog, null));
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();

This is the layout for the content:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/abc_dialog_padding_material"
    android:paddingRight="@dimen/abc_dialog_padding_material"
    android:paddingTop="@dimen/abc_dialog_padding_top_material"
    android:paddingBottom="@dimen/abc_dialog_padding_top_material">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Title:"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:background="@null"
        android:layout_marginTop="20dp"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@color/divider"/>
</LinearLayout>

Android Solutions


Solution 1 - Android

The ?dialogPreferredPadding attribute now has this value. It was introduced in API 22; see https://developer.android.com/sdk/api_diff/22/changes/android.R.attr.html.

You can get consistent padding in your custom dialogs by specifying this attribute on the dialog's layout. For example, android:paddingLeft="?dialogPreferredPadding" will bring the dialog's contents into alignment with its title.

Solution 2 - Android

I got stuck with the same problem, so had to find the answer. In case anyone comes looking for the answer, here is my solution.

The source code for AlertDialog's layout is described in alert_dialog.xml (e.g. here for android 4.4.1, which should correspond to Holo theme). Layout has hard-coded paddings (and they might be different in different versions of android). To know the default padding you have to sum up all paddings for Views containing id/message" TextView. In this case they are 3+14+5=22 dp left and 1+10+5=16 dp right.

The android:id/custom element is where a custom view gets inserted into and it has 3 dp left and 1 dp right paddings (from the root element, others do not have paddings) which you do not have to set manually.

So to have resulting padding of a custom View be the same as message's, set it to 19 dp left and 15 dp right (and don't forget to keep default 5 dp top and bottom padding).

Example code:

final EditText input = new EditText( getContext() );
float dpi = ctx.getResources().getDisplayMetrics().density;
AlertDialog dialog = (new AlertDialog.Builder(getContext()))
		.setTitle("Rename track")
		.setMessage("Track name:")
        .setPositiveButton("OK", null)
        .setNegativeButton("Cancel", null)
		.create();
dialog.setView(input, (int)(19*dpi), (int)(5*dpi), (int)(14*dpi), (int)(5*dpi) );
dialog.show();

These code gives result like this (looks good). BTW, this is Material theme, which seems to have the same paddings (also hardcoded).

[]

Solution 3 - Android

Just to add a more complete answer of how to use it:

You can use in the layout XML file android:paddingStart="?dialogPreferredPadding" and android:paddingEnd="?dialogPreferredPadding" .

Alternatively, if you want to do it by code:

@JvmStatic
fun getDimensionFromAttribute(context: Context, attr: Int): Int {
    val typedValue = TypedValue()
    return if (context.theme.resolveAttribute(attr, typedValue, true)) 
           TypedValue.complexToDimensionPixelSize(typedValue.data, context.resources.displayMetrics) 
           else 0
}

usage:

val alertDialogPadding = getDimensionFromAttribute(context, R.attr.dialogPreferredPadding)
view.setPadding(alertDialogPadding, 0, alertDialogPadding, 0)

If you use a CheckBox (and maybe some other views), I don't think it's possible to set the paddings or margins so that it will get aligned nicely with other views in the dialog, unless you wrap it with a different view, or extend it

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSnapDragonView Question on Stackoverflow
Solution 1 - AndroidtrooperView Answer on Stackoverflow
Solution 2 - AndroidOsman-pashaView Answer on Stackoverflow
Solution 3 - Androidandroid developerView Answer on Stackoverflow