Android SharedPreferences Example – Writing and Reading Values – Android Coding by DMTechnolab

The package The trap.Simplified coding.Shared;

Import Android.Content.Sharing;

Import Android.Support.v7.App.AppCompatActivity;

Import Android.The.Bundle;

Import Android.Opinion.Opinion;

Import Android.Widget.Change in writing;

Import Android.Widget.text view;

public class Main activity Is spread AppCompatActivity {{

Private Fixed The last String SHARED_PREF_NAME = “mysaredpref”;

Private Fixed The last String KEY_NAME = “Keyname”;

Change in writing Change in writing;

text view text view;

@ Override

Protected Zero Go ahead()Bundle saveInstanceState) {{

Super.Go ahead()saveInstanceState);


Change in writing = findViewById()R.Id.editTextName);

text view = findViewById()R.Id.text view);

findViewById()R.Id.buttonSave).setOnClickListener()New Opinion.OnClickListener()) {{

@ Override

public Zero on click()Opinion Opinion) {{

To save());

display name());




Private Zero To save()) {{

String Name = Change in writing.Gettext()).Wire());

If ()Name.Is Empty())) {{

Change in writing.on the set()“Name (required)”);

Change in writing.request());

The return;


Sharing SP = getSaredPreferences()SHARED_PREF_NAME, MODE_PRIVATE);

Sharing.Editor Editor = SP.Edit()); put()KEY_NAME, Name);


Change in writing.Settext()“”);


Private Zero display name()) {{

Sharing SP = getSaredPreferences()SHARED_PREF_NAME, MODE_PRIVATE);

String Name = SP.Get up()KEY_NAME, Invalid);

If ()Name ! = Invalid) {{

text view.Settext()“Welcome “ + Name);




Source link

About dmtechnolab

Check Also

Simplified Coding

Android Hilt Tutorial – Injecting Dependencies with Hilt – Android Coding by DMTechnolab

You are welcome Android Hilt tutorial. I hope you all are aware of it Dependency …

Leave a Reply

Your email address will not be published. Required fields are marked *