Skip to main content

Android Shared Preference Tutorial


The sharedPreferernce provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use sharedPreferences  to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

to know more about   Refer  sharedPreference

you can download the source code of this project from  google drive https://drive.google.com/folderview?id=0BySLpWhqmbbdRXZxd0ZveHllcG8&usp=sharing
click on the above link ->sign into  your  google account ->add this to your google drive -> open it in google drive and download it.

Following is the demo application using SharedPrefence. This application is just used to set and get name.Value that you set will be persistent even if you close the application. the value is saved in a xml file. you can view this file in eclipse . open  DDMS go to   FileExplore there is folder data. Open
data->data->yourAppPackage(com.arun.DemoSharedPreference)->shared_prefs-> yourAppPackage.xml(com.arun.DemoSharedPreference.xml)


First create a xml layout for  setting and getting name.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editname_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/setBtn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set Name" />

    <Button
        android:id="@+id/getBtn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Name" />

    <EditText
        android:id="@+id/showname_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


Activity for setting and getting files



package com.arun.DemoSharedPreference;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainaActivity extends Activity {
 SharedPreferences namePreferences;
 EditText ediName, showName;
 Button setBtn, getBtn;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  namePreferences = PreferenceManager.getDefaultSharedPreferences(this);
  ediName = (EditText) findViewById(R.id.editname_id);
  showName = (EditText) findViewById(R.id.showname_id);
  setBtn = (Button) findViewById(R.id.setBtn_id);
  getBtn = (Button) findViewById(R.id.getBtn_id);
  setBtn.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

    SharedPreferences.Editor nameEditor = namePreferences.edit();
    nameEditor.putString("name", ediName.getText().toString());
    nameEditor.commit();
   }

  });
  getBtn.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

    String nameValue = namePreferences.getString("name", "");
    showName.setText(nameValue);
   }

  });

 }
}


following is the sharedPreference file of my appliction from DDMS


<?xml version='1.0' encoding='utf-8' standalone='yes' ?>

<map>

<string name="name">arun</string>

</map>

Here is the screen shot of my application



Comments

  1. Please can I have the source code??
    sanvittore72@gmail.com

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete

Post a Comment

Popular posts

Android Sqlite and ListView Example

This is simple application which insert data into Sqlite database --and shows the data  from the database in a ListView ListView is not used in android Anymore . We use RecyclerView and CardView   in Android RecyclerView Demo is available on the following link http://androidtuts4u.blogspot.in/2017/04/android-recyclerview-example.html RecyclerView with CardView Demo is available on the following link http://androidtuts4u.blogspot.in/2017/09/android-card-view-example.html This  is the first Activity of application which shows data from database in listview. Register here buton will start a Registration Activity. Submit button will add data to database and show it in the ListView of MainActivity. Update can be performed by clicking ListView items.     you can download the source code of this project from  google drive   https://drive.google.com/folderview?id=0BySLpWhqmbbdS0dtT1R2TXdBWEE&usp=sharing click on the abov...

Android List View using Custom Adapter and SQLite

following is a simple applicaton to create ListView using  Custom adapter.screenshot of the application  is like this . ListView is not used in android Anymore . We use  RecyclerView  and  CardView   in Android RecyclerView Demo is available on the following link http://androidtuts4u.blogspot.in/2017/04/android-recyclerview-example.html RecyclerView with Cardview Example is available on the following link http://androidtuts4u.blogspot.in/2017/09/android-card-view-example.html The ListView below the submit button is populated using Custom Adapter.Data is stored and retrieved using SQLite databsase. you can download the source code of this project from  google drive   https://drive.google.com/folderview?id=0BySLpWhqmbbdUXE5aTNhazludjQ&usp=sharing click on the above link ->sign into  your  google account ->add this to your google drive -> open it in google drive and download it. To create a simple ...

Controlling Back button Behavior in Fragment-- kotlin

when user navigates in an application android maintains back stack of destinations . This allows android to go back to previous destination when user clicks back button . some  times we  need to implement our own back button behavior  for best user experience . For example  if we are in a timer based quiz  application . when the user clicks on the back button if we go back to the previous destination without stopping  the counter , it will crash  the application . So first we need to know whether the  user  pressed the back button accidentally or not by  using a dialogue box . If the user want to go back to previous destination  we need to stop the counter and  then go back to the previous destination. We can control the back button hahavior byusing OnBackPressedDispatcher  . This controls how back button events are dispatched to one or more onBackPressedCallback objects . callbacks are added using addCallback methods. e...