Preferences
Create XML file
- Create am Android Xml File
- In Resource Type, select Preference
- In Root Element, select PreferenceScreen
Create a class
- Create a class that extends from PreferenceActivity.
- Override the method onCreate.
- Load Preference file
- Use the method addPreferencesFromResource(R.xml.name_file);. //This method is deprecated in Android version after HoneyComb (Build.VERSION_CODES.HONEYCOMB).
To knkow the version use: Build.VERSION.SDK_INT - For later version after Honeycomb you need to use a FragmentTransaction and Fragment object, where Fragment receive an inner class that extends from PreferenceFragment.
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
Fragment fragment = new MyPreferencesFragment();
transaction.replace(android.R.id.content, fragment);
transaction.commit();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class MyPreferencesFragment extends PreferenceFragment {
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.d("F", "I'm attached to an activity - I have a context!");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.addPreferencesFromResource(R.xml.penguin_prefs);
return super.onCreateView(inflater, container, savedInstanceState);
}
};
- asda
No hay comentarios:
Publicar un comentario