Home » Android » android – See navigation drawer preview

android – See navigation drawer preview

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m designing a native navigation drawer in Android Studio.
I can’t see the drawer in my preview because it is sitting left of the activity, out of range of the preview.
For now I’m using a testlayout.xml file to see my changes, but a lot of times I forget to copy paste them into the right activity.
Is there a way to preview the drawer layout?

How to&Answers:

If you want to be able to preview the drawer layout / navigation bar

If you are following the tutorial at https://developer.android.com/training/implementing-navigation/nav-drawer.html

You can put your listview for the nav drawer in a different xml file in the /layout folder, then include it in the drawer layout xml. Means you can edit it in the drawer_layout file and be able to see it properly

layout/drawer_layout

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/left_drawer"
      android:layout_width="280dp"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      android:choiceMode="singleChoice"
      android:divider="@android:color/transparent"
      android:dividerHeight="0dp"
      android:background="@color/nav_drawer_background_color"/>

Then in the navbar

<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 -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


</FrameLayout>

<!-- The navigation drawer -->
<include
    android:id="@+id/listView"
    layout="@layout/drawer_layout"/>
</android.support.v4.widget.DrawerLayout>

Answer:

To see NavigationView preview add tools namespace to your drawer layout:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

then add attribute tools:openDrawer equal to gravity of layout_gravity value of your Navigation view, for example

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:openDrawer="left">


<android.support.design.widget.NavigationView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    app:headerLayout="@layout/navigation_header_main"
    app:itemTextAppearance="@style/NavigationItemStyleLight"
    app:menu="@menu/navigation_main_menu"/>

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

Answer:

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

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_camera"
        android:icon="@drawable/ic_menu_camera"
        android:title="Import" />
    <item
        android:id="@+id/nav_gallery"
        android:icon="@drawable/ic_menu_gallery"
        android:title="Gallery" />
    <item
        android:id="@+id/nav_slideshow"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="Slideshow" />
    <item
        android:id="@+id/nav_manage"
        android:icon="@drawable/ic_menu_manage"
        android:title="Tools" />
</group>

<item android:title="Communicate">
    <menu>
        <item
            android:id="@+id/nav_share"
            android:icon="@drawable/ic_menu_share"
            android:title="Share" />
        <item
            android:id="@+id/nav_send"
            android:icon="@drawable/ic_menu_send"
            android:title="Send" />
    </menu>
</item>

Just Add

tools:showIn="navigation_view"

Answer:

You can use NavigationView to get its preview on AndroidStudio, Its much better and easier to implement then the old NavigationDrawer.

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/prof_app_bar"
        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:fitsSystemWindows="true"
        app:itemTextColor="@color/text_head_color"
        app:itemIconTint="@null"
        app:headerLayout="@layout/nav_header_main2"
        app:menu="@menu/professional_menu" />
</android.support.v4.widget.DrawerLayout>

Define the menu in navigationmenu.xml place it in menu resource folder:-

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


    <group android:checkableBehavior="single"
        android:id="@+id/top_menu">
        <item
            android:id="@+id/pnav_setting"
            android:icon="@drawable/accountsettings"
            android:title="@string/paccount_settingss" />
        <item
            android:id="@+id/pnav_expertise"
            android:icon="@drawable/professional"
            android:title="@string/pexpertise" />
        <item
            android:id="@+id/pnav_comu"
            android:icon="@drawable/communication"
            android:title="@string/pcomunication" />
        <item
            android:id="@+id/pnav_change_pass"
            android:icon="@drawable/changepasswd"
            android:title="@string/pchange_pass" />
        <item
            android:id="@+id/pnav_change_lang"
            android:icon="@drawable/changelang"
            android:title="@string/pchange_lang"
            android:visible="true"/>
    </group>

    <group android:checkableBehavior="single"
        android:id="@+id/middle_menu">

        <item
            android:id="@+id/pnav_about_us"
            android:icon="@drawable/aboutus"
            android:title="@string/about_us" />
        <item
            android:id="@+id/pnav_feedback"
            android:icon="@drawable/feedback"
            android:title="@string/feedback"
            android:visible="false"/>
        <item
            android:id="@+id/nav_privacy_policy"
            android:icon="@drawable/privacy"
            android:title="@string/privacy_policy" />
        <item
            android:id="@+id/pnav_faq"
            android:icon="@drawable/faq"
            android:visible="false"
            android:title="@string/faq" />
        <item
            android:id="@+id/pnav_contact_us"
            android:icon="@drawable/contactus"
            android:title="@string/contact_us" />

    </group>

    <group android:checkableBehavior="single"
        android:id="@+id/bottom_menu">
        <item
            android:id="@+id/pnav_sign_out"
            android:icon="@drawable/signout"
            android:title="@string/sign_out" />
    </group>



</menu>

And, for getting click events use this:-

 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(yourItemclickListener);