Cannot Select Item in Navigation Drawer - android

I created a navigation bar in android and I am unable to select an item. I want to display a toast when a specific item is selected but it doesn't enter into the if condition of checking id of the item clicked.
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (abdt.onOptionsItemSelected(item)) {
if (item.getItemId() == R.id.addCat) {
Toast.makeText(
getApplicationContext(),
"Category Name Field is Empty!",
Toast.LENGTH_LONG
).show();
}
return true;
}
return super.onOptionsItemSelected(item);
}

In Navigation Drawer to implement item selection, you need to implement the interface of NavigationView.OnNavigationItemSelectedListener and
navigationView.setNavigationItemSelectedListener(this);
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.addCat:
Toast.makeText(getApplicationContext(), "Category Name Field is Empty!", Toast.LENGTH_LONG).show();
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return false;
}

Related

how to stop click events of checked item in navigation drawer activity using in multiple activities

here i used basic navigation view provided by android studio
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.getMenu().getItem(1).setChecked(true);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
/* // Handle navigation view item clicks here.
int id = item.getItemId();*/
//Check and un-check menu item if they are checkable behaviour
switch (item.getItemId()) {
case R.id.nav_camera: {
Intent intent = new Intent(MainActivity.this, TabbedActivity.class);
startActivity(intent);
break;
}
case R.id.nav_gallery: {
Toast.makeText(getApplicationContext(), "youclicked", Toast.LENGTH_SHORT).show();
break;
}
/* 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;
}
});
even though i'm checking the second item it was responding to clicks how to stop that
please help me i'm facing this problem since yesterday
You can do this in onPrepareOptionsMenu(Menu menu)
#Override
public boolean onPrepareOptionsMenu (Menu menu){
super.onPrepareOptionsMenu(menu);
MenuItem myItem = menu.findItem(R.id.myId); //here your menu ids
myItem.setEnabled(false);
return true;
}
Prepare the Screen's standard options menu to be displayed. This is
called right before the menu is shown, every time it is shown. You can
use this method to efficiently enable/disable items or otherwise
dynamically modify the contents.
Comment these id's which one you don't want to click like this :-
case R.id.nav_camera: {
Intent intent = new Intent(MainActivity.this, TabbedActivity.class);
startActivity(intent);
break;
}
case R.id.nav_gallery: {
Toast.makeText(getApplicationContext(), "youclicked", Toast.LENGTH_SHORT).show();
break;
}
This way you can check the last item and the current item is the same or not
private int prevId = -1;
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (prevId == -1 || prevId != id) {
// Do event handling on each item.
}
// ...
}

Changing options menu icon

Lets say I have a options menu like below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_home_page, menu);
final MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) myActionMenuItem.getActionView();
mSearchView.setQueryHint("Enter text to search(min. 3 chars)...");
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(final String searchQuery) {
if (!mSearchView.isIconified()) {
mSearchView.setIconified(true);
}
mSearchView.setQuery(searchQuery, false);
return false;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
});
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
saveState();
finish();
} else if (item.getItemId() == R.id.action_bookmark) {
FragmentAdapter adapter = (FragmentAdapter) mViewPager.getAdapter();
PageFragment fragment = (PageFragment)adapter.instantiateItem(mViewPager,mViewPager.getCurrentItem());
fragment.getContent();
Toast.makeText(this, "Bookmark Added", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
Is there any way to check which icon set to options menu item also change it with another when clicked on it?
here is xml for options menu:
<item
android:id="#+id/action_bookmark"
android:title="Bookmark"
app:showAsAction="always|collapseActionView"
android:icon="#drawable/ic_bookmark_empty" />
onclicking i want to check which icon bookmark has and change it.
if you want to check which drawable is currently set you can use this code
item.getIcon().getConstantState().equals
(getResources().getDrawable(R.drawable.ic_bookmark_empty).getConstantState())
and for setting new drawable you can use this code
item.setIcon(R.drawble.ic_bookmark_empty)
You need to check the following condition in onOptionsItemSelected :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
saveState();
finish();
} else if (item.getItemId() == R.id.action_bookmark) {
if(item.getIcon() ==R.drawable.ic_bookmark_empty)
{
// call bookmark code
item.setIcon(R.drawable.ic_bookmark_filled); // set your desired bookmark icon
} else {
// call remove bookmark code
item.setIcon(R.drawable.ic_bookmark_empty);
}
FragmentAdapter adapter = (FragmentAdapter) mViewPager.getAdapter();
PageFragment fragment = (PageFragment)adapter.instantiateItem(mViewPager,mViewPager.getCurrentItem());
fragment.getContent();
Toast.makeText(this, "Bookmark Added", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
You can use this code menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_bookmark_empty));

Show Popup Menu on menu key event

I have problem in creating pop-up menu on menu key event. I don’t understand how to pass required parameter to popmenu constructor. If any know what is the problem in my code then please suggest.
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU)
{
View v=getCurrentFocus();
PopupMenu popupMenu = new PopupMenu(this,v);
popupMenu.inflate(R.menu.poupup_menu);
popupMenu.setOnMenuItemClickListener(
new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_red:
Toast.makeText(context,"red",2000).show();
break;
case R.id.menu_blue:
Toast.makeText(context,"red",2000).show();
break;
case R.id.menu_green:
Toast.makeText(context,"red",2000).show();
break;
}
return true;
}
});
popupMenu.show();
// ...
return true;
} else {
return super.onKeyUp(keyCode, event);
}
}
}
thanks guys for answering...I got perferct solution to handle android menus..Override onCreateoption menu method,in that get menu instance and inflate our customize menu..and handle menu click event.
/* Initiating Menu XML file (menu.xml) */
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_save:
Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_search:
Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_share:
Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_delete:
Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_preferences:
Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

How can I change some text in an ActionBar item after an onOptionItemSelected event?

I'm using ActionBar in my application, and I want when the user clicks a button, the item in the ActionBar should change the text.
This is my code for onOptionsItemSelected():
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT)
.show();
finish();
break;
case R.id.lg:
Toast.makeText(getBaseContext(), "ma", Toast.LENGTH_SHORT).show();
break;
case R.id.French:
Toast.makeText(getBaseContext(), "zaki", Toast.LENGTH_SHORT).show();
break;
case R.id.nerlandais:
Toast.makeText(getBaseContext(), "brahim", Toast.LENGTH_SHORT)
.show();
}
return true;
}
What must I do to change the item title from another item?
Example: When I click in French item I want to change nerlandais item title.
If you want to change an MenuItem's title clicking to another item you can do something like this :
private String mMenuItemTitle;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.nerlandais);
item.setText(mMenuItemTitle);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.French:
Toast.makeText(getBaseContext(), "zaki", Toast.LENGTH_SHORT).show();
mMenuItemTitle = "My New Title";
supportInvalidateOptionsMenu();
break;
}
return true;
}
Try item.setTitle("new title")
Or, if from another item then the Menu has findItem

