how to use shared preferences in android.... i am a beginner so help me -
i beginner of android.. in designing android app.. don't know shared preferences.. first time have use shared preferences,. 1 can in code can implement. follwoing code , please make changes in code: tell me how save values onclick of button m storing in different variable temporaryliy.
import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.view.window; import android.view.windowmanager; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class newpage extends activity { string un = null; string pass= null; string fname= null; string lname= null; string age= null; string city= null; string coun= null; string email= null; string pno= null; string repass= null; @override protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); this.getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.create_acc); final button b = (button) findviewbyid(r.id.button3); b.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { intent = new intent(newpage.this, signin.class); i.addflags( intent.flag_activity_clear_top | intent.flag_activity_new_task); startactivity(i); } }); final button c = (button) findviewbyid(r.id.button2); c.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { intent = new intent(newpage.this, createacc.class); i.addflags( intent.flag_activity_clear_top | intent.flag_activity_new_task); startactivity(i); } }); final button = (button) findviewbyid(r.id.button1); a.setonclicklistener(new view.onclicklistener() { @suppresslint("showtoast") public void onclick(view v){ edittext input1= (edittext)findviewbyid(r.id.fname1); fname = input1.geteditabletext().tostring(); edittext input2= (edittext) findviewbyid(r.id.lname1); lname = input2.geteditabletext().tostring(); edittext input3= (edittext) findviewbyid(r.id.age1); age = input3.geteditabletext().tostring(); edittext input4= (edittext) findviewbyid(r.id.city1); city = input4.geteditabletext().tostring(); edittext input5= (edittext) findviewbyid(r.id.country1); coun = input5.geteditabletext().tostring(); edittext input6= (edittext) findviewbyid(r.id.email1); email = input6.geteditabletext().tostring(); edittext input7= (edittext) findviewbyid(r.id.pno1); pno = input7.geteditabletext().tostring(); edittext input8= (edittext) findviewbyid(r.id.uname1); un = input8.geteditabletext().tostring(); edittext input9= (edittext) findviewbyid(r.id.passwd1); pass = input9.geteditabletext().tostring(); edittext input10= (edittext) findviewbyid(r.id.repass1); repass = input10.geteditabletext().tostring(); if((fname!=null) && (lname!=null) && (age!=null) && (city!=null) && (coun!=null) && (email!=null) && (pno!=null) && (un!=null) && (pass!=null) && (repass!=null)) { if(pass == repass) { toast.maketext(newpage.this, ".....data saved....." ,toast.length_long).show(); intent in = new intent(newpage.this, signin.class); in.addflags( intent.flag_activity_clear_top | intent.flag_activity_new_task); startactivity(in); } else { toast.maketext(newpage.this, ".....password doesn't match....." ,toast.length_long).show(); } } else { toast.maketext(newpage.this, ".....complete form please....." ,toast.length_long).show(); } } }); } }
to read or write sharedpreferences
first need sharedpreferences
sharedpreferences prefs = getsharedpreferences(tag, context.mode_private);
once have sharedpreferences
can write them
prefs.edit().putstring(key, value).commit();
or can read them
prefs.getstring(key, value);
that's pretty it. tag in preferences name you're going call sharedpreferences
- can string. saved key value pair. need figure out goes in code since have no idea you're trying store.
Comments
Post a Comment