Android: Establecer margen para editar texto en el cuadro AlertDialog

Estoy tratando de crear un cuadro de diálogo Alerta como Lollipop , todo va bien, pero estoy atrapado en una sección el caso de EditText

Quiero un EditText con subrayado y margen izquierdo y derecho con 20dp.Para subrayar intenté setBackground () , y su funcionamiento bien.

Pero hay un problema que setBackground () no funcionará nivel API por debajo de 16.

Para setMargin lo intenté

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); 

Pero Editar texto utilizando el ancho completo de los padres.

Código Completo

  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")); 

¿Hay alguna manera de establecer el fondo para el EditText que trabajan debajo de API 16 y setMargin izquierda y derecha para el EditText .

Los márgenes en la vista de raíz no funcionarán. Trate de añadir relleno a la disposición de los padres como se indica en otras respuestas.
Pero en lugar de crear el diseño de diálogo en java, le aconsejo inflar un xml y utilizar AppCompatEditText si desea utilizar línea de fondo
He aquí un código de ejemplo para usted

 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(); 

Por último, no puede obtener botones al instante después de crear el diálogo. Necesita hacer esto en OnShowListener si desea personalizar colores de texto de botón. O use android.support.v7.app.AlertDialog para los diseños de diálogo más recientes.

 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:orientation="vertical"> <android.support.v7.widget.AppCompatEditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_margin="16dp" app:backgroundTint="@color/colorPrimary"/> </LinearLayout> 

Utilice este código que funciona para mí.

  final EditText input = new EditText(MainActivity.this); input.setSingleLine(); FrameLayout container = new FrameLayout(thisActivity); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.topMargin = convertDpToPx(30); params.bottomMargin = convertDpToPx(30); input.setLayoutParams(params); container.addView(input); 
  • Android: Cómo evitar que el diseño se presione cuando se invoca el teclado
  • TextInputLayout: RuntimeException - No se pudo resolver el atributo en el índice 24
  • Android - cómo convertir int a cadena y colocarlo en un EditText?
  • ¿Hay alguna manera de poner un campo EditText / entrada en un widget homescreen?
  • Formatear automáticamente el número de teléfono en EditText
  • Android: muestra un texto de edición en un menú
  • TextInputLayout y AutoCompleteTextView
  • Android 6 EditText.setError no funciona correctamente
  • Teclado personalizado: manejo del tipo de entradaTipo
  • Cómo mostrar la fecha seleccionada en EditText utilizando DialogFragment
  • EditText: Evita concentrarte en setText ()
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.