Download Code

Android Preference Example

In android 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 preference example application project.

preference,android preference,android preference example,preferece example,

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.

preference,android preference,android preference example,preferece example,

Next go ahead and create new xml file in ‘xml’ directory.Right click New > Android XML File.But make sure xml should be preference type and select from list PreferenceScreen.Then hit next and Finish.

preference,android preference,android preference example,preferece example,

preference,android preference,android preference example,preferece example,

 

After clicking Finish button your new layout will look like this.

Graphical

preference,android preference,android preference example,preferece example,

XML

preference,android preference,android preference example,preferece example,

ok now from ‘codec.xml’ layout you will add three elements like edit text,list and a check box.Then your xml will look like this.

preference,android preference,android preference example,preferece example,

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 file named ‘array.xml’ with Resource type like this.

preference,android preference,android preference example,preferece example,

Next hit finish.Go into xml view of that newly created layout.Add array of two types options and option values just like this.

preference,android preference,android preference example,preferece example,

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.

preference,android preference,android preference example,preferece example,

Ok now you will deal your MainActivity.java class.You are not concern with your layout>activity_main.xml layout because you are setting a screen for users.So for that your setting screeen activity should look like this from start.

public class MainActivity {
       //code goes here
}

Next extends MainActivity from PreferenceActivity because you are provide user setting.

public class MainActivity extends PreferenceActivity{
       //code goes here
}

after right click on add method onCreate() from activity hierarchy.Then use PreferenceActivity method to display preferences layout to the user.After that your MainActivity.java class will look like this.
MainActivity.java

package androidcodec.preferences;

/*
 * 
 * All Rights Reserved by: Farhan Rasheed
 * 
 * */




public class MainActivity extends PreferenceActivity {

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

   
}

So till here if you run your andorid application project output will look like this.

preference,android preference,android preference example,preferece example,

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.Then your final Code will look like this.

MainActivity.java

package androidcodec.preferences;

/*
 * 
 * All Rights Reserved by: Farhan Rasheed
 * 
 * */

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends PreferenceActivity {

    @SuppressWarnings("deprecation")
	@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();
    }

   
}

That’s it for this android preference example tutorial.
Thanks.