navigation drawer wont start new activity after item click - android

I'm trying to make a navigation drawer that opens a new activity when the user taps an item.
There is 3 items in my drawer and I want each of them to open a different activity but when i click on item nothing happen.
public class MarkerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_markers);
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().hide();
mDrawerLayout = findViewById(R.id.drawer);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
R.string.open_drawer, R.string.close_drawer);
mDrawerLayout.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.map_button:
Intent intent = new Intent(MarkerActivity.this, MapActivity.class);
startActivity(intent);
break;
case R.id.comingsoon:
Toast.makeText(this, "Coming soon", Toast.LENGTH_SHORT).show();
break;
case R.id.comingsoon2:
Toast.makeText(this, "Coming soon", Toast.LENGTH_SHORT).show();
break;
}
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
My activity_markers.xml
<android.support.v4.widget.DrawerLayout
android:id="#+id/activity_markers"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.NavigationView
android:id="#+id/drawer"
app:headerLayout="#layout/header"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/white"
app:menu="#menu/drawermenu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
drawermenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/map_button"
android:title="Map"/>
<item android:id="#+id/comingsoon"
android:title="Coming Soon"/>
<item android:id="#+id/comingsoon2"
android:title="Coming Soon"/>
</menu>

You probably put each item individually in the navigation drawer layout.
If so, you need to define these elements in onCreate() method and setOnClickListener for them.
Can you please share the navigation drawer layout xml.
Otherwise, you defined a ListView, but haven't initialised it.

The id of the navigationview and drawer are incorrect
Try this:
mDrawerLayout = findViewById(R.id.activity_markers);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
R.string.open_drawer, R.string.close_drawer);
mDrawerLayout.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.drawer);
navigationView.setNavigationItemSelectedListener(this);

I found the answer. You just need to paste this line
navigationView.bringToFront(); after NavigationView navigationView = findViewById(R.id.your_id);

Related

Showing hamburger icon on a custom toolbar?

I made a NavigationView and included getSupportActionBar().setDisplayHomeAsUpEnabled(true);in my code to show the hamburger button, and it worked. but then i removed the default toolbar (by setting AppTheme in styles values to parent="Theme.AppCompat.Light.NoActionBar"), and implemented my own toolbar in the layout. And now instead of the Hamburger button, it shows the back button, although clicking on it draws the NavigationView. What should i do?
My Java code:
private DrawerLayout sideBar;
private ActionBarDrawerToggle sideBarToggle;
private Toolbar actionToolbar;
#Override
public void onCreate(...) {
sideBar = (DrawerLayout) findViewById(R.id.sqliteLayout);
sideBarToggle = new ActionBarDrawerToggle(this, sideBar, actionToolbar, R.string.sideBarOpen, R.string.sideBarClose);
actionToolbar = (Toolbar) findViewById(R.id.navAction);
sideBar.addDrawerListener(sideBarToggle);
sideBarToggle.syncState();
setSupportActionBar(actionToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(sideBarToggle.onOptionsItemSelected(item)){
return true;}
return super.onOptionsItemSelected(item);
}
My Toolbar code:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/navAction"
android:background="#color/colorPrimary"
app:theme="#style/Base.Theme.AppCompat.Light.DarkActionBar">
</android.support.v7.widget.Toolbar>
SOLUTION:
I reordered the java code to this, and removed the 3rd argument in new ActionBarDrawerToggle(...) and it worked!
actionToolbar = (Toolbar) findViewById(R.id.navAction);
setSupportActionBar(actionToolbar);
sideBar = (DrawerLayout) findViewById(R.id.sqliteLayout);
sideBarToggle = new ActionBarDrawerToggle(this, sideBar, R.string.sideBarOpen, R.string.sideBarClose);
sideBar.addDrawerListener(sideBarToggle);
sideBarToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Call setSupportActionBar(actionToolbar); before you set up your ActionBarDrawerToggle.
ActionBarDrawerToggle uses getSupportActionBar() under the hood, so all the work it is doing is wiped out when you call setSupportActionBar afterwards.
Make sure you have added:
actionBar.setDisplayHomeAsUpEnabled(true);
Not this one:
getSupportActionBar().setHomeAsUpIndicator(true);

navigation drawer main activity content

hi i have navigation drawer in my android application in the main activity java class and this is the code
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//add this line to display menu1 when the activity is loaded
displaySelectedScreen(R.id.nav_menu1);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
private void displaySelectedScreen(int itemId) {
//creating fragment object
Fragment fragment = null;
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
//initializing the fragment object which is selected
switch (itemId) {
case R.id.nav_menu1:
fragment = new gallary();
break;
case R.id.nav_menu2:
fragment = new about();
break;
case R.id.nav_menu3:
fragment = new contact();
break;
}
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
drawer.closeDrawer(GravityCompat.START);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
//calling the method displayselectedscreen and passing the id of selected menu
displaySelectedScreen(item.getItemId());
//make this method blank
return true;
}
}
and this is the main activity xml code
<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>
so i want to add some content such an images and texts to main activity xml page of the app but when i add it in main content of navigation it looks good and everything okay but when i click on for example gallary fragment from the navigation drawer it took me to new page which has the content that i put in gallary xml but the problem is the content of main activity appear too with all fragment pages that i click from navigation drawer
how can i make the content of each fragment appear without the content of main activity
anyone understand me ? #_#
I would suggest you to not to use any element other than navigationView and the fragment which will show up your screen for the navigationDrawer items, because you if clicked on some item you will be redirected to that page but later if you ever wanted to get back to that mainactivity page it becomes nearly impossible.Either you restart the activity or try some complex method to achieve that.
Instead you should use fragments. You can call a default fragment which will be loaded on opening the app in onResume() method. This fragment should also be available in navigation item for later references.You can do it like this:
fragment = new YourFragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.contentfragment,fragment).commitNow();
But if you still wanted to do so, you can set the elements of the mainactivity to visibility to gone. I don't know if this method will work.
This should be set on navigation item click
[your_element].setVisibility(View.GONE);
You fragments are overlapping each other.
Just make sure you remove the previous fragment before adding a new fragment when selecting a navigation item from the drawer:
FragmentManager fm = getSupportFragmentManager();
fm.bginTransaction()
.remove(oldFragment)
.add(newFragment)
.commit();

