java - How to disable a button from another activity -
i have little login program saves data shared preferences. main activity has "login" , "account" buttons. want disable login button when shared preferences null , enable "account" button user sign account second activity. how can enable/disable 2 buttons if shared preferences null?
activity 1
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main_interface); final button login = (button)findviewbyid(r.id.login); final button account = (button)findviewbyid(r.id.account); login.setenabled(false); login.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { startactivity(new intent(mainactivity.this, summation.class)); } }); account.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { startactivity(new intent(mainactivity.this, append.class)); } }); button exit = (button)findviewbyid(r.id.exit); exit.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { finish(); system.exit(0); } });
activity 2
protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); stetho.initializewithdefaults(this); setcontentview(r.layout.append_interface); final edittext usernameappend = (edittext)findviewbyid(r.id.usernameappend); final edittext passwordappend = (edittext)findviewbyid(r.id.passwordappend); //this buttons appends username , password sharedpreferences button append = (button)findviewbyid(r.id.append); append.setonclicklistener(new view.onclicklistener() { public void onclick(view arg0) { final sharedpreferences mysharedpref = getsharedpreferences(pref_name, mode_private); final sharedpreferences.editor editor = mysharedpref.edit(); //append data final string un = usernameappend.gettext().tostring(); editor.putstring(editorusername,un); editor.commit(); final string pw = passwordappend.gettext().tostring(); editor.putstring(editorpassword, pw); editor.commit(); if(mysharedpref != null){ toast.maketext(append.this,"account saved",toast.length_long).show(); startactivity(new intent(append.this, mainactivity.class)); }else{ toast.maketext(append.this,"please enter again",toast.length_long).show(); } } } ); //exit button,, exits app button exit = (button)findviewbyid(r.id.exit); exit.setonclicklistener(new view.onclicklistener() { public void onclick(view arg0) { finish(); system.exit(0); } }); }
in 1st activity check either shared preference exists or not. first time null.
btnlogin.setenabled(false); btnaccount.setenabled(false); sharedpreferences mprefs=this.getsharedpreferences(your_preference_name,context.mode_private); if(mprefs==null) { btnaccount.setenabled(true); } btnaccount.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { } }); btnlogin.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { } });
Comments
Post a Comment