Prerequisite

If you not understand this lecture Shared Preference in android studio.You should also read my previous lectures Popup Menu in android studio and Contextual Action mode in android studio . After reading these lecture you will also able to create Shared preference of your app.

Overview

Android provides many ways of storing data of an application. One of this way is called Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of key value pair.Shared preference is API from android SDK to store and retrieve application preference.Shared preference store date persistently even user close the application or turn off the device But date saved.

Introduction

In this lecture well we describe how can create Shared Preference in android studio.And also describe what Shared preference in android.why we can use it.After Reading this lecture you will understand Shared preference in android And i hope you will create Shared preference of your application.

Description of Shared preference in android

Basically shared preference in android are used to save the state of an activity .In this lecture we describe what are the mechanism is used to save information in android.There is lot of mechanism is available to store information and data in android.You can use share preference API to store date and you can saves date into file and you can save date in sq-lite Database and you can save data remote server.So we create a simple application that demonstrate how can saved information using shared preference API.So we create a new project in android studio.Every project contain two file first is java class file and second is layout XML file.So open a layout .XML file and add a radio group inside the radio group you will add a radio button.After that add seek-bar and also define id of seek bar.In this example we are going to create a simple UI that includes only (Edit text) with Different attributes.And paste the following code into layout .XML file:

XML Layout 

First of you will open a XML file of layout and set Linear Layout  and add  Edit text

Different property that are used in layout.xml file  explain here

  • warp_content   warp_content means height and width similar to the text that are entered.
  • Match_parent   means height and width similar to parent.
  • Radio group      Radio button is used to create multipal-exclusion scope of radio button.
  • Radio button     Radio button allow to user to choice one option at a same time to set predefined options
  • Button                 when button can be pressed, or clicked, by the user to perform an action
  • Seek-bar             Seek-bar is similarly to progress bar but provides facility to user it has a draggable slider that will allow the user to change the display control.
  • Edit text              The Edit Text control allows a user to enter text into an application

Code that are write in Layout. XML file

Now you may open the Graphical layout editor to preview the User Interface you created of Shared preference in android.

Alt Tag Shared preference in android layout

Code that are write MainActivity. java class file.

So open a Main.java.class file.and create a object of Edit text. After that create other object of seek-bar and also initialize these two objects.In a seek-bar, there call back method is used.

  • onProgressChanged
  • onStartTrackingTouch
  • onStopTrackingTouch

If the progress bar change then onProgressChanged method invoke.At beginning of seekbar onStartTrackingTouch method invoke.if user finished the seek-bar then onStopTrackingTouch method is invoke. After completing this step we define a on-click method in a Radio buttons when user select a Radio button option change the state of text.For change state of text we  will use changeColor method  inside the method we use a switch case statement and get the id that are define layout.XML file . In a switch case we will used three alternative. if user select first option then first condition or case will execute and change the color that are define if user select second option then second condition or case will execute and change the color that are define .if user select third option then third condition or case will execute and change the color that are define.

Run the Application

Alt Tag Shared preference in android

Thank you  for reading this lecture Hope you got the idea.