How to wrap DrawerLayout around PreferenceActivity in an Android app?
Asked Answered
R

1

7

I am writing an Android app to take advantage of Drawer Layout functionality. I want to have an ability to use Drawer Layout as a navigational tool available throughout the app. To use the Drawer Layout, I am using the following in my activity layout xml files:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view -->
    <LinearLayout
        android:id="@+id/main_content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->
    <ListView android:id="@+id/navigational_drawer"
        android:layout_width="220dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#FFFFFF"/>
</android.support.v4.widget.DrawerLayout>

However, I am having a lot of difficulties wrapping DrawerLayout around the PreferenceActivity. PreferenceActivity is a special sort of activity that I'd like to preserve because it helps me to maintain the look and feel of the Android preferences in my app; that being said, it uses addPreferencesFromResource() calls to load preferences and does not use addContentView() calls to load XML files; the preference resources typically contain PreferenceScreen tags that don't like to be wrapped in DrawerLayouts.

Anybody had to deal with that issue and how did you solve it? If you have PreferenceActivity in your Android app and you have DrawerLayout available on the same activity, please share how did you do it. Any help would be much appreciated.

EDITED: If I change

<!-- The main content view -->
<LinearLayout
    android:id="@+id/main_content_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

to

<ListView 
     android:id="@android:id/list"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" />

then the app doesn't crash when PreferenceActivity is loaded (because the id "list" matches what the Preference Activity is expecting to find); however, the Drawer is not showing. It's almost as if everything below the ListView "list" is getting ignored.

Repossess answered 26/5, 2013 at 18:58 Comment(1)
I also want to preserve my existing activities and add the drawer dynamically. Not because of PreferenceActivity, but because there are nearly 100 activities and changing them all to fragments would take a while. I was able to do this with HorizontalScrollView, but I feel the DrawerLayout would be much cleaner.Halfback
S
2

This one took me a while, but I was able to make this work for my app. I don't know if it makes a difference, but I am using HoloEverywhere, so I only assume this will work on the standard PreferenceActivity as well.

In my PreferenceActivity when I overrode all versions of setContentView() to load my custom DrawerLayout which is like yours and has a ListView with the standard android id of list. In the last method I made sure to call onContentChanged() after I called super.setContentView():

@Override
public void setContentView(int layoutResID)
    {
    setContentView( getLayoutInflater().inflate( layoutResID ) );
    }

@Override
public void setContentView(View view)
    {
    setContentView( view, new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT ) );
    }

@Override
public void setContentView(View view, LayoutParams params)
    {

    // inflate the drawer layout

    super.setContentView( R.layout.navigation_drawer_base_layout );

    DrawerLayout dl = findViewById(R.id.drawer_layout);

    // do stuff here initialize the DrawerLayout like add a DrawerToggle, etc

    ...STUFF

    // Call onContentsChanged() to let the Activity know it needs to refresh itself.

    onContentChanged();

    }

In onBuildHeaders() I still call loadHeadersFromResource() to create all the Headers for my different PreferenceScreens.

Sharanshard answered 24/9, 2013 at 20:1 Comment(2)
Thank you Jon. I've long since moved on and implemented my own Activity to mimic PreferenceActivity look and feel. I'll mark your reply as "answered".Repossess
@jon i implement the same code as u write here, but its not working, can you help me plz, here is my question: #37630355Graduation

© 2022 - 2024 — McMap. All rights reserved.