miércoles, 26 de febrero de 2014

Android - First Steps V

Preferences

Create XML file

  1. Create am Android Xml File 
  2. In Resource Type, select Preference
  3. In Root Element, select PreferenceScreen

Create a class

  1. Create a class that extends from PreferenceActivity.
  2. Override the method onCreate.
  3. 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.
if (savedInstanceState == null) {
            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);
        }
    }; 

  1. asda
     
     
     

No hay comentarios:

Publicar un comentario