• Advertisement

  • Advertisements

Simple use of Shared Preference Android

Here is a very simple implementation of Shared Preferences in Android Development.

Step 1:   Create a Shared preferences object prefs from file “com.sagar.prefs”.

SharedPreferences prefs = getSharedPreferences(“com.sagar.prefs”, Context.MODE_PRIVATE );// this code is enough for creating the file and getting its object.


Step 2. Write some value in the shared preferences

copy this method in your code:

private void setPreferences(String key_str, String value_str){

           Editor editor = prefs.edit();
           editor.putString(key_str, value_str); 

and use this to save any value in shared preferences  setPreferences(“name”, “value”); // this will save the value for this name.


Step 3: Get data from shared preferences:

String str = prefs.getString(“name”, “d”);  // where d is the default value which will be return if there is no value for the given “name” in the shared preferences.



Android ListView with multiple String arrays using Simple Adapter

Here is a Simple and very Easy tutorial if you want to show more than two lines in a single list item.


  1. Create 2 layouts in res/layout folder. “main.xml” and   “list_item.xml”
  2. Create a listview in main.xml                (code given below)
  3. Create two textview in list_item.xml      (code given below)
  4. Create MainActivity and set main.xml as its layout. (code given below)
  5. Run the application.

source belowSource code:

Continue reading

Android Expandable List Simple example

Android Expandable List Simple example

This is a very important view of Android and it is really complicated but here is a very simple example for the ExpandableListView.

  • Create new Android Project with package name  “com.sagar.expandablelistview.example”.
  • Create Main Activity “ExpandableListExample.java”.
  • You don’t need any layout.xml for this example.
  • Copy the code in your ExpandableListExample activity.

source belowSource code:

Continue reading