How to open Navigation Drawer when click on navigation drawer icon - android

How to open Navigation Drawer when click on navigation drawer icon.
I want open Navigation Drawer like this
![Navigation Drawer][1]
I added a menu but still not showing
toggle = new ActionBarDrawerToggle(
DashBordActivity.this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
if (navigationView != null) {
setupDrawerContent(navigationView);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
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
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
//#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void setupToolbar(String title) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(title);
toolbar.setTitleTextColor(Color.BLACK);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private void setupDrawerContent(NavigationView navigationView) {
//revision: this don't works, use setOnChildClickListener() and setOnGroupClickListener() above instead
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
drawer.closeDrawers();
return true;
}
});
}
navigation drawer is not open when i click on the drawer how to solve this problem
is there any code is missing in my code.

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.menu);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawer.openDrawer(START);
}
});

Try this,
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="#string/import" />
</group>

Try this:
This tutorial is very help full like:-
Add the navigation drawer layout
set icon and text in drawer layout
I refer this link
https://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
it is very helpful try this

Related

Click on hamburger icon to open/close DrawerLayout [duplicate]

I have this nav drawer which was working perfectly fine.
Refactoring my code I removed all onOptionsItemSelecteds in activities and made all activities inherit from a base activity which extends AppComplatActivity and implements all the necessary methods.
After this clicking on hamburger icon does not work any more even though I have syncstate() and every thing.
Any clues why this is not working?
One of the activities:
public class MainActivity extends BaseActivity implements SearchFilterFragment.OnFragmentInteractionListener {
NavigationView navigationView;
DrawerLayout drawerLayout;
private Tracker mTracker;
#Override
protected void onResume() {
super.onResume();
drawerLayout.openDrawer(GravityCompat.START);
}
#Override
protected void onPostResume() {
super.onPostResume();
mTracker.setScreenName("MainActivity" + "-----");
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.openDrawer(GravityCompat.START);
navigationView = (NavigationView) findViewById(R.id.navigation_view_primary);
navigationView.setNavigationItemSelectedListener(new NavigationDrawerListener(this));
setupToolbar();
Haftdong application = (Haftdong) getApplication();
mTracker = application.getDefaultTracker();
}
private void setupToolbar() {
// Show menu icon
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);// will make the icon clickable and add the < at the left of the icon.
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();//for hamburger icon
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
BaseActivity:
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#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_base, 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);
}
}
You're using the four-parameter constructor for ActionBarDrawerToggle, which means you'll have to call the toggle's onOptionsItemSelected() method in MainActivity's onOptionsItemSelected() override in order to open/close the drawer.
For example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
If you happen to be providing your own Toolbar – e.g., as the support ActionBar (though it's not necessary to set it as such) – then you can instead pass that Toolbar as the third argument in the ActionBarDrawerToggle constructor call. For example:
Toolbar toolbar = findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
The drawer opening/closing will then be handled by ActionBarDrawerToggle internally, and you won't need to call through to the toggle in onOptionsItemSelected().
The setDisplayHomeAsUpEnabled() call is also unnecessary for this setup, which is handy if you don't want to set the Toolbar as the ActionBar.

Why menu items in android studio don't work on click?

I'm developing a project on android and completely new to this. I had made a drawer menu with some of menu items. Now when i want to put another activity on click from menu items, the drawer closes automatically, it doesn't work. Here
private DrawerLayout mdrawerLayout;
private ActionBarDrawerToggle mdrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mdrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mdrawerToggle = new ActionBarDrawerToggle(this, mdrawerLayout, R.string.open, R.string.close);
mdrawerLayout.addDrawerListener(mdrawerToggle);
mdrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mdrawerToggle.onOptionsItemSelected(item)) {
return true;
}
int id = item.getItemId();
if(id == R.id.setup){
Intent csetup = new Intent(MainActivity.this, Subactivity.class);
startActivity(csetup);
return false;
}
return super.onOptionsItemSelected(item);
}
}
`
Try this:
Add below code in your onCreate method:
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);
Implement lister in your mainactivity class:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
And after that in your onNavigationItemSelected method look like this:
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.setup) {
//call new activity
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Hope this may help you
This is because you are overriding the wrong method. You have to override onNavigationItemSelected() for drawer, instead of onOptionsItemSelected() which is for top-right settings menu. See the following code for reference:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.setup) {
Intent csetup = new Intent(MainActivity.this, Subactivity.class);
startActivity(csetup);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
First find the view for you NavigationView using findViewById method like below-NavigationView navView=(NavigationView)findViewById(R.id.navView)
then use this.
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.setup) {
Intent csetup = new Intent(MainActivity.this, Subactivity.class);
startActivity(csetup);
}
return false;
}
});
then it will work.. onOptionsItemSelected(MenuItem item) is used to get the access to the MenuItems we have inflated on toolbar or on other layouts using onCreateOptionsMenu() method.

implementing Activities instead of fragments in Navigation drawer

I have been working on this nav drawer with activites. I have a couple of activites in my projects that i want to switch between using the nav drawer.
So far I have achieved this
Create Nav drawer and Write some activities code.
Show same nav drawer in all activities.
Start a default activity when app starts.
But now my issue is that when i open my app for first time, it loads my desired activity and i can see this nav drawer. when i switch activity and press the back button, I'm getting this blank activity with nav drawer. I know its some kinda beginner level issue. But please if someone can suggest the edit. Here is my code.
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView = null;
Toolbar toolbar = null;
DrawerLayout drawer;
FrameLayout frameLayout;
private static boolean isLaunch = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = (FrameLayout) findViewById(R.id.contant_frame);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
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, "", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
if (isLaunch) {
isLaunch = false;
startActivity(new Intent (this,MyAct.class));
}
}
#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_manage) {
startActivity(new Intent(this,About.class));
} else if (id == R.id.nav_share) {
startActivity(new Intent(this,About.class));
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
I ve this same app working perfect in simple listview type nav drawer. but now i m trying to implement fancy material design.
Try overriding the onBackButtonPressed method.
Clearing/removing the onBackpressed method in MyAct and About class will bring back the last view with anything that was populated.
But if you want to open some other view or want to start new activity when back key is pressed, you can add your code here in this onBackPressed method
Hope it helps. Cheers!

Clicking hamburger icon on Toolbar does not open Navigation Drawer

I have this nav drawer which was working perfectly fine.
Refactoring my code I removed all onOptionsItemSelecteds in activities and made all activities inherit from a base activity which extends AppComplatActivity and implements all the necessary methods.
After this clicking on hamburger icon does not work any more even though I have syncstate() and every thing.
Any clues why this is not working?
One of the activities:
public class MainActivity extends BaseActivity implements SearchFilterFragment.OnFragmentInteractionListener {
NavigationView navigationView;
DrawerLayout drawerLayout;
private Tracker mTracker;
#Override
protected void onResume() {
super.onResume();
drawerLayout.openDrawer(GravityCompat.START);
}
#Override
protected void onPostResume() {
super.onPostResume();
mTracker.setScreenName("MainActivity" + "-----");
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.openDrawer(GravityCompat.START);
navigationView = (NavigationView) findViewById(R.id.navigation_view_primary);
navigationView.setNavigationItemSelectedListener(new NavigationDrawerListener(this));
setupToolbar();
Haftdong application = (Haftdong) getApplication();
mTracker = application.getDefaultTracker();
}
private void setupToolbar() {
// Show menu icon
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);// will make the icon clickable and add the < at the left of the icon.
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();//for hamburger icon
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
BaseActivity:
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#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_base, 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);
}
}
You're using the four-parameter constructor for ActionBarDrawerToggle, which means you'll have to call the toggle's onOptionsItemSelected() method in MainActivity's onOptionsItemSelected() override in order to open/close the drawer.
For example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
If you happen to be providing your own Toolbar – e.g., as the support ActionBar (though it's not necessary to set it as such) – then you can instead pass that Toolbar as the third argument in the ActionBarDrawerToggle constructor call. For example:
Toolbar toolbar = findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
The drawer opening/closing will then be handled by ActionBarDrawerToggle internally, and you won't need to call through to the toggle in onOptionsItemSelected().
The setDisplayHomeAsUpEnabled() call is also unnecessary for this setup, which is handy if you don't want to set the Toolbar as the ActionBar.

How to change selected Item in the navigation drawer depending on the activity/view?

I have a Navigation Drawer Activity and different activities in which I get. I want the Items in the nav drawer being selected depending on the activity or view whatever.
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);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), MainAddMedActivity.class);
/*EditText editText = (EditText) findViewById(R.id.search_medicament);
String medicament_search = editText.getText().toString();*/
/*intent.putExtra(EXTRA_MESSAGE, medicament_search);*/
startActivity(intent);
}
});
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);
navigationView.getMenu().getItem(0).setChecked(true);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
/*navigationView.getMenu().getItem(0).setChecked(true);*/
}
#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_healthdiary) {
} else if (id == R.id.nav_appointment) {
} else if (id == R.id.nav_physician) {
} else if (id == R.id.nav_protocol) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
For example, I get into the MainAddMedActivity and Press back. Then I want some code to check in which view or activity I am and select the item in the navigation drawer.
Seems like you have not yet implemented the switching of the content area. I suggest you use fragments for this.
So, if you use fragments, override onAttachFragment of your activity like:
#Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
int id;
if(fragment instanceof HealthDiaryFragment) id = R.id.nav_healthdiary;
else
if(fragment instanceof AppointmentFragment) id = R.id.nav_appointment;
...
else return;
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setCheckedItem(id);
}
Also, modify your onBackPressed:
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
if(getFragmentManager().getBackStackEntryCount()>1) {
getFragmentManager().popBackStack();
}
else {
super.onBackPressed();
}
}
/*navigationView.getMenu().getItem(0).setChecked(true);*/
}
This is assuming that in your handling of drawer selection you replace fragments with pushing them on the back stack.

Categories

Resources