Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting default padding for AlertDialog

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> 
like image 261
SnapDragon Avatar asked Oct 12 '15 06:10

SnapDragon


People also ask

Is AlertDialog deprecated?

A simple dialog containing an DatePicker . This class was deprecated in API level 26.

How can I customize AlertDialog in Android?

Step 1: Create a XML file: custom_layout. Add the below code in custom_layout. xml. This code defines the alertdialog box dimensions and add a edittext in it.

Which method is used to set in AlertDialog?

Alert Dialog code has three methods: setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message. setIcon() method is use to set the icon on Alert dialog box.


1 Answers

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.

like image 198
trooper Avatar answered Sep 18 '22 16:09

trooper