0

My problem is that my PreferenceFragment overlaps my toolbar. Since I do not have a layout activity which the fragment comes from, I cannot apply the solutions I have seen elsewhere. I got this far by skipping and deleting the pref_header.xml and everything works except the toolbar.

PreferenceActivity.xml

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new GeneralPreferenceFragment())
            .commit();

}@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class GeneralPreferenceFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_general);
        setHasOptionsMenu(true);

        // Bind the summaries of EditText/List/Dialog/Ringtone preferences
        // to their values. When their values change, their summaries are
        // updated to reflect the new value, per the Android Design
        // guidelines.
        bindPreferenceSummaryToValue(findPreference("bredd"));
        bindPreferenceSummaryToValue(findPreference("höjd"));
        bindPreferenceSummaryToValue(findPreference("vecka"));
        bindPreferenceSummaryToValue(findPreference("klass"));


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            startActivity(new Intent(getActivity(), PreferencesActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    Toolbar bar;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        LinearLayout root = (LinearLayout) findViewById(android.R.id.list).getParent().getParent().getParent();
        bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
        root.addView(bar, 0); // insert at top
    } else {
        ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
        ListView content = (ListView) root.getChildAt(0);

        root.removeAllViews();

        bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);


        int height;
        TypedValue tv = new TypedValue();
        if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
            height = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
        } else {
            height = bar.getHeight();
        }

        content.setPadding(0, height, 0, 0);

        root.addView(content);
        root.addView(bar);
    }
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

}

settings_toolbar.xml

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:navigationContentDescription="@string/abc_action_bar_up_description"
android:background="?attr/colorPrimary"
app:navigationIcon="?attr/homeAsUpIndicator"
app:title="Settings"
android:elevation="4dp"/>

I am aware of the .replace(R.id.whateverlayoutthefragmentcomesfrom) fix, but since it isn't bound to a layout file, I don't know what to do.

Edit: Note that getListView().setPadding(0, 50, 0, 0); and similar lines compile, but do not have any practical effect at all.

Edit 2: SOLUTION Solved by following this thread: link

Community
  • 1
  • 1

1 Answers1

0

You have to set the built in "list" of the preferenceFragment/Activity further down with setPadding

Try the following code:

     //Put this line in onCreate
    getListView().setPadding(0, (int) getTypedValueInDP(50), 0, 0);

   private float getTypedValueInDP(float value) {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, getResources().getDisplayMetrics());
}
  • "Wrong 2nd argument type. Found 'float', required 'int' on getTypedValueInDP. Also what am I suppose to put in place of context? Sorry for noob questions, I am a beginner :) – Martin R. L. Jan 25 '17 at 18:02
  • @MartinR.L. I just edited my answer. Try now. If getResources(); isn't working then try with getActivity().getResources(); –  Jan 25 '17 at 18:24
  • Alright, code compiles fine now, but sadly doesn't have any practical effect at all :/ I've tried similar codes before giving no result either. – Martin R. L. Jan 25 '17 at 18:43