I'm using a NavigationView in a DrawerLayout, I'm not sure if I've forgotten something, but just is hidding the Navigation when I click it.
this is my code:
MainActivity.java:
private void instantiateDrawerLayout() {
Toolbar mToolbar = (Toolbar) generalContent.findViewById(R.id.toolbar);
mToolbar.setBackgroundColor(getResources().getColor(R.color.colorMainActivity));
setSupportActionBar(mToolbar);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setItemIconTintList(ColorStateList.valueOf(getResources().getColor(R.color.colorMainActivity)));
navigationView.setNavigationItemSelectedListener(this);
Log.d("instantiateDrawerLayout", "called");
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, mDrawer, mToolbar, R.string.drawer_open, R.string.drawer_close);
mDrawer.setDrawerListener(drawerToggle);
drawerToggle.syncState();
...
}
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
Log.d("onNavigationItem", "called");
final int itemId = menuItem.getItemId();
Intent intent;
switch (itemId) {
case R.id.navigation_item_1:
hideDrawer();
break;
case R.id.navigation_item_2:
intent = new Intent(this, FirstActivity.class);
startActivity(intent);
this.finish();
break;
case R.id.navigation_item_3:
intent = new Intent(this, SecondActivity.class);
startActivity(intent);
this.finish();
break;
case R.id.navigation_item_4:
intent = new Intent(this, ThirdActivity.class);
startActivity(intent);
this.finish();
break;
}
return false;
}
activity_general.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBackground">
<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_header"
app:itemIconTint="#color/colorAccent"
app:menu="#menu/menu_drawer" />
<LinearLayout
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical" />
menu_drawer.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!--All the items added without any grouping-->
<item
android:id="#+id/navigation_item_1"
android:icon="#drawable/ic_number_1"
android:title="#string/navigation_item_1" />
<item
android:id="#+id/navigation_item_2"
android:icon="#drawable/ic_number_2"
android:title="#string/navigation_item_2" />
<item
android:id="#+id/navigation_item_3"
android:icon="#drawable/ic_number_3"
android:title="#string/navigation_item_3" />
<item
android:id="#+id/navigation_item_4"
android:icon="#drawable/ic_number_4"
android:title="#string/navigation_item_4" />
</menu>
and my Logcat:
enter image description here
I solved it, was for the orden in the Layout.
As you see I put the NavigationView first, and It causes when I clicked over the NavigationView thinks I clicked In LinearLayout, or I think this this is what is happened.
Just change the order and all fine.
Related
I have watched a lot of solutions but I'm in trouble with my NavigationView. I created it and tried to use it but somehow it doesn't react. This is the code I used.
Thank you !
This is my XML for "drawermenu"
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/add"
android:icon="#drawable/ic_add_circle_black_24px"
android:title="Добави автомобил" />
<item
android:id="#+id/settings"
android:icon="#drawable/ic_build_black_24px"
android:title="Настройки" />
<item
android:id="#+id/export"
android:icon="#drawable/ic_importexport"
android:title="Експорт на инф."
/>
<item
android:id="#+id/about"
android:icon="#drawable/ic_info_outline_black_24px"
android:title="За програмиста" />
<item
android:id="#+id/exit"
android:icon="#drawable/ic_exit_to_app_black_24px"
android:iconTintMode="src_in"
android:title="Изход" />
</group>
This is all I used for NavigationView
<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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/myYellow"
android:paddingRight="10dp"
android:theme="#style/AppTheme2"
tools:context="com.example.hpdemon.diploma.MainForm"
tools:openDrawer="start">
<FrameLayout
android:id="#+id/flcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/myYellow"
android:fitsSystemWindows="true"
app:headerLayout="#layout/header"
app:itemIconTint="#color/colorPrimaryDark"
app:itemTextColor="#56733b"
app:menu="#menu/drawermenu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
And this is the code from MainActivity. I call the Drawer function in onCreate method
public void Drawer() {
NavigationView navigationView = findViewById(R.id.nv);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
// close drawer when item is tapped
int id = menuItem.getItemId();
if (id == R.id.add) {
//Handle your stuff here
Toast.makeText(MainForm.this, "Ще проработя ;) ", Toast.LENGTH_LONG).show();
}
mDrawerLayout.closeDrawers();
// Add code here to update the UI based on the item selected
// For example, swap UI fragments here
return true;
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
This is for the mToggle element
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
enter image description hereAs you can see I have added 6 item in navigation drawer but on clicking Logout it is not triggered. And sorry for bad code format. I'm new here.
#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_log_out) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.nav_profile:
break;
case R.id.nav_wallet:
break;
case R.id.nav_search:
break;
case R.id.nav_about_us:
break;
case R.id.nav_contact_us:
break;
case R.id.nav_log_out:
Intent intent = new Intent(this,LogIn.class);
FirebaseAuth.getInstance().signOut();
startActivity(intent);
}
This is my XML file for menu item. The id are the same I declared in the XML file. Log out in action bar is working fine but it is not working in Navigation Drawer.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_profile"
android:icon="#drawable/ic_person_black_24dp"
android:title="Profile" />
<item
android:id="#+id/nav_wallet"
android:icon="#drawable/ic_attach_money_black_24dp"
android:title="EasyFare Wallet" />
<item
android:id="#+id/nav_search"
android:icon="#drawable/ic_directions_bus_black_24dp"
android:title="Search Bus" />
<item
android:id="#+id/nav_about_us"
android:icon="#drawable/ic_folder_shared_black_24dp"
android:title="About Us"/>
<item
android:id="#+id/nav_contact_us"
android:icon="#drawable/ic_email_black_24dp"
android:title="Contact Us" />
<item
android:id="#+id/nav_log_out"
android:icon="#drawable/ic_exit_to_app_black_24dp"
android:title="Log Out" />
</group>
</menu>
activity file with DrawerLayout
<?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">
<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>
You have to add brackets for case in logout..
case R.id.nav_log_out:
{
Intent intent = new Intent(this,LogIn.class);
FirebaseAuth.getInstance().signOut();
startActivity(intent);
}
Could you include the code in your activity's onCreate?
One possibility is that you forgot to register your activity as the listener for the navigation view.
mNavigationView.setNavigationItemSelectedListener(this)
Clicking on some other item, let's say the second one, gets this item triggered?
If yes add a break statement in your logout case and also create a default case.
You could also try replacing your code with the following sample.
#SuppressWarnings("StatementWithEmptyBody")
#Override public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.nav_profile:
break;
case R.id.nav_wallet:
break;
case R.id.nav_search:
break;
case R.id.nav_about_us:
break;
case R.id.nav_contact_us:
break;
case R.id.nav_log_out:
{
Intent intent = new Intent(this,LogIn.class);
FirebaseAuth.getInstance().signOut();
startActivity(intent);
break;
}
DrawerLayout drawer;
drawer = findViewById(R.id.THE_ID_OF_YOUR_DRAWER);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Also make sure that in your OnCreate , OnStart or onResume you include the following
DrawerLayout drawer;
drawer = findViewById(R.id.THE_ID_OF_YOUR_DRAWER);
I will provide you with a code i use for a project of mine and try to see if you find any differences that would explain the fact your code does not work.
AnnouncementsActivity.java
.
.
.
.
public class AnnouncementsActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_announcements);
.
.
.
.
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer;
drawer = findViewById(R.id.drawer_layout_astiko);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
toolbar.setNavigationIcon(R.mipmap.ic_toolbar);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
.
.
.
.
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_first_item) {
if(prev_activity_routes){
Intent intent = new Intent(this,RoutesActivity.class);
intent.putExtra("Direction Forth",bus_direction_forth);
intent.putExtra("Team",team);
intent.putExtra("Route name", value1);
intent.putExtra("odigos_onomateponimo",odigos_onomateponimo);
startActivity(intent);
}else{
if(isAstiko){
Intent intent = new Intent(this,TicketIssuanceActivityAstiko.class);
intent.putExtra("Direction Forth",bus_direction_forth);
intent.putExtra("Team",team);
intent.putExtra("Route name", value1);
intent.putExtra("odigos_onomateponimo",odigos_onomateponimo);
startActivity(intent);
}else{
Intent intent = new Intent(this,TicketIssuanceActivity.class);
intent.putExtra("Direction Forth",bus_direction_forth);
intent.putExtra("Team",team);
intent.putExtra("Route name", value1);
intent.putExtra("odigos_onomateponimo",odigos_onomateponimo);
startActivity(intent);
}
}
} else if (id == R.id.nav_second_item) {
Intent intent = new Intent(this,DefinedTicketsActivity.class);
intent.putExtra("Direction Forth",bus_direction_forth);
intent.putExtra("Team",team);
intent.putExtra("Route name", value1);
intent.putExtra("odigos_onomateponimo",odigos_onomateponimo);
intent.putExtra("PreviousActivityRoutes",prev_activity_routes);
intent.putExtra("isAstiko",isAstiko);
startActivity(intent);
}
.
.
.
.
}else if (id == R.id.nav_eighth_item) {
Intent intent = new Intent(this, SignInActivity.class);
startActivity(intent);
}
DrawerLayout drawer;
drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
}
ActivityAnnouncements.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:openDrawer="start">
<include
layout="#layout/app_bar_announcements"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="500dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:theme="#style/NavigationDrawerStyle"
app:headerLayout="#layout/my_nav_header"
app:menu="#menu/my_drawer" />
</android.support.v4.widget.DrawerLayout>
app_bar_announcements.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"
tools:context="com.e_analysis.AlphaPro.MobileTicketing.AnnouncementsActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/MyTheme">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="65dp"
android:background="?attr/colorPrimary"
app:popupTheme="#style/MyTheme"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_announcements" />
</android.support.design.widget.CoordinatorLayout>
content_announcements.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:layout_marginBottom="-0dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:orientation="vertical"
android:background="#color/colorPrimary"
android:gravity="fill"
tools:context="com.e_analysis.AlphaPro.MobileTicketing.AnnouncementsActivity"
tools:showIn="#layout/app_bar_announcements">
.
.
.
.
</LinearLayout>
my_drawer.xml in the menu subfolder
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_first_item"
android:icon="#mipmap/ic_printer_black"
android:title="#string/nav_first_item_value"/>
<item
android:id="#+id/nav_second_item"
android:icon="#mipmap/ic_tune_vertical"
android:title="#string/nav_second_item_value" />
.
.
.
.
<item
android:id="#+id/nav_eighth_item"
android:icon="#mipmap/ic_logout"
android:title="#string/nav_eighth_item_value" />
</group>
</menu>
my_nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<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="#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_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
.
.
.
.
</LinearLayout>
This code is working (not as it is ofcourse) so try to do some cross-checking with it and see what you are missing. Most probably it will be something easy to overlook and frustrating when you find out. Good luck!
I am trying to use NavigationView from Android Support Design library in my app. For some reason, OnNavigationItemSelected listener is not being called. Here is my code
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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<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_header"
app:menu="#menu/drawer_menu" />
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>
Activity onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutID());
toolbar = (Toolbar) findViewById(R.id.activity_toolbar);
setSupportActionBar(toolbar);
toolbar.inflateMenu(R.menu.common_menu);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
actionBar.setDisplayHomeAsUpEnabled(true);
}
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
Snackbar.make(contentLayout, menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show();
menuItem.setChecked(true);
// allow some time after closing the drawer before performing real navigation
// so the user can see what is happening
drawerLayout.closeDrawer(GravityCompat.START);
mDrawerActionHandler.postDelayed(new Runnable() {
#Override
public void run() {
navigate(menuItem.getItemId());
}
}, DRAWER_CLOSE_DELAY_MS);
drawerLayout.closeDrawers();
return true;
}
});
usernameTextView = (TextView) findViewById(R.id.drawer_header_username);
usernameTextView.setText(getAppDContext().getAccount().getUsername());
}
When you make XML layout, you should write down NavigationView after BaseLayout (FrameLayout, LinearLayout, etc..)
<DrawerLayout>
<FrameLayout />
<NavigationView />
</DrawerLayout>
For me this did the trick!
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.bringToFront();
Your activity main layout should look like this:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/navigationDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/activity_main_content" />
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
style="#style/NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
app:headerLayout="#layout/header"
app:menu="#menu/menu_drawer"/>
</android.support.v4.widget.DrawerLayout>
In this NavigationView I linked header.xml and menu_drawer.xml (from menu folder)
for example menu_drawer.xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav1"
android:checked="true"
android:icon="#drawable/logo"
android:title="Navigation item 1"/>
<item
android:id="#+id/nav2"
android:icon="#drawable/logo"
android:title="Navigation item 2"/>
</group>
</menu>
than your java code:
public class ActivityMain extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpToolbar();
setUpNavDrawer();
}
private void setUpNavDrawer() {
NavigationView view = (NavigationView) findViewById(R.id.navigationView);
mDrawerLayout = (DrawerLayout) findViewById(R.id.navigationDrawer);
view.setNavigationItemSelectedListener(this);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawerOpen, R.string.drawerClose);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
Check if this work for you. In my project works like a charm.
Can anyone please help me on designing this android layout? I want to put two icons on the drawer toolbar like on the image below.
This is LeftNavigationActivity.class
public class LeftNavigationActivity extends BaseActivity
implements NavigationView.OnNavigationItemSelectedListener {
private ImageView profilePic;
Session session;
FragmentManager fragmentManager = getSupportFragmentManager();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_left_navigation);
this.setTitle("Home");
session = new Session(this);
if (!session.loggedIn()) {
logOut();
}
View v = getLayoutInflater().inflate(R.layout.nav_header_left_navigation,
null);
TextView username = (TextView) v.findViewById(R.id.username);
Intent intent = getIntent();
String uname = intent.getStringExtra("username");
Toast.makeText(this, "Welcome " + uname, Toast.LENGTH_SHORT).show();
username.setText(uname);
LoansAndDebtsFragment loansAndDebtsFragment = new LoansAndDebtsFragment();
fragmentManager.beginTransaction().replace(R.id.layout_for_fragment,
loansAndDebtsFragment, loansAndDebtsFragment.getTag()).commit();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
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();
NavigationView navigationView = (NavigationView)
findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
FragmentManager fragmentManager = this.getSupportFragmentManager();
int stackCount = fragmentManager.getBackStackEntryCount();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if (fragmentManager.getFragments() != null) {
LoansAndDebtsFragment loansAndDebtsFragment = new
LoansAndDebtsFragment();
fragmentManager.beginTransaction().replace(R.id.layout_for_fragment,
loansAndDebtsFragment, loansAndDebtsFragment.getTag()).commit();
}
}
The LeftNavigationActivity is the BaseDrawerActivity which contains the navigation menu.
This is app_bar_left_navigation.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"
tools:context="com.gebeya.jo.pyf.activities.LeftNavigationActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.NoToolBarWithABrandTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorWhite"
app:titleTextColor="#color/colorBrand1"
app:popupTheme="#style/AppTheme.NoToolBarWithABrandTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_left_navigation" />
</android.support.design.widget.CoordinatorLayout>
This is nav_header_left_navigation.xml
<?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="190dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#drawable/nav_background"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/eyasu"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
app:civ_border_color="#color/colorBrand1"
app:civ_border_width="4dp"
android:layout_alignParentStart="true"
android:paddingBottom="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/userFullName"
android:textSize="14sp"
android:textColor="#FFF"
android:textStyle="bold"
android:gravity="left"
android:paddingBottom="4dp"
android:id="#+id/username"
android:layout_above="#+id/email"
android:layout_alignLeft="#+id/profile_image"
android:layout_alignStart="#+id/profile_image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/email"
android:hint="#string/userEmail"
android:gravity="left"
android:layout_marginBottom="8dp"
android:textSize="14sp"
android:textColor="#fff"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/username"
android:layout_alignStart="#+id/username" />
</RelativeLayout>
Open Android Studio
Start a new Android Studio project
Select Navigation Drawer Activity
Pick your icons here: https://material.io/icons/
1. Create an menu XML with your desired menu items and put this XML into res/menu/ folder.
menu.xml:
<menu 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"
tools:context="com.ferdous.advancefilter.MainActivity">
<item
android:id="#+id/action_notification"
android:title="Notification"
android:icon="#drawable/ic_action_notification"
app:showAsAction="always" />
<item
android:id="#+id/action_warning"
android:title="Notification"
android:icon="#drawable/ic_action_warning"
app:showAsAction="always" />
</menu>
2. In your Activity, override method onCreateOptionsMenu() to add menu items to Toolbar and override method onOptionsItemSelected() to handle the Toolbar item click event.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
switch (id) {
case R.id.action_notification:
// Do something
break;
case R.id.action_warning:
// Do something
break;
}
return super.onOptionsItemSelected(item);
}
OUTPUT:
#. If you want to add count badge over your menu item like below then you can follow my another answer Notification Badge On Action Item Android. I have added detail steps and codes.
Hope this will help~
I have a double-drawer layout set up and am trying to handle selecting items inside the drawers. The way I have it set up though, I can see that neither onNavigationItemSelected nor onOptionsItemSelected is getting called when I tap something on the menu (I put a log statement right inside the functions). I'm also not totally clear which of those functions should be called.
EDIT: It looks like onOptionsItemSelected() is being called after all, but whichever item I click on, I always get the same id. So maybe it's only allowing me to click some other layer?
There are a lot of other questions similar to this, but none of the answers have been helpful to me. I'd appreciate any insight you may have.
Here is the relevant code inside my onCreate() function in MainActivity.java:
// Adding Toolbar to Main screen
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView leftNavigationView = (NavigationView) findViewById(R.id.notifications_view);
leftNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Log.i("left", "left"); // this is never called
// Handle left navigation view item clicks here
int id = item.getItemId();
switch(id) {
case R.id.emerg_con_menuitem:
break;
case R.id.ride_hist_menuitem:
Log.i("I'm hit", "I'm hit");
intent = new Intent(MainActivity.this, loc_report.class); // The action I want to happen when this menu item is tapped
MainActivity.this.startActivity(intent);
break;
case R.id.settings_menuitem:
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
NavigationView rightNavigationView = (NavigationView) findViewById(R.id.nav_view);
rightNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Log.i("right", "right"); // this is also never called
// Handle Right navigation view item clicks here.
int id = item.getItemId();
Log.i("I am firing", "I am firing");
switch(id) {
case R.id.emerg_con_menuitem:
break;
case R.id.ride_hist_menuitem:
Log.i("I'm hit navigation", "I'm hit navigation");
intent = new Intent(MainActivity.this, loc_report.class);
MainActivity.this.startActivity(intent);
break;
case R.id.settings_menuitem:
break;
}
drawer.closeDrawer(GravityCompat.END);
return true;
}
});
// Adding menu icon to Toolbar
ActionBar supportActionBar = getSupportActionBar();
if (supportActionBar != null) {
supportActionBar.setHomeAsUpIndicator(R.drawable.ic_notifications);
supportActionBar.setDisplayHomeAsUpEnabled(true);
supportActionBar.setDisplayShowTitleEnabled(false);
}
And the onOptionsItemSelected() that is being called, although not on the correct menu:
#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, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here.
Log.i("I'm hit options", "I'm hit options"); // also never called...what is going on??
int id = item.getItemId();
switch(id) {
case R.id.emerg_con_menuitem:
break;
case R.id.ride_hist_menuitem:
Log.i("I'm hit options", "I'm hit options");
Intent intent = new Intent(MainActivity.this, loc_report.class);
this.startActivity(intent);
break;
case R.id.settings_menuitem:
break;
case R.id.menu_navigation:
drawer.openDrawer(GravityCompat.END); /*Opens the Right Drawer*/
return true;
}
return super.onOptionsItemSelected(item);
}
Here is the main view activity_mail.xml:
<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:background="#drawable/main_background"
style="#style/Theme.AppCompat.DayNight">
<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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Protectors"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/Protectors"
android:textAlignment="center">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</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="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/navheader"
app:menu="#menu/menu_navigation" />
<android.support.design.widget.NavigationView
android:id="#+id/notifications_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/navheader"
app:menu="#menu/notifications" />
...
The main menu with the clickable icon to open the drawer:
<?xml version="1.0" encoding="utf-8"?>
<menu 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"
tools:context=".MainActivity">
<group android:checkableBehavior="single">
<item // this is the item that's being called on a click
android:id="#+id/menu_navigation"
android:icon="#drawable/ic_menu"
android:title="#string/action_notifications"
android:orderInCategory="100"
app:showAsAction="always" />
</group>
</menu>
and the stuff inside the drawer (the right-hand one):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/emerg_con_menuitem"
android:title="Emergency Contacts"
/>
<item
android:id="#+id/ride_hist_menuitem"
android:title="Ride History" />
<item
android:id="#+id/settings_menuitem"
android:title="Settings" />
</menu>
Ok, I think I figured this out. I'm still having trouble with the right-drawer links, but I got the left-drawer links to work. It was a layering problem--so I was clicking on the main_menu item (which contains the clickable icon that makes the drawer open) instead of the menu items underneath. To fix this, I added this line of code to fix the order:
NavigationView leftNavigationView = (NavigationView) findViewById(R.id.nav_view);
leftNavigationView.bringToFront(); // <- added this line
And then the OnNavigationItemSelectedListener actually fires as expected.