Reputation: 1352
I have a navigation drawer from here: reference link
I want to add "settings" and "exit" at the end of the navigation list but I can't.
Here is my code for drawer activity_main.xml
:
`
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="@color/list_background"/>
</android.support.v4.widget.DrawerLayout>`
I tried adding text to the end of the ListView and wrap them in RelativeLayout with aligning to the bottom. But no success so far.
This is my main activity:
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Communities, Will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
// Pages
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
// What's hot, We will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
}
...
I just want to add another row at the end of the list in navigation drawer separated from the rest of the list. something like this:
Upvotes: 7
Views: 10497
Reputation: 601
For the people who is still looking for a complete answer. Here is a few points.
1) if you add a linearlayout or relativelayout as a container, don't forget marking the container layout as start - this is key to let drawerlayout treats the container as a sliding drawer, or you will receive an error.
<RelativeLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
>
2) declare a global variable for this container layout, get a reference to it in onCreate(), something like :
RelativeLayout mRelativeLayout;
public void onCreate(){
mRelativeLayout = ..
}
3) there are at least 3 places you need to use the relativelayout.
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
mDrawerLayout.close(mRelativeLayout);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
mDrawerLayout.open(mRelativeLayout);
invalidateOptionsMenu();
}
};
The last one is the selectedItem method call
mDrawerLayout.closeDrawer(mContainerLayout);
Upvotes: 2
Reputation: 167
Late response! But i have just been working on something very similar that solves this issue without modifying the xml at all.
Each drawerlayout has a addHeader(View v) function and an addFooter(View v) function. They accept inflated views as their param.
View footer = getLayoutInflater().inflate(R.layout.comic_drawer_footer, null);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_content);
mDrawerList = (ListView) findViewById(R.id.bookmark_list);
mDrawerList.addFooterView(footer);
Upvotes: 1
Reputation: 10177
DrawLayout takes two children, one main display, the other the drawer, so you have wrap everything in two viewGroups.
The special attributes like gravity start need to be in the relative layout now, since it is now the direct child of DrawerLayout.
.....
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<RelativeLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
>
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_above="@+id/button2"
/>
<Button
android:id="@+id/button2"
android:layout_height="50dp"
android:text = "button2"
android:layout_width="match_parent"
android:layout_above="@+id/button1" />
<Button
android:id="@+id/button1"
android:layout_height="50dp"
android:text = "button1"
android:layout_width="match_parent"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Upvotes: 3
Reputation: 3275
I think You should add drawer list in linear layout. just like below
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/ne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/setting_background"
android:orientation="vertical" >
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_gravity="start"
android:layout_weight=".85"
android:background="@color/list_background"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_alignParentBottom="true"
android:layout_below="@+id/newslist_drawer"
android:layout_weight=".15"
android:orientation="horizontal" >
<Button
android:id="@+id/newslist_done"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.5"
android:text=" setting"
android:visibility="gone" />
<Button
android:id="@+id/newslist_cancel"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.5"
android:text=" exit"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
<android.support.v4.widget.DrawerLayout>
Upvotes: 2