Navigation drawer does not open when pressing the hamburger icon in toolbar - java

I am trying to toggle the navigation view by the hamburger icon. The navigation view is appearing on sliding from the left. The following is the code that I have included in my MainActivity "onCreate Function"
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setVisibility(View.VISIBLE);
setSupportActionBar(toolbar);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.addDrawerListener(toggle);
getSupportActionBar().setHomeButtonEnabled(true);
toggle.syncState();
I also have tried this code block which doesn't seem to work.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
I also tried this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER
return true;
}
return super.onOptionsItemSelected(item);
}
My main_activity.xml is this
<?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">
>
<RelativeLayout
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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/app_bar_main"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/colorPrimary"
app:itemIconTint="#drawable/bottom_navigation_bar"
app:itemTextColor="#drawable/bottom_navigation_bar"
app:menu="#menu/my_navigation_items" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_navigation"
android:layout_alignParentTop="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</RelativeLayout>
<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:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
This is my app_bar_main.xml which is where I define the ui of toolbar.
enter code here
<?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"
tools:context="com.example.android.moodindigo.MainActivity">
<!--<RelativeLayout-->
<!--android:id="#+id/relativelayout_for_fragment"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--></RelativeLayout>-->
<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"
app:title="Mood Indigo"
/>
</android.support.design.widget.AppBarLayout>
<!--<include layout="#layout/content_main" />-->
</android.support.design.widget.CoordinatorLayout>
Any help would be highly appreciated. I don't know what I am doing wrong. Please let me know if anything else is needed. I didn't paste the whole code here as the project is quite big. I checked many StackOverflow post for this and I tried to do the same thing but it doesn't seem to work.

try this in the onCreate of your main activity :
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
i hope it will help you!

In your ActionBarDrawerToggle constructor you use toolbar as third parameter. I am not sure if it is valid, since the official documentation for ActionBarDrawerToggle constructor parameters quotes:
Parameters:
activity - Activity: the Activity hosting the drawer
drawerLayout - Drawerlayout: the DrawerLayout to link to the given Activity's
ActionBar
drawerImageRes - int: A Drawable resource to use as the drawer
indicator
openDrawerContentDescRes - int: A String resource to describe the
"open drawer" action for accessibility
closeDrawerContentDescRes - int: A String resource to describe the
"close drawer" action for accessibility
So, the third parameter should be Drawable Resource but toolbar is a View.
Edit:
As you are using v7 and not v4, there is one sentence from official documentation https://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html I think is important:
Please use ActionBarDrawerToggle(Activity, DrawerLayout, int, int) if you are setting the Toolbar as the ActionBar of your activity.
So it seems you should use differnt constructor from the one you are using now.
Hope this will help.

Related

Navigation drawer is not collapsing, no hamburger button is there

My drawer layout is showing as a normal activity without any button in the toolbar also its not collapsing in any way..I want it from right side.
This is my drawer activity layout..
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".myDrawer"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#000"
android:id="#+id/drawer_toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:background="#color/colorPrimaryDark"
android:layout_width="100dp"
android:layout_height="match_parent"
android:foregroundGravity="left"
app:menu="#menu/drawer_menu"
app:headerLayout="#layout/nav_header">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
this is my nav_header.xml layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="300dp"
android:padding="15dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:layout_width="100dp"
android:layout_height="200dp"
android:src="#drawable/hhh"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="pehli line"
android:textColor="#color/colorPrimaryDark"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dusri line"
android:textColor="#color/colorAccent"/>
</LinearLayout>
This is drawer activity java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class myDrawer extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_drawer);
toolbar=findViewById(R.id.drawer_toolbar);
setSupportActionBar(toolbar);
}
}
Output of above code-->
but I want it a navigation drawer, that i can slide from left.
Thank you so much.
Replace your navigation view with below
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/my_menu">
</android.support.design.widget.NavigationView>
Add below method to display toggel
public void addToggel() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
//drawer.setDrawerListener(toggle);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
}
Add below string in string.xml
<string name="navigation_drawer_open">open</string>
<string name="navigation_drawer_close">close</string>
update drawer layout id and navigation view id as per your code
Let me know with more help

Navigation Drawer toggle button not working only when swiped across is working

I do have placed the navigation drawer icon on the action bar and it certainly is showing up, but upon click on the drawer icon the function is not happening, only upon swipe the navigation drawer is opening.
ArticleActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
mProgressView = findViewById(R.id.progress);
mFullContainerView = findViewById(R.id.full_container);
ActionBar actionBar = getActionBar();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
activity_article.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
<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"
android:focusableInTouchMode="true"
android:scrollbars="vertical"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/main_content"
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:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_article_drawer" />
<include layout="#layout/list_view" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
From your code it looks like you are using your custom toolbar as actionbar, But you have not set it as support actionbar. You need to use setSupportActionBar(toolbar) to set your custom toolbar as actionbar.
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Also, make sure you are extending your application main theme from theme with no actionbar.

