I am trying to add click functionality on the Actionbar back button. I have tried the below things till now, but unable to get the desired result.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Observed that the onClick method is not being called when i click on the back button in the action bar
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
};
drawer.setDrawerListener(toggle);
drawer.post(new Runnable() {
#Override
public void run() {
toggle.syncState();
}
});
toggle.setDrawerIndicatorEnabled(true);
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onKeyDown(4, null);
}
});
Use the updated code
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
switch (item.getItemId()) {
case android.R.id.home:
showToast("Test");
return true;
}
you can try like this
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
use following code inside your Activity :
//for going back or finish current activity.
#Override
public boolean onSupportNavigateUp() {
finish();
startActivity(new Intent(ResultActivity.this, MainActivity.class));
return true;
}
Below code are for actionBar 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) {
int id = item.getItemId();
if (id == R.id.logout) {
// toast Here
return true;
}
if (id == R.id.home) {
// toast Here
return true;
}
return super.onOptionsItemSelected(item);
}
onCreateOptionsMenu() is used for creating your menu & setting your menu.xml
onOptionsItemSelected() is used for selection of menu items & actions.
Related
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
I have created an app which uses one activity (Navigation Drawer) and a fragment. But I'm unable to use toolbar back button to navigate back from the fragment to the main activity. Hardware back button works perfectly. I know that I need to override onOptionsItemSelected, catch android.R.id.home, check if there is something in the back stack and then pop it. After changing the fragment, "burger" button changes to "back arrow", but when I click on it the overridden method "onOptionsItemSelected" is never called, rather on click of the back button NavigationDrawer menu opens.
NOTE: I have referred many answers in the StackOverflow including THIS which is the same as that of my problem but that did not work for me. I have been working on this for a week any help is greatly appreciated. Please do not down vote. 1
Here is my code
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
public void init() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
Log.d("Backstack Count", String.valueOf(getSupportFragmentManager().getBackStackEntryCount()));
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
showUpButton(false);
} else {
showUpButton(true);
}
}
});
}
private void showUpButton(boolean show) {
// To keep states of ActionBar and ActionBarDrawerToggle synchronized,
// when you enable on one, you disable on the other.
// And as you may notice, the order for this operation is disabled first, then enable - VERY VERY IMPORTANT.
if (show) {
// Remove hamburger
mDrawerToggle.setDrawerIndicatorEnabled(false);
// Show back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// when DrawerToggle is disabled i.e. setDrawerIndicatorEnabled(false), navigation icon
// clicks are disabled i.e. the UP button will not work.
// We need to add a listener, as in below, so DrawerToggle will forward
// click events to this listener.
} else {
// Show hamburger
mDrawerToggle.setDrawerIndicatorEnabled(true);
invalidateOptionsMenu();
}
// So, one may think "Hmm why not simplify to:
// .....
// getSupportActionBar().setDisplayHomeAsUpEnabled(enable);
// mDrawer.setDrawerIndicatorEnabled(!enable);
// ......
// To re-iterate, the order in which you enable and disable views IS important #dontSimplify.
}
#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) {
// super.onCreateOptionsMenu(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) {
switch (item.getItemId()) {
case android.R.id.home:
// onBackPressed();
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
}
return true;
}
// return false;
// return super.onOptionsItemSelected(item);
return true;
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
displayImportFrag();
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} 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;
}
public void displayImportFrag() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Log.d("called", "new frag");
ImportFragment importFragment = new ImportFragment();
ft.add(R.id.fragment_frame, importFragment).addToBackStack(null).commit();
}
}
ImportFragment.java
public class ImportFragment extends Fragment {
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.import_fragment, container, false);
return rootView;
}
}
Forgive my English :(
I have a problem with "NavigationDrawer". I have the Fragments "Home " that have no special activities, "Import, Gallery and SlideShow", all with activities running perfectly.
However, if I click on Import (HOME> IMPORT) the activity IMPORT opens, but if I click on (IMPORT> GALLERY / SLIDESHOW / HOME) the Import activity remains open.
I have to press the back button to go to HOME, to click on another activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
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.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
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.home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
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.
//here is the main place where we need to work on.
int id=item.getItemId();
switch (id){
case R.id.nav_home:
Intent h= new Intent(Home.this,Home.class);
startActivity(h);
break;
case R.id.nav_import:
Intent i= new Intent(Home.this,Import.class);
startActivity(i);
break;
case R.id.nav_gallery:
Intent g= new Intent(Home.this,Gallery.class);
startActivity(g);
break;
case R.id.nav_slideshow:
Intent s= new Intent(Home.this,Slideshow.class);
startActivity(s);
break;
// oh nightmare
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
IMPORT ACTIVITY
public class Import extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer;
NavigationView navigationView;
Toolbar toolbar=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_import);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button Button9 = (Button) findViewById(R.id.Button9);
Button Button11 = (Button) findViewById(R.id.Button11);
//We dont need this.
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) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Button9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(Import.this, PHP5.class);
startActivity(it);
}
});
Button11.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(Import.this, PHP7.class);
startActivity(it);
}
});
}
#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.home, 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.
//here is the main place where we need to work on.
int id=item.getItemId();
switch (id){
case R.id.nav_home:
Intent h= new Intent(Import.this,Home.class);
startActivity(h);
break;
case R.id.nav_import:
Intent i= new Intent(Import.this,Import.class);
startActivity(i);
break;
case R.id.nav_gallery:
Intent g= new Intent(Import.this,Gallery.class);
startActivity(g);
break;
case R.id.nav_slideshow:
Intent s= new Intent(Import.this,Slideshow.class);
startActivity(s);
break;
// after this lets start copying the above.
// FOLLOW MEEEEE>>>
//copy this now.
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Maybe you forgot to call finish() in the IMPORT Activity? If you call it after your Intent to another Activity, the current Activity will close, and your Home Activity will be shown again.
You need to remove all previous Activities from stack, it can be done trough a flag in the intent
Intent it = new Intent(Import.this, OtherAct.class);
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(it);
Else a combination of the keywors may work for you:
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
Check the doc about how it works:
https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK
https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK
I am using the ActionBarDrawerToggle with NavigationView. My content is displayed using fragments.
I am following this stackoverflow question to get the back button press to work but control never flows to onOptionsItemSelected.
This is my MainActivity.class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.openDrawerContentDescRes, R.string.closeDrawerContentDescRes);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() == 0) {
super.onBackPressed();
} else {
removeFragmentFromBackstack();
updateToolbarWithHomeButton();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case android.R.id.home:
// doesn't reach here ever.
return true;
case R.id.action_x:
// do something
return true;
case R.id.action_y:
// do something
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
private void updateToolbarWithBackButton() {
ActionBar actionBar = getSupportActionBar();
if (null != mDrawerToggle && null != actionBar) {
mDrawerToggle.setDrawerIndicatorEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
private void updateToolbarWithHomeButton() {
ActionBar actionBar = getSupportActionBar();
if (null != mDrawerToggle && null != actionBar) {
actionBar.setDisplayHomeAsUpEnabled(false);
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerToggle.syncState();
}
}
How can I capture the Back button <- click from Toolbar?
Update:
Thanks to #mike the back arrow button on the toolbar is now captured within the onOptionsItemSelected in my MainActivity code as updated below.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.openDrawerContentDescRes, R.string.closeDrawerContentDescRes);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() == 0) {
super.onBackPressed();
} else {
removeFragmentFromBackstack();
updateToolbarWithHomeButton();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//TODO: skip back press if fragment backstack count is 0.
onBackPressed();
updateToolbarWithHomeButton();
return true;
case R.id.action_x:
// do something
return true;
case R.id.action_y:
// do something
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
private void updateToolbarWithBackButton() {
ActionBar actionBar = getSupportActionBar();
if (null != mDrawerToggle && null != actionBar) {
mDrawerToggle.setDrawerIndicatorEnabled(false);
}
}
private void updateToolbarWithHomeButton() {
ActionBar actionBar = getSupportActionBar();
if (null != mDrawerToggle && null != actionBar) {
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerToggle.syncState();
}
}
If you want the onOptionsItemSelected() method to fire upon clicking the toggle, remove the toolbar argument from the ActionBarDrawerToggle constructor call.
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.openDrawerContentDescRes, R.string.closeDrawerContentDescRes);
Otherwise, the toggle handles opening and closing the drawer internally, and the call to ActionBarDrawerToggle#onOptionsItemSelected() isn't necessary.
If you want to handle clicking the home Button differently depending on the current state, you'll also want to remove the if block that returns at the top of the onOptionsItemSelected() method.
And, you should call setDisplayHomeAsUpEnabled(true) just once in onCreate(). You don't need to keep switching that on and off. Enabling and disabling the drawer indicator will take care of that.
Remove this line
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
from onOptionsItemSelected() so it will look like this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// doesn't reach here ever.
return true;
case R.id.action_x:
// do something
return true;
case R.id.action_y:
// do something
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I am using the appcompat activity for Android v-21. I want to enable the home button which I have set it to true in my code. I also have overridden the onOptionsItemSelected but it's still not working.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apply_card);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
//Action bar
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
onBackPressed();
return true;
}
Simple way to add action bar home enable in Appcompat activity
getSupportActionBar().show();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Add this two pulic functions in your activity also--
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
}
Following code snippet set navigation icon in toolbar,
toolbar.setNavigationIcon(R.mipmap.ic_launcher);
I hope it will help you.
I know it is an old question but in order to prevent others to devote their time to solve this issue, I want to share the working method for me.
I am not sure about the reason of this. Probably, since ActionBar is deprecated and gave way to Toolbar after AppCompat, some methods of AppCompatActivity may not work as it is expected. Although the Burger (navigation button) is defined as the home button of ActionBar, we could not control click events of this button by using .onOptionsItemSelected(MenuItem). Toolbar view presents us another method to achieve this, toolbar.setNavigationOnClickListener(View.OnClickListener).
To exemplify, I tried to use balysv's MaterialMenuIcon with this method instead of .onOptionsItemSelected(MenuItem) as follows:
private void setupToolbar() {
toolbar = (Toolbar) ((LinearLayout) findViewById(R.id.app_bar)).getChildAt(0);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (drawerLayout.isDrawerVisible(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
materialMenu.animatePressedState(MaterialMenuDrawable.IconState.BURGER);
} else {
drawerLayout.openDrawer(GravityCompat.START);
materialMenu.animatePressedState(MaterialMenuDrawable.IconState.ARROW);
}
}
});
materialMenu = new MaterialMenuIconToolbar(this, Color.WHITE, MaterialMenuDrawable.Stroke.THIN) {
#Override public int getToolbarViewId() {
return R.id.toolbar;
}
};
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
materialMenu.setTransformationOffset(
MaterialMenuDrawable.AnimationState.BURGER_ARROW,
isDrawerOpened ? 2 - slideOffset : slideOffset
);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
isDrawerOpened = true;
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
isDrawerOpened = false;
}
#Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
if (newState == DrawerLayout.STATE_IDLE) {
if (isDrawerOpened) materialMenu.setState(MaterialMenuDrawable.IconState.ARROW);
else materialMenu.setState(MaterialMenuDrawable.IconState.BURGER);
}
}
});
}
I hope it helps.
I am using Xamarin Android, and for AppCompatActivity I also used this method which did not work for me.
SupportActionBar.SetHomeButtonEnabled(true);
but after finding on internet and I found another method, which worked for me, and showed the home navigation button.
SupportActionBar.SetDisplayHomeAsUpEnabled(true);