JOHN MORGAN
JOHN MORGAN

Reputation: 113

Android Navigation View Custom width

Is there any way to give custom width to the Android navigation view, I am using Android studio. I tried it a different way by googling, but no luck, please help me.

Is it best practice to give custom width, when dealing with different screen sizes?

Here is my xml code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/top_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".Dashbored">

<include
    android:id="@+id/tool_bar"
    layout="@layout/tool_bar">

</include>


<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/tool_bar"
    android:elevation="7dp"
    android:fitsSystemWindows="true"
    tools:openDrawer="end">


    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

        </FrameLayout>


    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="#0284fe"
        android:fitsSystemWindows="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include
                android:id="@+id/navheader"
                layout="@layout/nav_header" />

            <ListView
                android:id="@+id/lst_menu_items"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:divider="@null"
                android:dividerHeight="0dp"

                />
        </LinearLayout>

    </android.support.design.widget.NavigationView>


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

Thanks

Upvotes: 1

Views: 6459

Answers (2)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21736

You can change NavigationView width using attribute android:layout_width="CUSTOM_WIDTH"

If you want to show same width for all devices then you can use android:maxWidth="CUSTOM_WIDTH" with android:layout_width="wrap_content"

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:maxWidth="200dp"
    android:layout_gravity="end"
    android:background="#0284fe"
    android:fitsSystemWindows="true">

FYI, its better to define width value 200dp to resolution specific dimens.xml file.

Hope this will help~

Upvotes: 11

bhaumiksoni
bhaumiksoni

Reputation: 244

try

android:maxWidth="250dp" // you should define width in dimens.xml file instead of hardcode it

Upvotes: 1

Related Questions