Skip to content

Instantly share code, notes, and snippets.

@cbeyls
Last active April 6, 2023 09:07
Show Gist options
  • Select an option

  • Save cbeyls/7475726 to your computer and use it in GitHub Desktop.

Select an option

Save cbeyls/7475726 to your computer and use it in GitHub Desktop.

Revisions

  1. cbeyls revised this gist Dec 12, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion PreferenceFragment.java
    Original file line number Diff line number Diff line change
    @@ -68,9 +68,11 @@ public void onCreate(Bundle savedInstanceState) {
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState) {
    ListView listView = new ListView(getActivity());
    listView.setId(android.R.id.list);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) && (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)) {
    final int horizontalPadding = (int) (HC_HORIZONTAL_PADDING * getResources().getDisplayMetrics().density + 0.5f);
    listView.setPadding(horizontalPadding, 0, horizontalPadding, 0);
    listView.setClipToPadding(false);
    listView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
    }
    return listView;
    }
  2. cbeyls revised this gist Dec 12, 2016. 1 changed file with 9 additions and 10 deletions.
    19 changes: 9 additions & 10 deletions PreferenceFragment.java
    Original file line number Diff line number Diff line change
    @@ -22,29 +22,28 @@

    /**
    * A PreferenceFragment for the support library. Based on the platform's code with some removed features and a basic ListView layout.
    *
    *
    * @author Christophe Beyls
    *
    */
    public abstract class PreferenceFragment extends Fragment {

    private static final int FIRST_REQUEST_CODE = 100;
    static final int MSG_BIND_PREFERENCES = 1;
    static final int MSG_REQUEST_FOCUS = 2;
    private static final String PREFERENCES_TAG = "android:preferences";
    private static final float HC_HORIZONTAL_PADDING = 16;
    private static final float HC_HORIZONTAL_PADDING = 16f;

    @SuppressLint("HandlerLeak")
    private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
    switch (msg.what) {
    case MSG_BIND_PREFERENCES:
    bindPreferences();
    break;
    case MSG_REQUEST_FOCUS:
    mList.focusableViewAvailable(mList);
    break;
    case MSG_BIND_PREFERENCES:
    bindPreferences();
    break;
    case MSG_REQUEST_FOCUS:
    mList.focusableViewAvailable(mList);
    break;
    }
    }
    };
    @@ -70,7 +69,7 @@ public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bun
    ListView listView = new ListView(getActivity());
    listView.setId(android.R.id.list);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    final int horizontalPadding = (int) (HC_HORIZONTAL_PADDING * getResources().getDisplayMetrics().density);
    final int horizontalPadding = (int) (HC_HORIZONTAL_PADDING * getResources().getDisplayMetrics().density + 0.5f);
    listView.setPadding(horizontalPadding, 0, horizontalPadding, 0);
    }
    return listView;
  3. cbeyls revised this gist Aug 27, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions PreferenceFragment.java
    Original file line number Diff line number Diff line change
    @@ -29,8 +29,8 @@
    public abstract class PreferenceFragment extends Fragment {

    private static final int FIRST_REQUEST_CODE = 100;
    private static final int MSG_BIND_PREFERENCES = 1;
    private static final int MSG_REQUEST_FOCUS = 2;
    static final int MSG_BIND_PREFERENCES = 1;
    static final int MSG_REQUEST_FOCUS = 2;
    private static final String PREFERENCES_TAG = "android:preferences";
    private static final float HC_HORIZONTAL_PADDING = 16;

    @@ -51,7 +51,7 @@ public void handleMessage(Message msg) {

    private boolean mHavePrefs;
    private boolean mInitDone;
    private ListView mList;
    ListView mList;
    private PreferenceManager mPreferenceManager;

    @Override
    @@ -213,7 +213,7 @@ private void postBindPreferences() {
    }
    }

    private void bindPreferences() {
    void bindPreferences() {
    final PreferenceScreen preferenceScreen = getPreferenceScreen();
    if (preferenceScreen != null) {
    preferenceScreen.bind(getListView());
  4. cbeyls revised this gist Dec 20, 2014. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions PreferenceFragment.java
    Original file line number Diff line number Diff line change
    @@ -61,7 +61,7 @@ public void onCreate(Bundle savedInstanceState) {
    Constructor<PreferenceManager> c = PreferenceManager.class.getDeclaredConstructor(Activity.class, int.class);
    c.setAccessible(true);
    mPreferenceManager = c.newInstance(this.getActivity(), FIRST_REQUEST_CODE);
    } catch (Exception e) {
    } catch (Exception ignored) {
    }
    }

    @@ -103,7 +103,7 @@ public void onStop() {
    Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityStop");
    m.setAccessible(true);
    m.invoke(mPreferenceManager);
    } catch (Exception e) {
    } catch (Exception ignored) {
    }
    }

    @@ -119,7 +119,7 @@ public void onDestroy() {
    Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityDestroy");
    m.setAccessible(true);
    m.invoke(mPreferenceManager);
    } catch (Exception e) {
    } catch (Exception ignored) {
    }
    }

    @@ -139,7 +139,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityResult", int.class, int.class, Intent.class);
    m.setAccessible(true);
    m.invoke(mPreferenceManager, requestCode, resultCode, data);
    } catch (Exception e) {
    } catch (Exception ignored) {
    }
    }

    @@ -158,7 +158,7 @@ public void setPreferenceScreen(PreferenceScreen screen) {
    postBindPreferences();
    }
    }
    } catch (Exception e) {
    } catch (Exception ignored) {
    }
    }

    @@ -179,7 +179,7 @@ public void addPreferencesFromIntent(Intent intent) {
    m.setAccessible(true);
    PreferenceScreen screen = (PreferenceScreen) m.invoke(mPreferenceManager, intent, getPreferenceScreen());
    setPreferenceScreen(screen);
    } catch (Exception e) {
    } catch (Exception ignored) {
    }
    }

    @@ -190,7 +190,7 @@ public void addPreferencesFromResource(int resId) {
    m.setAccessible(true);
    PreferenceScreen screen = (PreferenceScreen) m.invoke(mPreferenceManager, getActivity(), resId, getPreferenceScreen());
    setPreferenceScreen(screen);
    } catch (Exception e) {
    } catch (Exception ignored) {
    }
    }

  5. cbeyls revised this gist Dec 20, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion PreferenceFragment.java
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@
    import android.widget.ListView;

    /**
    * A PreferenceFragment for the support library. Based on the platform's code with a some removed features and a basic ListView layout.
    * A PreferenceFragment for the support library. Based on the platform's code with some removed features and a basic ListView layout.
    *
    * @author Christophe Beyls
    *
  6. cbeyls revised this gist Dec 20, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions PreferenceFragment.java
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@
    import android.widget.ListView;

    /**
    * A PreferenceFragment for the support library. Based on the platform's code with some removed features and a basic ListView layout.
    * A PreferenceFragment for the support library. Based on the platform's code with a some removed features and a basic ListView layout.
    *
    * @author Christophe Beyls
    *
    @@ -234,13 +234,13 @@ private void ensureList() {
    throw new IllegalStateException("Content view not yet created");
    }
    View rawListView = root.findViewById(android.R.id.list);
    if (rawListView == null) {
    throw new RuntimeException("Your content must have a ListView whose id attribute is 'android.R.id.list'");
    }
    if (!(rawListView instanceof ListView)) {
    throw new RuntimeException("Content has view with id attribute 'android.R.id.list' that is not a ListView class");
    }
    mList = (ListView) rawListView;
    if (mList == null) {
    throw new RuntimeException("Your content must have a ListView whose id attribute is 'android.R.id.list'");
    }
    mHandler.sendEmptyMessage(MSG_REQUEST_FOCUS);
    }
    }
  7. cbeyls renamed this gist Nov 14, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. cbeyls created this gist Nov 14, 2013.
    246 changes: 246 additions & 0 deletions SupportPreferenceFragment
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,246 @@
    package android.support.v4.preference;

    import java.lang.reflect.Constructor;
    import java.lang.reflect.Method;

    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Build;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.preference.Preference;
    import android.preference.PreferenceManager;
    import android.preference.PreferenceScreen;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ListView;

    /**
    * A PreferenceFragment for the support library. Based on the platform's code with some removed features and a basic ListView layout.
    *
    * @author Christophe Beyls
    *
    */
    public abstract class PreferenceFragment extends Fragment {

    private static final int FIRST_REQUEST_CODE = 100;
    private static final int MSG_BIND_PREFERENCES = 1;
    private static final int MSG_REQUEST_FOCUS = 2;
    private static final String PREFERENCES_TAG = "android:preferences";
    private static final float HC_HORIZONTAL_PADDING = 16;

    @SuppressLint("HandlerLeak")
    private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
    switch (msg.what) {
    case MSG_BIND_PREFERENCES:
    bindPreferences();
    break;
    case MSG_REQUEST_FOCUS:
    mList.focusableViewAvailable(mList);
    break;
    }
    }
    };

    private boolean mHavePrefs;
    private boolean mInitDone;
    private ListView mList;
    private PreferenceManager mPreferenceManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
    Constructor<PreferenceManager> c = PreferenceManager.class.getDeclaredConstructor(Activity.class, int.class);
    c.setAccessible(true);
    mPreferenceManager = c.newInstance(this.getActivity(), FIRST_REQUEST_CODE);
    } catch (Exception e) {
    }
    }

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState) {
    ListView listView = new ListView(getActivity());
    listView.setId(android.R.id.list);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    final int horizontalPadding = (int) (HC_HORIZONTAL_PADDING * getResources().getDisplayMetrics().density);
    listView.setPadding(horizontalPadding, 0, horizontalPadding, 0);
    }
    return listView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (mHavePrefs) {
    bindPreferences();
    }

    mInitDone = true;

    if (savedInstanceState != null) {
    Bundle container = savedInstanceState.getBundle(PREFERENCES_TAG);
    if (container != null) {
    final PreferenceScreen preferenceScreen = getPreferenceScreen();
    if (preferenceScreen != null) {
    preferenceScreen.restoreHierarchyState(container);
    }
    }
    }
    }

    public void onStop() {
    super.onStop();
    try {
    Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityStop");
    m.setAccessible(true);
    m.invoke(mPreferenceManager);
    } catch (Exception e) {
    }
    }

    public void onDestroyView() {
    mList = null;
    mHandler.removeCallbacksAndMessages(null);
    super.onDestroyView();
    }

    public void onDestroy() {
    super.onDestroy();
    try {
    Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityDestroy");
    m.setAccessible(true);
    m.invoke(mPreferenceManager);
    } catch (Exception e) {
    }
    }

    public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    PreferenceScreen preferenceScreen = getPreferenceScreen();
    if (preferenceScreen != null) {
    Bundle container = new Bundle();
    preferenceScreen.saveHierarchyState(container);
    outState.putBundle(PREFERENCES_TAG, container);
    }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
    Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityResult", int.class, int.class, Intent.class);
    m.setAccessible(true);
    m.invoke(mPreferenceManager, requestCode, resultCode, data);
    } catch (Exception e) {
    }
    }

    public PreferenceManager getPreferenceManager() {
    return mPreferenceManager;
    }

    public void setPreferenceScreen(PreferenceScreen screen) {
    try {
    Method m = PreferenceManager.class.getDeclaredMethod("setPreferences", PreferenceScreen.class);
    m.setAccessible(true);
    boolean result = (Boolean) m.invoke(mPreferenceManager, screen);
    if (result && (screen != null)) {
    mHavePrefs = true;
    if (mInitDone) {
    postBindPreferences();
    }
    }
    } catch (Exception e) {
    }
    }

    public PreferenceScreen getPreferenceScreen() {
    try {
    Method m = PreferenceManager.class.getDeclaredMethod("getPreferenceScreen");
    m.setAccessible(true);
    return (PreferenceScreen) m.invoke(mPreferenceManager);
    } catch (Exception e) {
    return null;
    }
    }

    public void addPreferencesFromIntent(Intent intent) {
    requirePreferenceManager();
    try {
    Method m = PreferenceManager.class.getDeclaredMethod("inflateFromIntent", Intent.class, PreferenceScreen.class);
    m.setAccessible(true);
    PreferenceScreen screen = (PreferenceScreen) m.invoke(mPreferenceManager, intent, getPreferenceScreen());
    setPreferenceScreen(screen);
    } catch (Exception e) {
    }
    }

    public void addPreferencesFromResource(int resId) {
    requirePreferenceManager();
    try {
    Method m = PreferenceManager.class.getDeclaredMethod("inflateFromResource", Context.class, int.class, PreferenceScreen.class);
    m.setAccessible(true);
    PreferenceScreen screen = (PreferenceScreen) m.invoke(mPreferenceManager, getActivity(), resId, getPreferenceScreen());
    setPreferenceScreen(screen);
    } catch (Exception e) {
    }
    }

    public Preference findPreference(CharSequence key) {
    if (mPreferenceManager == null) {
    return null;
    }
    return mPreferenceManager.findPreference(key);
    }

    private void requirePreferenceManager() {
    if (this.mPreferenceManager == null) {
    throw new RuntimeException("This should be called after super.onCreate.");
    }
    }

    private void postBindPreferences() {
    if (!mHandler.hasMessages(MSG_BIND_PREFERENCES)) {
    mHandler.sendEmptyMessage(MSG_BIND_PREFERENCES);
    }
    }

    private void bindPreferences() {
    final PreferenceScreen preferenceScreen = getPreferenceScreen();
    if (preferenceScreen != null) {
    preferenceScreen.bind(getListView());
    }
    }

    public ListView getListView() {
    ensureList();
    return mList;
    }

    private void ensureList() {
    if (mList != null) {
    return;
    }
    View root = getView();
    if (root == null) {
    throw new IllegalStateException("Content view not yet created");
    }
    View rawListView = root.findViewById(android.R.id.list);
    if (!(rawListView instanceof ListView)) {
    throw new RuntimeException("Content has view with id attribute 'android.R.id.list' that is not a ListView class");
    }
    mList = (ListView) rawListView;
    if (mList == null) {
    throw new RuntimeException("Your content must have a ListView whose id attribute is 'android.R.id.list'");
    }
    mHandler.sendEmptyMessage(MSG_REQUEST_FOCUS);
    }
    }