Can not open navigation drawer on action bar item click - android

i want the right drawer to be opened on menu click
i have used the following code but its not working for some reason
mDrawerToggle = new ActionBarDrawerToggle(getActivity(),mDrawerLayout,toolbar, R.string.drawer_open,
R.string.drawer_close) {
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == R.id.more) {
if (mDrawerLayout.isDrawerOpen(Gravity.END)) {
saveToPreferences(getActivity(),KEY_USER_LEARNED_DRAWER,mUserLearnedDrawer+"");
mDrawerLayout.closeDrawer(Gravity.END);
} else {
mDrawerLayout.openDrawer(Gravity.END);
}
}
return false;
}
};
please help me out
SOLVED IT BY MODIFYING THE MAINACTIVITY
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.more:
if (notificationDrawerFragment.mDrawerLayout.isDrawerOpen(Gravity.END)){
notificationDrawerFragment.mDrawerLayout.closeDrawer(Gravity.END);
}
else
{ notificationDrawerFragment.mDrawerLayout.openDrawer(Gravity.END);
}
default:
return super.onOptionsItemSelected(item);
}
}

Solved the following by modifying the menu option within the main activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.more:
notificationDrawerFragment.mDrawerLayout.openDrawer(Gravity.END);
default:
return super.onOptionsItemSelected(item);
}
}

Do you have this override:
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
also you should have overrides under ActionBarDrawerToggle:
public void onDrawerOpened(View drawerView) and
public void onDrawerClosed(View drawerView)

Try this, I had learnt this from SO:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == android.R.id.more) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
saveToPreferences(getActivity(),KEY_USER_LEARNED_DRAWER,mUserLearnedDrawer+" ");
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
return false;
}
};
Hope this helps.

Related

actionBarDrawerToggle.setDrawerIndicatorEnabled(false) back arrow has no action

I use a navigation drawer, which I want to disable for inner Fragments.
I used following code to setup the drawer:
private void initSideMenu() {
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.Open, R.string.Close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
binding.nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menuData:
selectPersonalDataFragment();
break;
case R.id.menuBlogPosts:
closeDrawerAndNavigateTo(new BlogPostFragment(), true);
break;
case R.id.menuSettings:
closeDrawerAndNavigateTo(new BackgroundImageFragment(), true);
break;
}
return true;
}
});
}
To disable the navigation drawer I use following code:
#Override
public void setDrawerEnabled(boolean enabled) {
Log.d(TAG, "setDrawerEnabled: " + enabled);
if(enabled) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
} else {
actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
actionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
}
The burger icon changes to a back arrow, but this arrow does not have any functionality. So why is my listener not listening?
After a lot of trying I found out, that I did not have to set a toolbar navigation click Listener, but had to override onSupportNavigateUp():
Changed setDrawerEnabled():
#Override
public void setDrawerEnabled(boolean enabled) {
Log.d(TAG, "setDrawerEnabled: " + enabled);
if(enabled) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
} else {
actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
}
}
New overriden onSupportNavigateUp():
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}

How to handle back arrow event in a SearchView

How can I handle the click event on the back arrow in the searchview widget:
I tried this code but it doesn't work:
searchtollbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "back arrow clicked");
}
});
also I tried this one:
MenuItemCompat.setOnActionExpandListener(item_search, new
MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true;
}
});
The problem with the above code is that calling onMenuItemActionCollapse() method also executes onQueryTextChange() which is undesirable.
So some help please.
To handle that you have to override onOptionsItemSelected method.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case android.R.id.home:
// handle back event.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try with
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = new SearchView(((MainActivity) mContext).getSupportActionBar().getThemedContext());
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
Log.d( TAG, "expand" );
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
Log.d( TAG, "collapse" );
return true;
}
});

How to make home button act as back button?

I have two activities.One of them has Fragment.
From this fragment I can go to another activity, but by clicking the button "home" it goes to previous activity instead of going to the fragment of the previous activity.
Image
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_closet);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Refere below code :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You need to override below method :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()== android.R.id.home) {
finish();
return true;
}
return false;
}
getSupportActionBar().setNavigationOnClickListener(new OnClickListener {
public void onClick(View v){
onBackPressed()
}
})
Since you want same behavior as of back button you should use onBackPressed();
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Menu item call twice in fragment android

In fragment when i click on menu item , it calls function twice one is from previous fragment and other one is from existing fragment.
Fragment 1:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.refresh){
RetryCallMap();
return true;
}else
return false;
}
Fragment 2:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.refresh){
RetryCall();
return true;
}else
return false;
}
MainActivity :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.map) {
pushFragments("mapFragment", new MapFragment(), true, true);
return true;
}
if (id == android.R.id.home) {
this.drawerLayout.openDrawer(drawableListView);
return true;
}
return super.onOptionsItemSelected(item);
}
These are the fragments and my MainActivity i am refreshing fragment by on menu item click so that if internet connection not found or any other problem occurs while fetching for i can refresh the page.
Returning true in onOptionsItemSelected() function worked for me.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.refresh){
RetryCall();
return true;
}else
return true;
}

toolbar back arrow doesn't work

I create other activity and hope use toolbar back arrow to return main activity, but under code doesn't work, please help me.
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setTitle(R.string.setting);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
System.out.println("??");
finish();
return true;
}
return true;
}
});
}
Try This Method:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
return true;
}
return (super.onOptionsItemSelected(menuItem));
}
The Toolbar has a method for this, setNavigationOnClickListener. It allows you to listen for click events on the back arrow. Here is the documentation if you want to read more about the Toolbar.
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("??");
finish();
}
});
You should use this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}

Categories

Resources