android - Toggle Button Not Working Properly on Bluetooth Adapter -
hi sir/ma'am, i'm trying enable/disable bluetooth on toggle button , there additional functionality ask password on enable/disable. toggle button taking default state of bluetooth @ launch(means if bluetooth enable, toggle set on , if bluetooth disable, toggle off).
also, working fine on when enter correct password. but, main problem when enter wrong password, toggle not behave expected.
what mean, suppose bluetooth on , trying disable app. when click on toggle, ask me enter password. now, if enter wrong password, bluetooth doesn't turn off, toggle button changed state turn off.
here tried far:-
setcontentview(r.layout.activity_main); mb = bluetoothadapter.getdefaultadapter(); tb1 = (togglebutton)findviewbyid(r.id.apptogglebtn); tb1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub check(); } }); } public void check() { final dialog dialog = new dialog(mainactivity.this); dialog.requestwindowfeature(window.feature_no_title); dialog.setcontentview(r.layout.dialog); dialog.setcancelable(true); dialog.show(); imageview savebtn=(imageview)dialog.findviewbyid(r.id.confirmdialogbtn); final edittext password=(edittext)dialog.findviewbyid(r.id.confirmdialogedittext); savebtn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if(password.gettext().tostring().equals("asd")) { if(mb.isenabled()) { mb.disable(); tb1.setchecked(mb.isenabled()); } else { mb.enable(); tb1.setchecked(mb.isenabled()); } } else { toast.maketext(getapplicationcontext(), "wrong", 1000).show(); if(mb.isenabled()) { tb1.setchecked(mb.isenabled()); } else { tb1.setchecked(mb.isenabled()); } } dialog.dismiss(); } }); }
also, toggle button, used:-
android:background="@drawable/toggle_selector"
and in toggle_selector.xml
<item android:drawable="@drawable/toggle_on" android:state_checked="true"/> <item android:drawable="@drawable/toggle_off" android:state_checked="false"/>
please please me this. badly stuck @ point. in advance.
try this.
if(password.gettext().tostring().equals("asd")) { if(mb.isenabled()) { mb.disable(); tb1.setchecked(mb.isenabled()); } else { mb.enable(); tb1.setchecked(mb.isenabled()); } } else { toast.maketext(getapplicationcontext(), "wrong", 1000).show(); }
Comments
Post a Comment