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)

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();
You can pass spacing parameter in setView method
alert.setView(view ,left_space , top_space , right_space , bottom_space);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With