Navigation Drawer Header is not hiding

I tried to create one simple app with Drawer Menu. I created a project with Android Studio and select Navigation Drawer Layout. I'm trying to hide navigation drawer header and put it on toolbar... I found the way to put on toolbar but I can't find hide RED part of header in picture. Can you help me please.
Here is my layout page xml codes
<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"
android:layout_marginTop="?attr/actionBarSize" //With this code i put menu under toolbar
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
Here is my MainActivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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.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();
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager fragmentManager = getFragmentManager();
if (id == R.id.nav_first_layout) {
fragmentManager.beginTransaction().replace(R.id.content_frame,new FirstFragment()).commit();
} else if (id == R.id.nav_second_layout) {
fragmentManager.beginTransaction().replace(R.id.content_frame,new SecondFragment()).commit();
} else if (id == R.id.nav_third_layout) {
fragmentManager.beginTransaction().replace(R.id.content_frame,new ThirdFragment()).commit();
} else if (id == R.id.nav_home) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
How can I hide that red part??
Set fitsystemwindow=false under android.support.design.widget.NavigationView in xml.
Use this In your MainActivity if you want to do it programmatically else see the second option below.
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header = navigationView.getHeaderView(0);
header.setVisibility(View.GONE);
OR if you not wanna use that and want to accomplish this via xml then check below option.
In your xml file of main activity you should have NavigationView like this
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/navigation_view_I"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/drawer_menu"
app:headerLayout="#layout/navigation_drawer_header">
Remove last line from the above NavigationView which is this
app:headerLayout="#layout/navigation_drawer_header"
Add below code inside of onCreateView() of NavigationDrawer Fragment class
View headerView= LayoutInflater.from(this).inflate(R.layout.drawer_header, null);
navigationView.addHeaderView(headerView);
navigationView.getHeaderView(0).setVisibility(View.GONE);
Note:- relace R.layout.drawer_header by your header view resource file.

Toggle Inside Fragment