Not able to view the navigation bar in Android app despite including the necessary syntaxes

I tried to include the navigation bar in an existing app. but the contens of the activity is also not shown on inclusion
this is my code --
<?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:layout_gravity="start"
android:fitsSystemWindows="true"
tools:context=".common.components.main.HomeActivity"
tools:openDrawer="start">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<include layout="#layout/nav_toolbar" />
<include layout="#layout/activity_home_contents" />
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:itemTextAppearance="#style/TextAppearance.AppCompat.Body1"
app:itemTextColor="#FFFFFF"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
This is the contents of my nav_toolbar layout
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/toolbar1"
android:background="#color/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
>
</android.support.v7.widget.Toolbar>
Similarly I have activity_home_contents in scroll view as
<ScrollView 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:fillViewport="true"
android:background="#color/white"
android:scrollbars="none"
>
<RelativeLayout android:orientation="vertical"
android:id="#+id/home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
tools:context=".common.components.main.HomeActivity">
......
And code snipplet from HomeActivity.java is
.....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
floodEvacApplication = (FloodEvacApplication)
getApplicationContext();
lastAddress = floodEvacApplication.getLatestAddress();
if (lastAddress == null) {
initGoogleAPI();
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout)
findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView navigationView = (NavigationView)
findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
......
I tried various ways avaliable online and searched online but couldn't able to display the activity_home_contents also but if I remove the navigation drawer and display the activiy_home then I am able to display it.
Try to add the following line under android.support.design.widget.NavigationView in Drawer's XML:
android:layout_gravity="start"

Add a navigation drawer to blank activity

I have created an entire project on a blank activity(Android Studio). I want to add a navigation drawer without hampering with my previous code.Is there a way to add a navigation drawer to a blank activity, if yes please do guide.
Step1:Include navigation view to your xml file which will be inflated by yor activity on create.
<?xml version="1.0" encoding="utf-8"?>
<include
layout="#layout/app_bar_main"
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:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
step2:initialize views
private NavigationView navigationView;
private DrawerLayout drawer;
step3: sent the event on drawer close and open
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) {
#Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawer.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessary or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
hope this helps how set the navigation drawer.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/icon">
<FrameLayout
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/lvLeft"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#ff8800"
android:choiceMode="singleChoice"
android:divider="#android:color/holo_blue_bright"
android:dividerHeight="0dp" />
<ListView
android:id="#+id/lvRight"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#0077ff"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

Navigation drawer - Header View with ListView

I'm currently creating and defining a navigation drawer. I want now a header view, like their on the google apps, above the item rows. I only found examples with RecyclerViews, and i dont want to use it. I have all finished with a ListView and all other stuff. Maybe someone can help me :) Thanks in advance
You can create NavigationView using android design support library without having pain to create listview or RecyclerView, its all created by android.
To add it to your project you need to add the android design support library to your project, add below line in build.gradle
compile 'com.android.support:design:22.2.0
Check out android design support features here
First create a header(header.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="#drawable/background_material"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nyname"
</RelativeLayout>
Next create a menu resource file, the items in the menu will be the items displayed in the drawer(drawer.xml)
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/first1"
android:checked="false"
android:icon="#drawable/icon1"
android:title="#string/string1" />
<item
android:id="#+id/second2"
android:checked="false"
android:icon="#drawable/icon2"
android:title="#string/string2" />
</menu>
Next create a DrawerLayout file, within the drawerlayout you can see I have included a Toolbar and a 'FrameLayout`. When the item in the drawerlayout is clicked you can replace fragment.
Also within it is the NavigationView with these parameters:
app:headerLayout="#layout/header"
app:menu="#menu/drawer"
android:layout_gravity="start"
app:headerLayout is the header.xml that we created in step 1.
app:menu is the menu resource item i.e drawer.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<include
android:id="#+id/toolbar"
layout="#layout/tool_bar"
/>
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="#layout/header"
app:menu="#menu/drawer"
/>
</android.support.v4.widget.DrawerLayout>
Next in your MainActivity extend AppcompatActivity,
public class MainActivity extends AppCompatActivity {
............................................
Intialise NavigationView and call setNavigationItemSelectedListener to get click events,
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing Toolbar and setting it as the actionbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing NavigationView
navigationView = (NavigationView) findViewById(R.id.navigation_view);
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if(menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()){
//Replacing the main content with ContentFragment
case R.id.first1:
SomeFragment fragment = new SomeFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame,fragment);
fragmentTransaction.commit();
return true;
...................
Step by step procedure to create navigationview go here
How it would look:

Categories

Resources