0

I'm planning to implement a navigation bar to let users navigate to different activites. But here's the problem, I've found plenty articles about creating a navigation drawer but it seems doesn't work for me , because my UI doesn't have any titlebar. And what i actually want is to call up the navigation drawer whenever users press on a button near to the navigation drawer.

Is there any possible way to do this ?

E Heng
  • 91
  • 3
  • 9

2 Answers2

0

Yes you can implement DrawerLayout with out ActionBar. You can manually open and close the DrawerLayout like

    drawerLayout.openDrawer(Gravity.LEFT);

    drawerLayout.closeDrawer(Gravity.LEFT);
Prasad
  • 3,462
  • 1
  • 23
  • 28
0

It is very simple.

Here is your main activity's layout, activity_main.xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:layout_height="match_parent">

    <FrameLayout
        android:animateLayoutChanges="true"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        tools:ignore="MergeRootFrame" />

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_list_header"
        app:menu="@menu/navigation"/>
</android.support.v4.widget.DrawerLayout>

And here's your MainActivity:

public class MainActivity extends AppCompatActivity {

    NavigationView navigationView;
    DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        navigationView = (NavigationView) findViewById(R.id.navigation_view);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.navi_1:
                        // on 1st item in the menu, do something
                        break;
                    case R.id.navi_2:
                        // on 2nd item in the menu, do something
                        break;
                }
                drawerLayout.closeDrawers();
                return false;
            }
        });

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    }

    // [...]

    private void openDrawer() {
        if (!drawerLayout.isDrawerOpen(navigationView)) {
            drawerLayout.openDrawer(navigationView);
        }
    }

    private void closeDrawer() {
        if (drawerLayout.isDrawerOpen(navigationView)) {
            drawerLayout.closeDrawers();
        }
    }
}

Now you can open the drawer with openDrawer() and close it with closeDrawer().

A sample navigation.xml file which is located under the menu dir in the res (resources) folder:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/navi_1"
            android:checked="true"
            android:icon="@drawable/ic_android"
            android:title="First item"/>
        <item
            android:id="@+id/navi_2"
            android:icon="@drawable/ic_android"
            android:title="Second item"/>
    </group>
</menu>

Sample drawer_list_header.xml file, located under the layout dir in the res folder:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
    android:paddingBottom="8dp"
    android:src="@drawable/list_header_final">

</ImageView>

Here are some notes:

  • You have to declare a menu file for the drawer, see app:menu="@menu/navigation" in the layout file.
  • You might want to declare a header layout, which is displayed over the menu elements in the drawer, see app:headerLayout="@layout/drawer_list_header".
  • The drawer can be opened by a fling-like action from the edge of the screen. To prevent the users from doing that, you might want to lock/unlock your drawer on action using drawerLayout.setDrawerLockMode(...);, see the documentation for details.

Also note that in order to use NavigationView, you'll need the latest design support lib by adding the dependency to your module's gradle file: compile 'com.android.support:design:22.2.0'. See more about it here.

Gergely Kőrössy
  • 5,620
  • 3
  • 28
  • 44
  • But my main_activity is already in relative layout , can i just add in the v4 widget code? – E Heng Aug 22 '15 at 04:01
  • The DrawerLayout must be the root, but you can replace the FrameLayout in my code with your existing RelativeLayout, it's gonna have the same look-and-feel but you'll have access to the drawer. – Gergely Kőrössy Aug 22 '15 at 04:02
  • It's available in the support design lib, see [this post's very end](http://android-developers.blogspot.hu/2015/05/android-design-support-library.html) about it. Basically you have to add a dependency to your gradle file: `compile 'com.android.support:design:22.2.0'` – Gergely Kőrössy Aug 22 '15 at 04:16
  • Thanks , may i know what is the latest version of com.android.design dependency library ? – E Heng Aug 22 '15 at 04:20
  • Because it shows "this support library should not use a lower version(22)" – E Heng Aug 22 '15 at 04:22
  • I think it's 22.2.0 as you can see here: `compile 'com.android.support:design:22.2.0'`. You might have to download the latest Android Support Library (v23) in your SDK Manager. The *""this support library should not use a lower version(22)"* text is just a warning which shows that there is a newer version available. Press ALT + ENTER and select to update it, the Android Studio will probably change the numbers to a newer version. – Gergely Kőrössy Aug 22 '15 at 04:23
  • Thanks for this , one more question , the menu items and list should be declare in main_menu.xml or create one xml under layout? because the code shows app:header=@layout – E Heng Aug 22 '15 at 04:35
  • The header is a custom layout file. I recommend using a simple ImageView as root for that if you want to display only an image. The menu file contains the menu items, thus it is placed in the menu dir. Refer to [this post's NavigationView part](http://android-developers.blogspot.hu/2015/05/android-design-support-library.html) if you want to know more. – Gergely Kőrössy Aug 22 '15 at 04:39
  • I Don't really get what you mean , how can i fix this ? – E Heng Aug 22 '15 at 04:45
  • Oops, as well as the menu/navigation . Sorry i'm new to android development. – E Heng Aug 22 '15 at 04:46
  • The list you are displaying in the drawer goes into a single drawer_menu.xml file (this is just a sample name, you can name it whatever you want) which will be located under the menu dir. Then you change `app:menu="@menu/navigation"` to `app:menu="@menu/drawer_menu"`. This drawer_menu.xml contains the menu elements, which is NOT a layout file but a simple menu descriptor. I added a sample code to the answer (**note** that I called it navigation.xml this time, not drawer_menu.xml). – Gergely Kőrössy Aug 22 '15 at 04:50
  • Oh , i got it ! Thank you so much ! – E Heng Aug 22 '15 at 04:53
  • Can i ask one more question? how can i open up the drawer from the right ? http://stackoverflow.com/questions/32155127/navigation-drawer-starts-from-right-side (My question) – E Heng Aug 22 '15 at 10:56
  • I posted my answer there. You have to change the layout_gravity attribute of NavigationView to a more appropriate one. – Gergely Kőrössy Aug 22 '15 at 12:13