Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set margins for an EditText inside an AlertDialog

Tags:

android

I use an EditText inside an AlertDialog like:

    final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
    final EditText input = new EditText(getActivity());
    alert.setView(input);

Now I'd like to set margins for this EditText so it doesn't take the full width of the dialog. How can I do this? (I do not consider the option of inflating a custom Layout at this point)

enter image description here

like image 990
Droidman Avatar asked Jan 26 '26 17:01

Droidman


2 Answers

if you don't want to inflate a custom Layout, Try this

AlertDialog.Builder dialog = new AlertDialog.Builder(Login.this);
final EditText email_input = new EditText(Login.this);
LinearLayout linearLayout = new LinearLayout(Login.this);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.CENTER;

email_input.setHint("Email Address");
email_input.setLayoutParams(layoutParams);

linearLayout.addView(email_input);
linearLayout.setPadding(60, 0, 60, 0);

dialog.setTitle("Forgot Password?");
dialog.setMessage("Enter Your Email Address");

dialog.setView(linearLayout);

dialog.setPositiveButton("Send", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // Some Code 
    }
});

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

dialog.show();
like image 65
Hafiz Muneeb Avatar answered Jan 29 '26 09:01

Hafiz Muneeb


You can pass spacing parameter in setView method

alert.setView(view ,left_space , top_space , right_space , bottom_space);
like image 24
Bhargav Thanki Avatar answered Jan 29 '26 07:01

Bhargav Thanki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!