Binil Surendran Binil Surendran - 5 months ago 65
Android Question

Android: Set Margin for Edit Text in AlertDialog Box

I am trying to create an Alert Dialog box like Lollipop, everything is going fine but I am stuck in one section the case of EditText

I want an EditText with underline and margin left and right with 20dp.For underline I tried setBackground() , and its working fine.

But there is a problem that setBackground() will not work API Level below 16.

For setMargin I tried

final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(30,0,30,0);
input.setLayoutParams(lp);
input.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
input.setBackground(getResources().getDrawable(R.drawable.edit_text_line));
builder.setView(input);


But Edit Text using full parent width.

Full Code

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Message");
builder.setMessage("Do you want to\n"+""+"exit from app");

final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(30,0,30,0);
input.setLayoutParams(lp);
input.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
input.setBackground(getResources().getDrawable(R.drawable.edit_text_line)); //call reequires api 16 and above
builder.setView(input);

builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "You exit from app " + input.getText().toString(),
Toast.LENGTH_LONG).show();

}
});

AlertDialog alert = builder.create();
alert.show();
Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
nbutton.setTextColor(Color.parseColor("#7e7e7e"));
Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
pbutton.setTextColor(Color.parseColor("#109c8f"));


Is there any way to set the background for the EditText that work below API 16 and setMargin left and right for the EditText.

Answer

Margins on root view won't work. Try adding it in parent layout as told in other answers.
But rather than creating dialog layout in java, I would advice you to inflate an xml and use AppCompatEditText if you want to use line background
Here's a sample code for you

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Message");
// Why are you setting message here when you are inflating custom view?
// You need to add another TextView in xml if you want to set message here
// Otherwise the message will not be shown
// builder.setMessage("Do you want to\n"+""+"exit from app");
View view = LayoutInflater.from(this).inflate(R.layout.dialog_layout, null);
final AppCompatEditText input = (AppCompatEditText) view.findViewById(R.id.editText);
builder.setView(view);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this, "You exit from app " + input.getText().toString(),
                Toast.LENGTH_LONG).show();

    }
});

AlertDialog alert = builder.create();
alert.show();

Lastly you cannot get buttons instantly after creating dialog. You need to do this in OnShowListener if you want to customize button text colors. Or use android.support.v7.app.AlertDialog for newer Dialog designs.

Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
// will be null
// nbutton.setTextColor(Color.parseColor("#7e7e7e"));
Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
// will be null
// pbutton.setTextColor(Color.parseColor("#109c8f"));

dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_margin="16dp"
              android:orientation="vertical">

    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        app:backgroundTint="@color/colorPrimary"/>

</LinearLayout>