Activate CAB menu when OnClickEvent happens in Android

Trying to activate CAB menu when clicking on MenuItem from ActionBar. Here is how I set the GridView for listening to Multi Choice. The multiModeChoiceListener is working fine when I long press on Any item in the GridView. It is working fine. Now I have a requirement to activate the CAB menu when do press on a menu item in Action Bar. Once it is pressed, the CAB menu should read that 0 items are selected. After that it should allow me to select items from GridView on single clicks. How can I achieve this feature?
GridView set listener:
gv.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
gv.setMultiChoiceModeListener(new MultiChoiceModeListener());
MultiChoiceModeListener.java
public class MultiChoiceModeListener implements
GridView.MultiChoiceModeListener {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.featured_multiselect, menu);
MenuItem mi = menu.findItem(R.id.close);
mi.setIcon(R.drawable.cancel);
mode.setTitle("Select Items");
return true;
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Toast.makeText(getApplicationContext(), item.getTitle(),
Toast.LENGTH_SHORT).show();
if (item.getTitle().toString().equalsIgnoreCase("Close")) {
mode.finish();
}
return true;
}
public void onDestroyActionMode(ActionMode mode) {
new ChangeNotifier().changeOnFavoriteStore = true;
new AddFavorites().execute("add", device_id, dataArray);
if (notify == true) {
Toast.makeText(getApplicationContext(),
"Selected items are added to Favorites",
Toast.LENGTH_SHORT).show();
notify = false;
}
}
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
int selectCount = gridView.getCheckedItemCount();
if (selectCount > 0) {
notify = true;
dataArray.add(position);
switch (selectCount) {
case 1:
mode.setSubtitle("One item added to favorites");
break;
default:
mode.setSubtitle("" + selectCount
+ " items added to favorites");
break;
}
}
}
OnMenuItemClick method:
public boolean onPrepareOptionsMenu(final Menu menu) {
final MenuItem editItem = menu.findItem(R.id.editit);
editItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
//the CAB menu should be activated here. So that it reads that 0 items are selected in ActionBar
return false;
}
});
From your question I understand that you're trying to start the GridView associated CAB from clicking one of the menu items. I don't know if you can do this(but I may be mistaken) as the MultiChoiceModeListener expects an item to be checked to start. Depending on your layout and the overall appearance of the GridView, I think you could have a dummy item(as an extra item in the adapter) at the end of the GridView(with no content showing) and use setItemChecked(dummyItemPosition, true) to start the GridView CAB. Of course you'll need to have additional logic to take care of that extra item in your MultiChoiceModeListener:
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
if (position == theDummyPosition)
return; // so we start the CAB but there aren't any items checked
}
int selectCount = gridView.getCheckedItemCount();
if (selectCount > 0) {
notify = true;
dataArray.add(position);
// if you select another item you'll have two selected items(because of the dummy item) so you need to take care of it
switch (selectCount) {
case 1:
mode.setSubtitle("One item added to favorites");
break;
default:
mode.setSubtitle("" + selectCount
+ " items added to favorites");
break;
}
}
}
The solution above is a hack, most likely it would be much easier to lose the MultiChoiceModeListener and simply start an ActionMode that you can manipulate for both situations.

Categories

Resources