I have design a Drawer Layout which contains some options on a list(basically menu name and an icon). But i am not able to make the list view on the vertical center of the Drawer Layout. Here is my total xml layout:
<?xml version="1.0" encoding="utf-8"?>
<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
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/header" />
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/pink_header"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="@+id/tot_detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Talk of Town Details"
android:textColor="#222222"
android:textSize="14sp"
android:textStyle="bold"
android:typeface="sans" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@id/feature_detail_title"
android:gravity="right"
android:orientation="horizontal" >
<Button
android:id="@+id/drawer_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_drawer"
android:visibility="visible" />
</LinearLayout>
</RelativeLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tot_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:textColor="#222222"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="sans" />
<ImageView
android:id="@+id/image_tot_detail"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@drawable/image_frame"
android:padding="1dip"
android:scaleType="fitXY" />
<TextView
android:id="@+id/tot_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textColor="#222222"
android:textSize="14sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/tot_loc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:textColor="#222222"
android:textSize="14sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/tot_Desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Description:"
android:textColor="#0C7DAF"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/tot_Desc_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textColor="#222222"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="fill_parent"
>
</FrameLayout>
<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="fill_parent"
android:layout_gravity="left|center_vertical"
android:choiceMode="singleChoice"
android:background="#343434"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector_drawer"
android:divider="#ffffff"
/>
</android.support.v4.widget.DrawerLayout>
Here is the drawer layout java code:
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.left_drawer);
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)));
if(strTableBook.equals("yes"))
{
// Table Booking
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
}
if(strGuestList.equals("yes"))
{
// Guest List
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
}
if(strBuyNow.equals("yes"))
{
//Buy now
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
}
Log.d("Testing", "here is 11111");
// Recycle the typed array
navMenuIcons.recycle();
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
Log.d("Testing", "here is 22222");
// enabling action bar app icon and behaving it as toggle button
// getActionBar().setDisplayHomeAsUpEnabled(true);
// getActionBar().setHomeButtonEnabled(true);
Log.d("Testing", "here is 33333");
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();
}
};
Log.d("Testing", "here is 444444");
mDrawerLayout.setDrawerListener(mDrawerToggle);
And here is the click event for the Drawer List Item:
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
displayView(position);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
It is coming like this:
Can anyone help me to make it on center.
Thanks..
The NavigationView is the “real” menu of our app. The menu items are written in nav_items file. Let’s we discuss some important methods of Navigation Drawer that may be called in order to manage the Navigation Drawer. 1. closeDrawer (int gravity): This method is used to close the drawer view by animating it into view.
In Android, DrawerLayout acts as top level container for window content that allows for interactive “drawer” views to be pulled out from one or both vertical edges of the window.
LinearLayout The LinearLayout has a gravity property which supports centering it’s child views. You can specify the gravity as center_horizontal, center_vertical and center (which centers both horizontally and vertically).
The FrameLayout has no gravity property. However, you can specify the layout_gravity of a child view inside a FrameLayout. Centering within this ViewGroup is completely different, because you can only specify contraints. For centering a TextView within a ContraintLayout horizontally you could do the following: android:text="Hello World!"
your ListView height is match_parent so setting layout_gravity to center_vertical wont make a difference try to set gravity center_vertical instead that will make listview content vertically centered.
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="fill_parent"
android:layout_gravity="left|center_vertical"
android:gravity="center_vertical"
android:choiceMode="singleChoice"
android:background="#343434"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector_drawer"
android:divider="#ffffff"
/>
This should work but if not try this:
<LinearLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#343434" >
<ListView
android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#343434"
android:choiceMode="singleChoice"
android:divider="#ffffff"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector_drawer" />
</LinearLayout>
Change:
mDrawerLayout.closeDrawer(mDrawerList);
to:
mDrawerLayout.closeDrawer(Gravity.LEFT);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With