Toolbar back button is not calling onOptionsItemSelected Android - android

I can able to see the back button in the toolbar but when i click, nothing happens. It is not going to onOptionsItemSelected but when i remove the whole implementation of ActionBarDrawerToggle then the back button is working fine. I need to switch between both when i needed. Thank in advance.
package demo.sample.com.sample.base;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private DrawerLayout mDrawer;
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_digi_care);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, mDrawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawer.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
mDrawer.setFocusable(false);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setItemIconTintList(null);
navigationView.setNavigationItemSelectedListener(this);
navigationView.getMenu().getItem(0).setChecked(true);
mDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Logger.i(TAG, "onOptionsItemSelected called");
switch (item.getItemId()) {
case android.R.id.home:
Logger.i(TAG, "Back button pressed"); //Never getting called
//onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.map_menu, menu);
return super.onCreateOptionsMenu(menu);
}
}

If you want the onOptionsItemSelected() method to fire when the toggle Button is clicked, you need to use the four-parameter constructor for ActionBarDrawerToggle that doesn't take a Toolbar argument.
public ActionBarDrawerToggle(Activity activity,
DrawerLayout drawerLayout,
int openDrawerContentDescRes,
int closeDrawerContentDescRes)
Otherwise, the toggle will just handle the drawer opening/closing directly itself.

And i finally got a solution. instead getting toolbar home button click ononOptionsItemSelected() it can be handled through DrawerToggle.setToolbarNavigationClickListener.
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// event when click home button
Log.d("cek", "home selected");
}
});
Thanks to #meow meo. Source - Cannot catch toolbar home button click event

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.

default back button in navigation drawer doesn't work

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;
}
}

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.

How to open the Sliding Navigation Drawer when ImageView is clicked?

I'm trying to implement the Navigation Drawer Activity on my Dashboard Activity. In my application, I have created a Theme where the 'Toolbar/ActionBar' is removed and just put an ImageView for my burger menu on a custom layout and include it on my dashboard layout. What I wanted is that when this ImageView is clicked the sliding drawer will display.
What I tried to do is add an Navigation Drawer Activity by com.project.projectname -> New -> activity -> 'Navigation Drawer Activity' and automatically it adds NavigationActivity class , activity_navigation.xml
app_bar_navigation.xml, etc.
But I was wondering how can I open the drawer?
In my Dashboard Activity I added this
public class DashboardActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
View side_tab = findViewById(R.id.side_tab);
expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);
expanded_menu.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// open drawer here
}
});
Also, in NavigationActivity it uses ActionBarDrawerToggle, I must not use this because I don't apply an ActionBar. But what alternative can I use?
Here is my NavigationActivity
public class NavigationActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
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();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation, 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();
Intent goToSreen;
if (id == R.id.nav_dashboard) {
goToSreen = new Intent(NavigationActivity.this, DashboardActivity.class);
startActivity(goToSreen);
} else if (id == R.id.nav_others) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Thank you for your kind help.
Update
So in my Dashboard Activity I added the following
NavigationActivity navigationActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
View side_tab = findViewById(R.id.side_tab);
expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);
expanded_menu.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
navigationActivity = new NavigationActivity() ;
navigationActivity.drawer.openDrawer(GravityCompat.START);
}
});
And in my Navigation Activity I add a global variable
public class NavigationActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
I have managed to call the drawer in my Dashboard Activity but the sliding menu still doesn't display when I clicked my burger menu. The app crashes and displays java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(int)' on a null object reference
Add this in your NavigationActivity
public class NavigationActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
//Add this line
public static DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
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();
.......
And next change this
expanded_menu.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// open drawer here
NavigationActivity. drawer.openDrawer(GravityCompat.START);
}
});
Try this hope it will solve your problrm.
Add the following code in NavigationActivity:
DrawerLayout mDrawerLayout; // Declare globally
In onCreate() :
mDrawerLayout = (DrawerLayout) findViewById(R.id.nav_drawer);
expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);
expanded_menu.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// open drawer here
mDrawerLayout.openDrawer(Gravity.LEFT);
}
});
If the drawer is open, close it onBackPressed():
#Override
public void onBackPressed() {
if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
mDrawerLayout.closeDrawer(Gravity.LEFT);
return;
}
}
Hope this helps.
I extend my NavigationActivity to my DashboardActivity like below
public class DashboardActivity extends NavigationActivity {
side_tab = findViewById(R.id.side_tab);
expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);
expanded_menu.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
side_tab.setVisibility(View.INVISIBLE);
drawer.openDrawer(GravityCompat.START);
}
});
and then follow the said answered by #Hassan Usman and #tahsinRupam
after that I added the DrawerLayout in my dashboard.xml

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.

Categories

Resources