Download Code

Android Studio Preference Example

In android studio preference example you will learn how to use preference in android application.Let me give you some introduction about preferences.

Preference

Mostly android applications have setting option which includes push notification enable or disable,sound enable or disable.So for that you can use Preferences API which is most common feature in android application to save user settings.

Ok let’s start create android studio preference example application project.

Next go to res directory of your android application project.Right click and create new folder named it as ‘xml’ make sure it should be inside ‘res’ directory.

Next create new xml file in ‘xml’ directory.Right click New > XML Resource File then add three elements like edit text,list and a check box.

Android Studio Preference,android studio preference example,preference example,preference,

Three attribute you have just used.

  • key( refering id ).
  • title( Title of Element ).
  • summary( Description what Element have).

Now next you will list items that you haven’t added yet.So for that go ahead inside res > values.Right click and create new XML Resource File named ‘array.xml’.

Go into Text view of that newly created layout.Add array of two types options and option values.

Android Studio Preference,android studio preference example,preference example,preference,

Next you will attach these things into preferences layout so for that go to your codec.xml layout and within the list preferences add these two lines for entries and entryvalues.Remeber both should be present if any one of them is absent your application will crashed.

Android Studio Preference,android studio preference example,preference example,preference,

At last with simple display of user settings called preferences you will now deal with MainActivity.java class.So, go to MainActivity.java class and do the following steps.

  • Extends MainActivity with PreferenceActivity class.
  • Set Preference from Resource using addPreferenceFromResource() just like setContentView().

MainActivity.java

package com.example.anonymous.studiopreference;

import android.content.SharedPreferences;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends PreferenceActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.codec);   // preferenceActivity method where we refer preference layout
       
    }


}

Now run your android studio preference example application project.

Android Studio Preference,android studio preference example,preference example,preference,

Next you will see how to read data from Preferences using SharedPreferences and Preferences Manager.

Shared Preferences

Shared preferences basically used to access and modify preferences for user setting or according to the user setting.

Preference Manager

Preference Manager to handle access or anything thing else.It handle how to create preference hierarchies from activities.

So you will use shared preferences variable to access preferences by using Preference Manager and then display in a Toast.

MainActivity.java

package com.example.anonymous.studiopreference;

import android.content.SharedPreferences;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends PreferenceActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.codec);   // preferenceActivity method where we refer preference layout
        // d variable to access preferences
        SharedPreferences d = PreferenceManager.getDefaultSharedPreferences(getBaseContext());  // getting access from Preference manager
        String data = d.getString("name", "Android Codec");
        Toast.makeText(getApplicationContext(), data, Toast.LENGTH_SHORT).show();
    }


}

Again run your android application project.What you will see that you entered something in edit-text. Whenever you run your application back it will display a toast of edit-text stored text.

That’s it for this tutorial.

Thanks.