android - how i retrive the data in edit text from shared preference? -
hi can tell me hod saved data in edit text after saving , mean when save text want text appear in edit text time thanks. here code:
public class mainactivity extends appcompatactivity { private toolbar toolbar; private edittext inputname; private textinputlayout inputlayoutname; private button btnsave; public static final string mypreferences = "myprefs" ; public static final string message = "namekey"; sharedpreferences sharedpreferences; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); inputlayoutname = (textinputlayout) findviewbyid(r.id.input_layout_name); inputname = (edittext) findviewbyid(r.id.input_message); btnsave = (button) findviewbyid(r.id.btn_save); inputname.addtextchangedlistener(new mytextwatcher(inputname)); sharedpreferences = getsharedpreferences(mypreferences, context.mode_private); btnsave.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { string m = inputname.gettext().tostring(); sharedpreferences.editor editor = sharedpreferences.edit(); editor.putstring(message, m); editor.apply(); toast.maketext(getapplicationcontext(), "thank you!", toast.length_short).show(); } }); }
inputname.settext(sharedpreferences.getstring(message, "default_value"));
Comments
Post a Comment