Development issue/problem:

I need to create an AlertDialog with a custom view.
The AlertDialog message has a default padding, but when I set the display, it has no padding, I want the same padding as the default message. I use a style that extends the holothema (if any).

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

is the layout of the content:

How can I solve this problem?

Solution 1:

The dialoguePreferredPadding 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 achieve consistent padding in your custom dialogs by specifying this attribute in the dialog format. For example, android:paddingLeft=?dialogPreferredPadding will align the contents of the dialog with the name.

Solution 2:

I’m stuck with the same problem, so I had to find the answer. If anyone’s looking for an answer, this is my solution.

The source code of the AlertDialog layout is described in the file alert_dialog.xml (e.g. here for android 4.4.1, which must match the Holo theme). The layout contains hard coded pads (and these can be different in different versions of Android). To know the default padding, you need to summarize all the paddings of the views that contain the TextView ID / message. In this case it is 3+14+5=22 dp left and 1+10+5=16 dp right.

The android:id/custom element into which the custom view is inserted has indentations of 3 dp on the left and 1 dp on the right (from the root element, the other indentations have no effect) that do not need to be set manually.

To make the resulting custom padding look exactly like the message, set 19 dp on the left and 15 dp on the right (and remember to keep the standard 5 dp padding at the top and bottom).

Example code :

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

This code gives this result (it looks good). In fact, it is a thread of material that seems to have the same stamps (also hard code).

http://server.digimetriq.com/wp-content/uploads/2021/01/Getting-default-padding-for-AlertDialog.png]

Solution 3:

Just to give a more complete answer to the question about use:

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

If you want to do it by code, you can also do it by code:

@JvmStatic
nice to getDimensionFromAttribute(context : Context, attr : Int): Int {
value typedValue = TypedValue()
return if (context.theme.resolveAttribute(attr, typedValue, true))))
typedValue.complexToDimensionPixelSize(typedValue.data, context.resources.displayMetrics)
otherwise 0
}

use:

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

If you use the checkbox (and possibly other views), I don’t think you can set dashes or margins to align with the other views in the dialog, unless you merge them with another view or expand them.

Good luck!

alertdialog padding flutter,alertdialog set full screen,android alertdialog,custom alert dialog android,android-alertdialog margins,android set dialog padding programmatically,dialog bottom margin,android dialog window padding

You May Also Like

4 Browsers that are Super Awesome

Facebook Twitter Pinterest Reddit Messenger WhatsApp Xbox One supports a wide range…

URBN Power Bank Review, Made In India Power Bank, 18W, QC 3.0 + PD

The Indian smartphone accessory brand URBN has recently launched its latest thin…

What You Need to Do If You See a Netflix VPN Error

A global pandemic changed millions of lives in a few months. Many…

How To Activate Microsoft Office 2010 Permanently 2021

How to permanently activate Microsoft Office 2010 – Who doesn’t know MS…