android - Validation on EditText in alertDialog -
i trying add empty field validations on edittext
on alertdialog
. after field empty error message not getting displayed, instead alertdialog
closing. if conditions working i'm not able post operations if of field empty.
here java sample code:
public class touractivity extends appcompatactivity { private layoutinflater inflater; private floatingactionbutton fab; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_tour); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { inflater = touractivity.this.getlayoutinflater(); view content = inflater.inflate(r.layout.activity_add_new_trip, null); final edittext editevent = (edittext) content.findviewbyid(r.id.edtxt_eventname); final edittext editstartdate = (edittext) content.findviewbyid(r.id.edtxt_eventsdate); alertdialog.builder builder = new alertdialog.builder(touractivity.this); builder.setview(content) .settitle("add event") .setpositivebutton(android.r.string.ok, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { editevent.seterror(null); editstartdate.seterror(null); boolean cancel = false; view focusview = null; if (textutils.isempty(editevent.gettext().tostring()))) { editevent.seterror("please enter event name."); return; } if (textutils.isempty(editstartdate.gettext().tostring())) { editstartdate.seterror("please enter event start date."); focusview = editstartdate; cancel = true; } if (cancel == true) { snackbar.make(findviewbyid(android.r.id.content), "event unsuccessful.", snackbar.length_long) .setactiontextcolor(color.red) .show(); focusview.requestfocus(); } else { // action here } } }) .setnegativebutton(cancel, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); } }); alertdialog dialog = builder.create(); dialog.show(); } }); } }
try this
if (textutils.isempty(edevent)) { editevent.seterror("please enter event name."); editevent.requestfocus(); return; }
Comments
Post a Comment