Android onMenuItemClick receiving wrong item - java

I have this menu that pops up fine when I create it.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/day"
android:title="Day" />
<item
android:id="#+id/week"
android:title="Week" />
<item
android:id="#+id/month"
android:title="Month" />
<item
android:id="#+id/year"
android:title="Year" />
</menu>
For some reason, the onMenuItemClick is only giving me "Year" no matter which item I click on. This is a really strange problem and I have no idea why it's happening.
public void showMenu(View v) {
PopupMenu popup = new PopupMenu(getContext(), v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.date_range_menu, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.day:
((MainActivity) requireActivity()).toast("Day");
changeDateRange(DateRange.DAY);
case R.id.week:
((MainActivity) requireActivity()).toast("Week");
changeDateRange(DateRange.WEEK);
case R.id.month:
((MainActivity) requireActivity()).toast("Month");
changeDateRange(DateRange.MONTH);
case R.id.year:
((MainActivity) requireActivity()).toast("Year");
changeDateRange(DateRange.YEAR);
default:
return false;
}
}
});
}

You need to add the break; keyword to each case.

Related

Show popup menu from an action bar item

So, I am trying to add a PopupMenu to a ActionBar item that sorts the list. I have seen some answers in stackoverflow but none of them worked for me. Below is the code.
XML
Menu Item:
<item
android:id="#+id/sortByBackers"
android:icon="#drawable/ic_sort"
android:title="Sort By Backers"
app:showAsAction="always" />
PopupMenu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/default_list"
android:title="Default" />
<item
android:id="#+id/sort"
android:title="Sort" />
</menu>
Activity
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.bar_menu, menu);
//...
//...
MenuItem sortItem = menu.findItem(R.id.sortByBackers);
sortItem.setOnMenuItemClickListener(item -> {
View view = findViewById(R.id.default_list);
popupMenu = new PopupMenu(getApplicationContext(), view);
popupMenu.inflate(R.menu.sort_or_default);
popupMenu.setOnMenuItemClickListener(item1 -> {
switch (item1.getItemId()) {
case R.id.default_list:
kickstarterAdapter.setData(kickstartersList);
break;
case R.id.sort:
kickstarterAdapter.sortList();
break;
default:
kickstarterAdapter.sortList();
}
popupMenu.show();
return true;
});
return true;
});
`

Menu item TITLE VISIBILITY in bottom navigation view

by default, menu titles and icons display in bottom navigation view.
how can we hide/show titles/icons of menu items programmically or in XML?
note: hide/show one of these: 'titles' or 'icons' (not both)
menu :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/home"
android:icon="#drawable/home"
android:title="#string/home"/>
<item
android:id="#+id/about"
android:icon="#drawable/about" />
<item
android:id="#+id/services"
android:icon="#drawable/services" />
<item
android:id="#+id/portfolios"
android:icon="#drawable/portfolios" />
<item
android:id="#+id/contact"
android:icon="#drawable/contact" />
</menu>
method:
private void bnvHelper(){
BottomNavigationView bnv = (BottomNavigationView)findViewById(R.id.bottom_navigation_view);
bnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int[] titles = {
R.string.home,
R.string.about,
R.string.services,
R.string.portfolios,
R.string.contact};
BottomNavigationView bnv = (BottomNavigationView)findViewById(R.id.bottom_navigation_view);
Menu menu = bnv.getMenu();
final int previousItem = bnv.getSelectedItemId();
final int nextItem = item.getItemId();
if (previousItem != nextItem) {
switch (nextItem) {
case R.id.home:
menu.getItem(0).setTitle(titles[0]);
menu.getItem(1).setTitle(null);
menu.getItem(2).setTitle(null);
menu.getItem(3).setTitle(null);
menu.getItem(4).setTitle(null);
break;
case R.id.about:
menu.getItem(1).setTitle(titles[1]);
menu.getItem(0).setTitle(null);
menu.getItem(2).setTitle(null);
menu.getItem(3).setTitle(null);
menu.getItem(4).setTitle(null);
break;
case R.id.services:
menu.getItem(2).setTitle(titles[2]);
menu.getItem(0).setTitle(null);
menu.getItem(1).setTitle(null);
menu.getItem(3).setTitle(null);
menu.getItem(4).setTitle(null);
break;
case R.id.portfolios:
menu.getItem(3).setTitle(titles[3]);
menu.getItem(0).setTitle(null);
menu.getItem(1).setTitle(null);
menu.getItem(2).setTitle(null);
menu.getItem(4).setTitle(null);
break;
case R.id.contact:
menu.getItem(4).setTitle(titles[4]);
menu.getItem(0).setTitle(null);
menu.getItem(1).setTitle(null);
menu.getItem(2).setTitle(null);
menu.getItem(3).setTitle(null);
break;
}
} return true;
}
});
}

How do i use my items in my Menu?

I have create a menu in the action bar but I don't know how to use the items inside of it as a button.
That's my menu 'xml' code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:icon="#drawable/ic_add_circle_outline_black_24dp"
android:title=""
app:showAsAction="ifRoom">
<menu>
<item
android:id="#+id/deleteMenu"
android:title="Delete All" />
<item
android:id="#+id/exitMenu"
android:title="Exit" />
</menu>
</item>
<item
android:id="#+id/addMovieOffline"
android:title="Offline Mode" />
<item
android:id="#+id/addMovieOnline"
android:title="Online Mode" />
</menu>
That's what i have in java:
public class MyMainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_main);
}
// "Creating" my menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
}
How I use the items I just declare?
For example the "Exit" option
I think it is the most basic thing although I don't know how to reach the item as a button.. Or its already define it self as a button ?
I would like to get an example and explanation.
you are using menu item inside the item, which is not the correct way to group item in menu.
Try this xml to generate your menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="#+id/my_menu" android:checkableBehavior="single">
<item
android:id="#+id/deleteMenu"
android:title="Delete All" />
<item
android:id="#+id/exitMenu"
android:title="Exit" />
</group >
<item
android:id="#+id/addMovieOffline"
android:title="Offline Mode" />
<item
android:id="#+id/addMovieOnline"
android:title="Online Mode" />
</menu>
For receiving events of the click on menu:
#override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
// action needed
return true;
case R.id.item2:
// action needed
return true;
}
}
Use this
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.exitMenu:
//Your Logic
return true;
}
}
Use onOptionsItemSelected Method.
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.exitMenu:
//code here
return true;
}
return(super.onOptionsItemSelected(item));
}
Use switch case to identify the ids of menu items.

Items in menu not showing action bar, rather being displayed without any icons in three dots

Previously my app name was being displayed hence I had to use getSupportActionBar().setDisplayShowTitleEnabled(false) to remove the text, but now in its place a blank action is displayed with three dots on the side showing the list items that I have defined.
I want Refresh and Back actions to be displayed directly into the action bar with their respective icons
eventdetails.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_refresh"
app:showAsAction="always"
android:title="Refresh"
android:icon="#drawable/ic_action_refresh"
/>
<item
android:id="#+id/action_settings"
android:title="Settings"
app:showAsAction="ifRoom"
>
</item>
<item
android:id="#+id/action_back"
android:title="Back"
android:icon="#drawable/ic_action_back"
app:showAsAction="always"
/>
</menu>
EventDetails.java
public class EventDetails extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarEventDetails);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.eventdetails, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_back:
Intent action_back = new Intent(EventDetails.this, EventView.class);
startActivity(action_back);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Your xmlns:app is not setup properly
change:
xmlns:app="schemas.android.com/apk/res-auto"
to
xmlns:app="http://schemas.android.com/apk/res-auto"

Android Design Navigation Drawer - How to add a switch in nav xml?

I am using the new Android Design Navigation Drawer. I want to add a switch in the drawer. Is there a away to implement this?
this is the menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/nav_view_menu_group_1"
android:checkableBehavior="single">
<item
android:id="#+id/nav_view_menu_item_myschedule"
android:icon="#drawable/ic_navview_my_schedule"
android:title="#string/navview_menu_item_myschedule"
android:titleCondensed="#string/navview_menu_item_myschedule" />
<item
android:id="#+id/nav_view_menu_item_iolive"
android:icon="#drawable/ic_navview_play_circle_fill"
android:title="#string/navview_menu_item_iolive"
android:titleCondensed="#string/navview_menu_item_iolive"
android:visible="false"/>
<item
android:id="#+id/nav_view_menu_item_explore"
android:icon="#drawable/ic_navview_explore"
android:title="#string/navview_menu_item_explore"
android:titleCondensed="#string/navview_menu_item_explore" />
<item
android:id="#+id/nav_view_menu_item_map"
android:icon="#drawable/ic_navview_map"
android:title="#string/navview_menu_item_map"
android:titleCondensed="#string/navview_menu_item_map"
android:visible="false"/>
</group>
</menu>
How can I change one <item> to be switch:
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Switch"
android:id="#+id/switch1"
android:layout_gravity="center_horizontal" />
I am currently using the default layout:
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/activity_main_drawer" />
Just Like this Image under Android Notification http://i.stack.imgur.com/M9nD7.png
I really Appreciate any feedback. Thank you.
One way I have found of doing this would be to use setActionView on the menuItem you want:
mNavigationView.getMenu().findItem(R.id.nav_connect)
.setActionView(new Switch(this));
// To set whether switch is on/off use:
((Switch) mNavigationView.getMenu().findItem(R.id.nav_connect).getActionView()).setChecked(true);
Probably want a click listener as well to change the state of the Switch:
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_connect:
((Switch) menuItem.getActionView()).toggle();
return true;
}
}
}
I haven't tried, but you could probably use android:actionLayout="#layout/switch_layout" in xml and point to a custom layout you created.
Also could try using an ActionProvider which might offer a little more robustness. I haven't tried this method either though.
We can do it by the following way
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
//Get a reference to your item by id
MenuItem item = menu.findItem(R.id.menu_pick_color);
//Here, you get access to the view of your item, in this case, the layout of the item has a FrameLayout as root view but you can change it to whatever you use
FrameLayout rootView = (FrameLayout)item.getActionView();
//Then you access to your control by finding it in the rootView
YourControlClass control = (YourControlClass) rootView.findViewById(R.id.control_id);
//And from here you can do whatever you want with your control
return true;
}
If you only want to check for the changes in the switch, you can set onCheckedChangeListener only for the switch like so
((Switch) navigationView.getMenu().findItem(R.id.darkModeSwitch).getActionView())
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Unchecked", Toast.LENGTH_SHORT).show();
}
}
});
If you want to be cool use lambda
((Switch) navigationView.getMenu().findItem(R.id.darkModeSwitch).getActionView())
.setOnCheckedChangeListener((buttonView, isChecked) -> {
if(isChecked) {
Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Unchecked", Toast.LENGTH_SHORT).show();
}
});

Categories