10

Hello guys I'm trying to change the height of my Navigation Drawer items and I don't know how, I've looked everywhere and did my best but nothing seems to work..

Result

This is my Navigation Drawer item which is customised to have a text under that image but also I want that image to be a little bigger.

The result I am looking up to is this:

Menu Items

With around 75dp of height for each item and also I want the menu to be shorter on the width to give that look in that image.

I am using a custom layout for my items of course, which I named: menu_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_margin="10dp">

    <ImageView
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:src="@drawable/menu_icon"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Menu Title"
        android:textSize="16sp"
        android:textColor="@color/white" />

</LinearLayout>

my activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/colorPrimaryDark"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

and finally the menu; activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto" >

        <item
            android:id="@+id/nav_profile"
            app:actionLayout="@layout/menu_item"/>
</menu>

Any ideas how to achieve what I want? Thanks in advance

EDIT: When I enlarge the image in my custom item as follows:

this is in menu_item.xml

<ImageView
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:src="@drawable/menu_icon"/>

..I get this result:

result2

As you see my image is bigger than the item..

siriuseteor77
  • 1,004
  • 2
  • 12
  • 30

2 Answers2

39

add this to your styles.xml

<style name="NavigationTheme" parent="AppTheme">
        <item name="listPreferredItemHeightSmall">60dp</item><!-- menu item height- vary as u need -->
    </style>

and your navigation view in drawer layout shd include that theme lik this

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:theme="@style/NavigationTheme"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
Moulesh
  • 2,762
  • 1
  • 22
  • 32
  • this is working perfectly, thank you, one more thing please, how do I center the item because it stays on the right side.. – siriuseteor77 Jun 20 '17 at 18:42
  • for that you have to modify you item xml, make linear layout width match parent & and make layout gravity of text & image view to center horizontal – Moulesh Jun 21 '17 at 05:16
  • 1
    Can u post the xml code... Or post it as another qsn n share the link.. will check that.. – Moulesh Jun 22 '17 at 04:44
  • same as in the question, my custom layout is the first code: *menu_item.xml* i only removed the `gravity` and `margins` – siriuseteor77 Jun 22 '17 at 04:55
  • If it is same as in the question it wont work... I told you to change width of parent and change the gravity of child views – Moulesh Jun 22 '17 at 05:11
  • I did that and it didnt work. Look at the menu item maybe I shouldnt make it as an actionLayout? – siriuseteor77 Jun 22 '17 at 05:13
  • i created a question: https://stackoverflow.com/questions/44690957/horizontal-center-items-inside-navigationdrawer – siriuseteor77 Jun 22 '17 at 05:38
2

make custom layout

 <android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

    <include layout="@layout/custom_view"/>

</android.support.design.widget.NavigationView>
Aashutosh Kumar
  • 183
  • 4
  • 12
  • Could you explain more? I tried to do that and included my `menu_item.xml` inside the NavigationView, the result is the same – siriuseteor77 Jun 20 '17 at 05:46
  • make a custom_view for menu items and simply get each item in java file by findViewById and set click listner on each item – Aashutosh Kumar Jun 20 '17 at 05:51
  • thank you it worked but it was appearing over the header of the navigation menu and in the left side of the menu, i want it in the center – siriuseteor77 Jun 20 '17 at 18:42