0

I am working on an app in android studio. I'm trying to create a navigation drawer that fits right underneath the status bar. For some reason the navigation bar shows up underneath the app bar instead kind of like in this picture:

Navdrawer1

enter image description here

I would like it to look like this:

Navdrawer2

enter image description here

Here is my xml code:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
>


<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:background="#ff4000"
    android:id="@+id/toolbar"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:title="Home"
    />

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_height="match_parent"
    android:layout_gravity="start"

    android:layout_width="match_parent"
    android:id="@+id/drawerLayout"
    >




<FrameLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:id="@+id/containerView">
 </FrameLayout>



<android.support.design.widget.NavigationView

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"

    android:fitsSystemWindows="true"
    android:layout_gravity="start"
    android:background="#FFFFFF"
    app:headerLayout="@layout/navigation_drawer_header"
    android:id="@+id/shitstuff"
    app:itemTextColor="@color/black"
    app:menu="@menu/drawermenu"
    android:layout_marginTop="-24dp"


    />



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

</LinearLayout>

Any help would be great! Thanks!

Mukesh Ram
  • 5,814
  • 4
  • 15
  • 34

3 Answers3

1

Here you need toolbar :

<?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"//fit to top status bar
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_home_actvity"
        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.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

app_bar_home_actvity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" //fit to top status bar
    tools:context=".activity.HomeActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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


</android.support.design.widget.CoordinatorLayout>
ViramP
  • 1,381
  • 9
  • 10
0

You must include your toolbar within the "NavigationView"

Your code would look something like this:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    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">

    <android.support.design.widget.CoordinatorLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

            <android.support.v7.widget.Toolbar
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:background="#ff4000"
                android:id="@+id/toolbar"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:title="Home"
                />


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


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

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

</android.support.v4.widget.DrawerLayout>
jojemapa
  • 831
  • 1
  • 9
  • 21
  • *within `DrawerLayout`, Toolbar height should be `?actionBarSize`. – Eugen Pechanec Jul 30 '16 at 09:14
  • I do not understand much English, but the toolbar and other components must be within the "CoordinatorLayout". If there is an outside component "DrawerLayout" simply visualize out. Try to place all components of your view within the "CoordinatorLayout" That should solve your problem. – jojemapa Jul 31 '16 at 22:29
  • I know but fix your first sentence in the answer. – Eugen Pechanec Jul 31 '16 at 22:31
0

activity_main_nav.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:context=".MainActivity"
    android:elevation="7dp">

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

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

    <!-- Let's add fragment -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frame"/>

    </LinearLayout>



    <android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/activity_home_drawer"

    app:headerLayout="@layout/header"/>


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

header.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="@drawable/back_header"
    android:layout_height="178dp">


    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:text="Header"
    android:textColor="@android:color/white"/>

</LinearLayout>

Menu Folder activity_home_drawer.xml

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

  <group android:id="@+id/grp1"  android:checkableBehavior="single">
    <item
        android:id="@+id/nav_first"
        android:icon="@drawable/first"
        android:title="First" />

   </group>

    // if you want to underline than use group

    <item
        android:id="@+id/nav_second"
        android:icon="@drawable/second"
        android:title="second" />


      </menu>

MainActivity.java

  public class MainActivity extends AppCompatActivity {


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



    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().replace(R.id.frame, new YourFragment()).commit();

    }
    setNavigationDrawer();
    setToolBar();




}

private void setToolBar() {
    final Toolbar tb = (Toolbar) findViewById(R.id.toolbar1);
    setSupportActionBar(tb);

    ActionBar ab = getSupportActionBar();
    ab.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
    ab.setDisplayHomeAsUpEnabled(true);
    ab.setDisplayShowHomeEnabled(true);


    final android.app.FragmentManager fm = getFragmentManager();

    fm.addOnBackStackChangedListener(new android.app.FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {

            if (getSupportFragmentManager().getBackStackEntryCount() ==0) {
                dLayout.closeDrawers();
                finish();
            }
            else
            {
                dLayout.closeDrawers();

            }
        }
    });
}




private void setNavigationDrawer() {
    dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    NavigationView navView = (NavigationView) findViewById(R.id.navigation);
    Menu m = navView.getMenu();
    for (int i=0;i<m.size();i++) {
        MenuItem mi = m.getItem(i);

        //for aapplying a font to subMenu ...
        SubMenu subMenu = mi.getSubMenu();
        if (subMenu!=null && subMenu.size() >0 ) {
            for (int j=0; j <subMenu.size();j++) {
                MenuItem subMenuItem = subMenu.getItem(j);


            }
        }


    }
    navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {

            Fragment frag = null;
            int itemId = menuItem.getItemId();

            if (itemId == R.id.first) {
               // frag = new First();
                frag = new SearchJobActivity();

            } else if (itemId == R.id.second) {
                frag = new second();
            }


            if (frag != null) {
                openFragmentNew(frag);
                dLayout.closeDrawers();
                return  true;
            }


            return false;
        }
    });
}


public void openFragmentNew(Fragment fragment) {
    String backStateName = fragment.getClass().getName();
    FragmentManager manager = getSupportFragmentManager();
    //fragment not in back stack, create it.
    FragmentTransaction ft = manager.beginTransaction();
    if(!fragments.contains(backStateName)) {

       // ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        // ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
        ft.replace(R.id.frame, fragment);
        ft.addToBackStack(backStateName);
        ft.commit();


        System.out.println("backStateName" + fragments);
    }
    else
    {
       // ft.remove(fragment);

        ft.replace(R.id.frame, fragment);
        ft.commit();
  //      manager.popBackStack();
    }


}



@Override
public void onBackPressed() {
    if (dLayout.isDrawerOpen(GravityCompat.START)) {
        dLayout.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

change accoding to you in mainactivity

Arjun saini
  • 3,891
  • 2
  • 16
  • 44