Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top