Android : Android LinearLayout in a Navigation Drawer

on Saturday, July 5, 2014


My goal is to create a "title" in the Navigation Drawer, below which is a list of items, like this: img


I created a default Navigation Drawer activity in Android Studio using the New > Activity > Navigation Drawer Activity and it generated all of the boilerplate for me.


The only thing I've changed is the Navigation Drawer fragment. I put a TextView above the ListView, both of which are in a LinearLayout.


I then changed the generated Java fragment code to inflate the LinearLayout instead of the ListView. However, this crashes my app.


I tried following this: How to add title in Navigation drawer layout?


I'm new to Android so any help is appreciated.


Here is the XML from the Navigation Drawer fragment (fragment_navigation_drawer.xml):



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.me.myapp.NavigationDrawerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/usernameTitle"
android:text="TEST" />

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#cccc"
android:id="@+id/playerList" />

</LinearLayout>


Here is (almost) all of the code I changed from the generated code:



...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// This is declared as a LinearLayout at the top of the file
mDrawerListView = (LinearLayout) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerInnerListView = (ListView) getView().findViewById(R.id.playerList);
mDrawerInnerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
mDrawerInnerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
new String[] {
preferences.getString("username", "Couldn't find username"),
getString(R.string.title_section2),
getString(R.string.title_section3),
"test",

}));
mDrawerInnerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}

...

private void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerInnerListView != null) {
mDrawerInnerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
if (mCallbacks != null) {
mCallbacks.onNavigationDrawerItemSelected(position);
}
}


Essentially I just inflated the LinearLayout instead of the ListView, leaving everything else to act on the ListView.


Full Java here: https://gist.github.com/25cf/90c629e884fc3c121c67


Finally, here's the stacktrace:


https://gist.github.com/25cf/3c5e84561fdc2f153607


3 comments:

Muhammad Maqsood said...

Instead of this ...
mDrawerListView = (LinearLayout) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerInnerListView = (ListView) getView().findViewById(R.id.playerList);
use this to get Listview that works fine for me

mDrawerListView = (LinearLayout) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerInnerListView = (ListView) mDrawerListView.findViewById(R.id.playerList);

Muhammad Maqsood said...

And thanx for above post...

Unknown said...

Hi ,

I turned "TextView" into "include a linearlayout", but got a "null pointer" error when onCreateView @ mDrawerInnerListView = (ListView) getView().findViewById(R.id.playerList);

And i follow all have to change by your code @java, xml.

How to solve this? Thanks!!

Post a Comment