I have made one Activity with a NavigationView (opened with a toggle button):
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private DrawerLayout drawer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
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.app_name, R.string.app_name);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
navigationView.getMenu().getItem(0).setChecked(true);
}
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer != null) {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment = null;
Class fragmentClass;
fragmentClass = FirstFragment.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.layout, fragment).commit();
item.setChecked(true);
setTitle(item.getTitle());
drawer.closeDrawers();
return true;
}
}
When I select one item of NavigationView it starts a Fragment:
public class FirstFragment extends Fragment {
private View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_layout, container, false);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) view.findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
getActivity(), drawer, toolbar, R.string.app_name, R.string.app_name);
drawer.addDrawerListener(toggle);
toggle.syncState();
return view;
}
}
Problem:
When I select the button ≡ to open the NavigationView inside Fragment and I select the items it doesn't open FirstFragment.
If I swipe from the left to open NavigationView inside Fragment and I select the items it opens FirstFragment.
How can I reuse onNavigationItemSelected from MyActivity on FirstFragment?
Edit:
This is the acitivity_layout.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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ddffff"
android:fitsSystemWindows="true">
<LinearLayout
android:id="#+id/list_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
//FragmentLayout
<RelativeLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<include
android:id="#+id/bar"
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
...
</RelativeLayout>
</LinearLayout>
<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:background="#ddffff"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
The root cause of this is that you're using the same layout in your Fragment as you are in the Activity, and the Toolbar is inside the container ViewGroup you transact the Fragment into. Once a Fragment has been loaded with its own DrawerLayout and Toolbar, it covers the Activity's Toolbar, so nothing looks out of the ordinary. However, the toggle in the Fragment is operating the drawer in the Fragment, and that drawer is a second NavigationView that hasn't been setup like the one in the Activity, so nothing happens when you click on the drawer opened with the toggle. But, if you drag the drawer open, the one that's opening is actually the Activity's drawer, and that NavigationView was setup correctly, so clicking on that one works as expected.
The first correction is to move the Toolbar - its <include> element, that is - to outside of the RelativeLayout you're using as the Fragment container. This will prevent the Activity's Toolbar from being obscured when Fragments are loaded.
The second step is to define separate, different layout XML files for the Fragments. You don't want to use the same layout in the Fragments as you are in the Activity, mostly because that's probably not really what you intend, and also because you really don't want to have multiple, redundant DrawerLayouts, Toolbars, and NavigationViews, which can cause problems, as you've seen.

Android 5 Hamburger doen't open the drawer

I'm trying to make a simple navigation drawer, matching the material guidelines. I'm following along the official training. Everything is running well, only a tap on the hamburger icon won't open the drawer. I can open the drawer with a swipe from the side, only the hamburger isn't working. I already looked up some other questions like this one, but nothing helped. What did I miss?
Here is my code:
Activity.java:
public class stream extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private String[] mDrawerTitles;
private ListView mDrawerList;
private Toolbar mToolbar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stream);
// Init the Support-toolbar
mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (mToolbar != null) {
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.setDrawerListener(mDrawerToggle);
// enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onBackPressed() {
if(mDrawerLayout.isDrawerOpen(Gravity.START|Gravity.LEFT)){
mDrawerLayout.closeDrawers();
return;
}
super.onBackPressed();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggle
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
Layout.xml
<RelativeLayout
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"
tools:context="de.square7.gtz.lwenzahn.stream"
android:background="#color/ColorBackground">
<!-- Toolbar -->
<include layout="#layout/toolbar"/>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Content View -->
<FrameLayout
android:id="#+id/content_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="56dp"/>
<!-- Drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="270dp"
android:layout_height="match_parent"
android:focusableInTouchMode="false"
android:choiceMode="singleChoice"
android:background="#ffffffff"
android:layout_gravity="start"
android:elevation="8dp" >
</ListView>
</android.support.v4.widget.DrawerLayout>
You are not using the DrawerLayout as the root layout of the activity. You are almost there but you see how you use the RelativeLayout as the root (or at least that is what I infer from the posted xml - the RelativeLayout does not look like it has a closing tag so maybe you just missed it)?
The NavigationDrawer opens on the tap of the "hamburger" (or icon) only when it is the root so that Android knows that the DrawerLayout is encompassing everything. My guess is the toolbar you are including is interfering with the layout.

Categories

Resources