I am trying to add DrawerLayout on my app but the items are not responding to click. I have tried logging the output on the console but nothing show up when the items are clicked. Here is my code
MainActivity
public class DashboardActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Toolbar toolbar;
DrawerLayout drawerLayout;
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawal);
drawerLayout = (DrawerLayout) findViewById(R.id.drawal_layout);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer,R.string.close_drawer);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
Log.i("info", String.valueOf(id));
switch (id){
case R.id.dashoard:
Toast.makeText(DashboardActivity.this, "This is dasbhard", Toast.LENGTH_SHORT).show();
case R.id.hire_mechanic_id:
Intent intents = new Intent(DashboardActivity.this, LIkelyProblemsActivity.class);
startActivity(intents);
break;
case R.id.tow_van_id:
Toast.makeText(getApplicationContext(),"Hire a Toyin Van", Toast.LENGTH_LONG).show();
break;
}
drawerLayout.closeDrawer(Gravity.START);
return true;
}
}
DrawerLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawal_layout"
android:fitsSystemWindows="false"
tools:openDrawer="start">
<include layout="#layout/activity_dashboard" />
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:id="#+id/navigation_view"
android:layout_gravity="start"
android:fitsSystemWindows="false"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:headerLayout="#layout/activity_navigation_header"
app:menu="#menu/navigation_menu"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
item.xml
<item android:title="Dashboard">
<menu>
<item
android:title="item 1"
android:icon="#drawable/mechanic"
android:id="#+id/dashoard"/>
<item
android:title="item 2"
android:icon="#drawable/mechanic"
android:id="#+id/hire_mechanic_id"/>
<item
android:title="item 3"
android:icon="#drawable/spareparts"
android:id="#+id/spare_parts_id"/>
<item
android:title="item 4"
android:id="#+id/tow_van_id"
android:icon="#drawable/toyin_van" />
</menu>
</item>
</menu>
I have checked the drawerlayout, menu and the main activity but I can't really figure out what the problem is. Anyone one who can help me.
You forget to call navigationView.setNavigationItemSelectedListener listener that's why navigation item not being called, just add this line
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this)
Hope it will help you!!
Related
I learnded to use Android NavigationView,it was shown well but could't response to click event.
Here are my codes. I want to show a Toast when i click any item.but it doesn't work...
nav = (NavigationView) findViewById(R.id.navigation);
//nav.setClickable(true);
nav.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
item.setChecked(true);
Toast.makeText(getApplicationContext(), "caonima", Toast.LENGTH_SHORT).show();
return false;
}
});
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:icon="#drawable/homepage"
android:title="首页"
/>
<item android:id="#+id/psychology_item" android:title="日常心理学" android:icon="#drawable/right"/>
<item android:id="#+id/recommand" android:title="用户推荐日报" android:icon="#drawable/right"/>
<item android:id="#+id/movies" android:title="电影日报" android:icon="#drawable/right"/>
<item android:id="#+id/internet" android:title="互联网安全" android:icon="#drawable/right"/>
<item android:id="#+id/noboring" android:title="不许无聊" android:icon="#drawable/right"/>
<item android:id="#+id/desingn" android:title="设计日报" android:icon="#drawable/right"/>
</group>
<?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"
android:fitsSystemWindows="true"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/tool_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<android.support.design.widget.NavigationView
android:fitsSystemWindows="true"
android:id="#+id/navigation"
android:layout_gravity="start"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/nav_menu"
/>
</android.support.v4.widget.DrawerLayout>
it has disturbed me for two days, i find some question use listView ,
or add drawerView.bringToFront();drawerView.requestLayout();
But nothing changed.
by the way , i used toolbar in a fragment like this
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main_title,container,false);
mToolbar = (Toolbar)v.findViewById(R.id.main_title_toolbar);
mDrawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
mToolbar.setTitle("首页");
mToolbar.setTitleTextColor(getResources().getColor(R.color.text));
((AppCompatActivity)getActivity()).setSupportActionBar(mToolbar);
mToolbar.setNavigationIcon(R.drawable.navigation);
setHasOptionsMenu(true);
mToggle = new ActionBarDrawerToggle(getActivity(),mDrawerLayout,mToolbar,
R.string.drawer_open,R.string.drawer_shut){
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
drawerView.bringToFront();
drawerView.requestLayout();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
//mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
return v;
}
Oh, i just so a article.it has solved my problem.
here is the link : enter link description here
You just have to bring your drawerLayout to the front,
so try to use these two method
mDrawerLayout.bringToFront();
mDrawerLayout.requestLayout();
then you can find it solved,if you want know more about this,search"Z order".
I'm currently developing an android app using a Toolbar and a DrawerLayout. The main content is a custom SurfaceView where I can draw differents shapes, text ... The left menu is a NavigationView and is used as a toolbox (I select what I want to draw from the left and I draw it on the SurfaceView). Everything is working fine except for one thing : when I first try to open the left menu (by clicking the toolbar or by sliding from the left side of the screen) the items are not visible. And while I don't click on any item (which are not visible) they stay invisible. The problem is only fixed when I click on an invisible item, after that the menu is working fine. I'm using a custom theme to hide the status bar and remove the default actionbar :
<style name="AppBaseTheme" parent="#style/Theme.AppCompat.Light.NoActionBar"></style>
<style name="ColladiaTheme" parent="AppBaseTheme">
<!-- Remove action bar -->
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<!-- Remove status bar -->
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<!-- Material theme -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Here are some screenshots :
Menu open but not visible
Menu visible after clicking an item
<?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" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.ia04nf28.colladia.DrawColladiaView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/draw_view" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
</FrameLayout>
<!-- Left navigation menu -->
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:layout_gravity="start"
android:fitsSystemWindows="false"
app:menu="#menu/nav_view_items" >
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
And here is the Activity code :
public class DrawActivity extends AppCompatActivity {
private static final String TAG = "DrawActivity";
private DrawerLayout drawer;
ActionBarDrawerToggle drawerToggle;
private NavigationView nav;
private DrawColladiaView colladiaView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw);
// Change toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
colladiaView = (DrawColladiaView) findViewById(R.id.draw_view);
colladiaView.setApplicationCtx(getApplicationContext());
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// Add burger button
drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(drawerToggle);
// Removes overlay
drawer.setScrimColor(Color.TRANSPARENT);
drawer.closeDrawers();
nav = (NavigationView) findViewById(R.id.nav_view);
setUpDrawerContent(nav);
}
public void setUpDrawerContent(NavigationView nav)
{
nav.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
selectDrawerItem(item);
return true;
}
}
);
}
public void selectDrawerItem(MenuItem item)
{
switch(item.getItemId())
{
case R.id.nav_home:
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
break;
default:
Element newElement = ElementFactory.createElement(getApplicationContext(), item.getTitle().toString());
if (newElement != null) colladiaView.insertNewElement(newElement);
drawer.closeDrawers();
break;
}
}
#Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
#Override
public void onBackPressed() {
Log.d(TAG, "onBackPressed");
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
It looks like you should be overriding onOptionsItemSelected instead of onBackPressed:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// This will toggle the drawer if android.R.id.home is clicked
if(drawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle any other menu item selections...
return super.onOptionsItemSelected;
}
To be honest, I do not see where your drawer is ever being opened. It should still respond to a swipe from the left edge though. This code will set it to be toggled when android.R.id.home is clicked (the back/menu button).
I have built an application with an navigation drawer activity in it but now I want it to be present on my other activities as well. I don't want to handle events again and again in different classes.
Also I got suggestions like making one base class and extend that but as I cannot extend more than one class.
Also I also want to use fragments so that I did not have to write the same code again and again
My activity_main.xml is like:
<?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"
android:background="#color/colorPrimary">
<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" />
</android.support.v4.widget.DrawerLayout>
nav_header.xml is like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="72dp"
android:layout_height="72dp"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:src="#drawable/bcet_logo" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="#string/college_name"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/college_name_full" />
</LinearLayout>
MainActivity.java:
package com.bcetapp.bcetbeta;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#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);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_student_list) {
// Handle the camera action
} else if (id == R.id.nav_staff_list) {
} else if (id == R.id.nav_help) {
} else if (id == R.id.nav_settings) {
} else if (id == R.id.nav_action_share) {
} else if (id == R.id.nav_contact_us) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
/****************************************************************************************USER DEFINED FUNCTIONS******************************************************************/
public void viewMyProfile(View view){
/*intent = new Intent(this, ProfileActivity.class);
startActivity(intent);*/
}
}
activity_main_drawer.xml:
<?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/nav_student_list"
android:icon="#drawable/ic_group"
android:title="Students" />
<item
android:id="#+id/nav_staff_list"
android:icon="#drawable/ic_action_glasses"
android:title=" College Staff" />
<item
android:id="#+id/nav_help"
android:icon="#drawable/ic_action_help"
android:title="help" />
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_menu_manage"
android:title="Settings" />
</group>
<item android:title="More">
<menu>
<item
android:title="About us"
android:id="#+id/nav_about_us"
android:icon="#drawable/ic_action_info"
/>
<item
android:id="#+id/nav_action_share"
android:icon="#drawable/ic_action_share"
android:title="Share App" />
<item
android:id="#+id/nav_contact_us"
android:icon="#drawable/ic_menu_send"
android:title="contact us" />
</menu>
</item>
</menu>
It's not as easy. There are 2 tech-ticks to achieve it. One is to create use fragment second one is to add the whole code in all activities.
To achieve your result in above case, Add the fragment in your activity and replace/add activities as your required
Also i got suggestions like making one base class and extend that but as I cannot extend more than one class.
True, but the thing about class extension is that you can create a base class that extends AppCompatActivity, and then make your activities extend that base class:
public class BaseClass extends AppCompatActivity...
public class MainActivity extends BaseClass...
This will implicitly cause your MainActivity to extend AppCompatActivity.
Navigation drawer menu and setting menu not working on Android kitkat and its lower version. But these menu options are completely functional on Android lollipop and its higher version. Even menu popup for setting does not display on kitkat or its lower version but it displays on android lollipop or its higher version. Following is the code :
activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
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/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/appbar"/>
<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">
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginTop="56dp"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true"
tools:context="com.vinitmandlesha.myins.MainActivity"
android:background="#f2374a">
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
content_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.vinitmandlesha.myins.MainActivity"
tools:showIn="#layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
styles.xml(v21)
<resources>>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
</resources>
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#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);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (id == R.id.nav_home) {
// Handle the camera act
} else if (id == R.id.nav_about_us) {
FragmentPersonal frag = new FragmentPersonal();
transaction.add(R.id.drawer_layout,frag, "About");
transaction.commit();
} else if (id == R.id.nav_contactus) {
} else if (id == R.id.nav_faq) {
} else if (id == R.id.nav_personal) {
} else if (id == R.id.nav_commercial) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
First when you are updating your toolbar it always hide your default menu. What you have to do is create your on menu with an icon and set is properties e.g ifroom. On the other hand for your navigation drawer you have to set your a theme with action bar activity and if you are using it in devices which are lollipop version than you will have to use AppCompat theme. I will reference the tutorial of slidenerd which are available on you tube which covers the material design it have details you need about navigation drawer in lollipop and pre-lollipop devices .
I have written the following code to add drawer layout in my material design theme which is not showing up. Just the tool bar is showing up.
MainActivity.java
package com.example.materiald;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
public class MainActivity extends ActionBarActivity {
ActionBarDrawerToggle mActionBarDrawerToggle;
DrawerLayout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open_navigation_drawer, R.string.close_navigation_drawer);
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/toolbar" />
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<!-- Nav drawer -->
<ListView
android:id="#+id/drawerList"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#android:color/white"
android:divider="#android:color/white"
android:dividerHeight="8dp"
android:drawSelectorOnTop="true"
android:headerDividersEnabled="true" />
</android.support.v4.widget.DrawerLayout>
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res /android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/myColorPrimary"
android:minHeight="56dp"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
values/styles.xml
<style name="AppTheme.Base" parent="Theme.AppCompat"></style>
values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="colorPrimary">#color/myColorPrimary</item>
<item name="colorPrimaryDark">#color/myColorPrimaryDark</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
</resources>
values-v21/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
I missed out certain important statements in my activity. Here is the complete activity the remaining stays the same. With following changes I was able to add the new NavigationDrawer
public class MainActivity extends ActionBarActivity {
ActionBarDrawerToggle mDrawerToggle;
DrawerLayout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open_navigation_drawer, R.string.close_navigation_drawer);
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //<---- added
getSupportActionBar().setHomeButtonEnabled(true); //<---- added
}
#Override
public boolean onOptionsItemSelected(MenuItem item) { //<---- added
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) { //<---- added
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState(); // important staetment for drawer to identify its state
}
#Override
public void onConfigurationChanged(Configuration newConfig) { //<---- added
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public void onBackPressed() {
if(mDrawerLayout.isDrawerOpen(Gravity.START|Gravity.LEFT)){ //<---- added
mDrawerLayout.closeDrawers();
return;
}
super.onBackPressed();
}
}