Saturday , October 23 2021

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);

setContentView()R.Layout.activity_main);

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());

Editor.to put()KEY_NAME, Name);

Editor.Applicable());

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 Espresso Tutorial – Testing Fragment in Isolation – Android Coding by DMTechnolab

Welcome to another tutorial in the Android test series. And this post is called Android …

Leave a Reply

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