Suggested Pages

Wednesday, January 18, 2017

Android Preferences Example


Preference Fragment Example



package com.example.myapp.preference;

import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;

import com.example.myapp.R;


public class MyPreferenceFragment extends PreferenceFragment {

 
     @Override
     public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       PreferenceManager manager = getPreferenceManager();
       manager.setSharedPreferencesName("Preference");
       addPreferencesFromResource(R.xml.settings);
     }

     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
     }
}


res/xml/settings.xml


settings.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="@string/pref_user_profile">
       
        <com.example.myapp.activity.preference.NamePreference
            android:key="prefUserName"
            android:summary="@string/pref_user_name_summary"
            android:title="@string/pref_user_name_title" 
            />
        
    </PreferenceCategory>
   

</PreferenceScreen>




No comments :

Post a Comment

Suggested Pages