Android: Set Margin for Edit Text in AlertDialog Box -


i trying create alert dialog box lollipop, going fine stuck in 1 section case of edittext

i want edittext underline , margin left , right 20dp.for underline tried setbackground() , , working fine.

but there problem setbackground() not work api level below 16.

for setmargin 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 want to\n"+""+"exit 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 , 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 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 way set background edittext work below api 16 , setmargin left , right edittext.

margins on root view won't work. try adding padding parent layout told in other answers.
rather creating dialog layout in java, advice inflate xml , use appcompatedittext if want use line background
here's sample code you

alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("message"); // why setting message here when inflating custom view? // need add textview in xml if want set message here // otherwise message not shown // builder.setmessage("do want to\n"+""+"exit 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 app " + input.gettext().tostring(),                 toast.length_long).show();      } });  alertdialog alert = builder.create(); alert.show(); 

lastly cannot buttons instantly after creating dialog. need in onshowlistener if want customize button text colors. or use android.support.v7.app.alertdialog newer dialog designs.

button nbutton = alert.getbutton(dialoginterface.button_negative); // null // nbutton.settextcolor(color.parsecolor("#7e7e7e")); button pbutton = alert.getbutton(dialoginterface.button_positive); // 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> 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -