Add bigger margin to EditText in Android AlertDialog

b85411 picture b85411 · Jan 5, 2015 · Viewed 18.8k times · Source

I have an EditText inside an AlertDialog. It looks like this.

enter image description here

See where it says tddjdjck and how it is indented quite a lot. That is what I want (I used setPadding with left and right set to 50), but I also want the blue line under it to be indented too. How do I do that?

The code I am using is below:

            final AlertDialog.Builder alert = new AlertDialog.Builder(thisActivity);
            final EditText input = new EditText(thisActivity);
            input.setSingleLine();
            input.setPadding(50, 0, 50, 0);

            alert.setTitle("by...");
            alert.setMessage("enter the name of the person who did:");
            alert.setView(input);
            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String value = input.getText().toString().trim();

                }
            });

            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();
                }
            });
            alert.show();  

Thank you

Answer

Ashton Engberg picture Ashton Engberg · Jan 5, 2015
final AlertDialog.Builder alert = new AlertDialog.Builder(thisActivity);
final EditText input = new EditText(thisActivity);
input.setSingleLine();
FrameLayout container = new FrameLayout(thisActivity);
FrameLayout.LayoutParams params = new  FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin = getResources().getDimensionPixelSize(R.dimen.dialog_margin);
input.setLayoutParams(params);
container.addView(input);
alert.setTitle("by...");
alert.setMessage("test message");
alert.setView(container);

Make sure you add another line to your dimens.xml resource file, such as

<dimen name="dialog_margin">20dp</dimen>