1

I have a problem with DrawerLayout and AppBarLayout. I need to add DrawerLayout to make NavigationDrawer. When I add DrawerLayout, the entire Layout painted in blue color. And Navigation Drawer opens only via swipe does not work through the click of a button. I do not understand what the problem is. Help me please.

enter image description here

This is my main layout:

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

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:expanded="false">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                app:tabMode="scrollable" />
        </android.support.design.widget.AppBarLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

        <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:menu="@menu/drawermenu" />
    </android.support.v4.widget.DrawerLayout>
</android.support.design.widget.CoordinatorLayout>

And this is my Main Activity code:

package com.jeffe.tabstest;

import android.content.Intent;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private DrawerLayout drawerLayout;

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

        toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        viewPager = (ViewPager)findViewById(R.id.viewPager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout)findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        assert navigationView != null;
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();

        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onBackPressed() {
        assert drawerLayout != null;
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new MusicTab(), "Tab1");
        adapter.addFragment(new AnimeTab(), "Tab2");
        adapter.addFragment(new ArtistTab(), "Tab3");
        adapter.addFragment(new AlbumTab(), "Tab4");
        adapter.addFragment(new FavoritesTab(), "Tab5");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

Something wrong with layout because none of the buttons in the AppBar doesn't work. But before I added DrawerLayout everything worked perfectly.

Irvin Bridge
  • 303
  • 5
  • 11
  • 1
    The `DrawerLayout` needs to have only one main content `View`. You've got two, and it's treating the `AppBarLayout` like its whole main content. You can rearrange your layout like is shown in [this post](http://stackoverflow.com/questions/30719369/android-design-library-coordinatorlayout-appbarlayout-and-drawerlayout). – Mike M. Jul 06 '16 at 10:38
  • Thanks, I just move AppBarLayout to another layout file and now everithing works perfect. – Irvin Bridge Jul 06 '16 at 12:32

1 Answers1

0

Don't use ActionBarDrawerToggle and NavigationView which is the oldest way.

Try this -

 toolbar = (Toolbar)findViewById(R.id.toolbar);
 toolbar.setNavigationIcon(YOUR_MENU_ICON);  //set your navigation icon
 setSupportActionBar(toolbar);


 toolbar.setNavigationOnClickListener(new View.OnClickListener() {

     @Override
     public void onClick(View v) {
           drawerLayout.openDrawer(drawerLayout);  
        }
     });

Hope it will help :)

Onkar Nene
  • 1,359
  • 1
  • 17
  • 23
  • Does not work. Key still does not work, but at least was NavigationView animation - now it is not. What about the blue screen? I think it has something to do with my Layout, but I do not understand how. By the way, I can not press the tabs too. – Irvin Bridge Jul 06 '16 at 07:47
  • Didn't get you? Please elaborate what do you mean – Onkar Nene Jul 06 '16 at 07:54
  • I mean that my problem in layout because none of the buttons in AppBar doesn't work. – Irvin Bridge Jul 06 '16 at 08:01
  • I think I should not use CoordinatorLayout and AppBarLayout, but I do not know what to replace them, to still work. – Irvin Bridge Jul 06 '16 at 08:50
  • don't use coordinatorlayout, if you want drawer on the top of actionbar then make your drawer layout as parent – Onkar Nene Jul 06 '16 at 09:06
  • After removing CoordinatorLayout and making DrawerLayout as parent nothing changes. – Irvin Bridge Jul 06 '16 at 09:16