In my application when the user clicks on the application logo or title, system will open the navigation drawer.
I want to make the navigation drawer to be open only if the navigation icon clicked, as i want to do some other logic if the user clicks application logo and title.
How to perform that ?
This code is intended to open navigation drawer upon clicking application logo, title or navigation icon.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
//return false;
}
}
Any advice is appreciated
I Would advice you to create a custom Toolbar using a separate XML layout and use it in your Main layout,as it is easy.
so the imageview and text View can serve for a specific purpose when clicked!
Related
I have a navigation drawer layout in my fragment and a floating action menu in the layout. Now when the floating action menu expands then i have to handle the click of the navigation drawer hamburger icon.
When floating action menu expands then the navigation icon should not work and don't allow drawer to open.
#SuppressWarnings("deprecation")
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
break;
}
I have tried this but, it isn't working.
If someone has a better answer that is useful please let me know.
I have an activity with 2 fragments and drawer. So when i am in fragment i get "Up" icon instead hamburger icon (with the help of setDisplayHomeAsUpEnabled) but the action is still the same - navigation_drawer_open/close. So how to get onBackPressed() instead?
And according to this comment i dont know how to handle the Home/Up by myself because of "automatically handle clicks".
#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.
Automatic handling usually only works between activities. Since you are using fragments you may need to handle them manually.
Here's an example on how to handle a back button press.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: // This is the home/back button
onBackPressed(); // Handle what to do on home/back press
break;
}
return false;
}
For do this you must use Custom Toolbar and instead of android menu key use from this library :
material-menu
In this library you can change state for arrow and rotate -90 for show Up Icon instead of Burger icon.
How to check whether the left button in action bar is menu button or back button? To enable back button we use following:
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
I want to know which button is enable at a particular time. I tried by using stack count but it didn't worked for me.
Menu icon
Back icon
Thanks
You mean this?
set ID:
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add((int groupId, int itemId, int order, charsequence title) .setIcon(drawable ID)
return true;
}
get ID:
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case Menu.First+1 :
break;
case Menu.First+2 :
break;
}
return true;
}
If you are using navigation drawer, then you can do this by knowing state of navigation drawer either open or closed.
Assuming you have defined a drawerlayout in XML:
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
//drawer is open
}
if your drawer is open it means you have back icon, else you have bar icon on action bar
Is there any way to use the standard menu button in the action bar to do something? In my case a sliding menu, I already have code that works in the onClick of a normal button. I just want to use the menu button in the action bar instead. Is this possible? Or will I have to customize the action bar and not use the button that is already there?
I believe you mean the ActionBar Home icon.
First you have to enable it
getActionBar().setHomeButtonEnabled(true);
Then you have to handle the event
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
//do your work
return true;
default:
return super.onOptionsItemSelected(item);
}
}
FYI if you want to follow the Navigation drawer pattern you should read this tutorial.
In my project I have used action bar sherlock library. I want to make back button in action bar I used following code
getSupportActionBar().setHomeButtonEnabled(true);
And
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
default:
return super.onOptionsItemSelected(item);
}
return false;
}
Every thing is working fine, but back Arrow (to the left of icon) is not displaying. I want to show Back Arrow also. Can anyone tell me what I am doing wrong.
You have to set it up this way:
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
Hope that helps.