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.
Related
I cant click on item inside my menu. My menu opens and everything is displayed normally, but nothing happens when I click. I don't know why and searched for information but it doesn't help me.
This my XML code profile__detail_menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="always"
android:icon="#drawable/ic_baseline_more_vert_24"
android:title="">
<menu>
<item
android:id="#+id/change_name"
android:title="Change name"
android:icon="#drawable/ic_baseline_edit_24"
/>
<item
android:id="#+id/new_photo"
android:title="New photo"
android:icon="#drawable/ic_baseline_add_a_photo_24"
/>
<item
android:id="#+id/log_out"
android:title="Log out"
android:icon="#drawable/ic_baseline_login_purp_24"
/>
</menu>
</item>
</menu>
This my Java code.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.profile__detail_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
Toast.makeText(Profile.this,"Something", Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.change_name:
Toast.makeText(this, "change_name selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.new_photo:
Toast.makeText(this, "new_photo selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.log_out:
Toast.makeText(this, "log_out selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The thing which I feel is wrong is your menu XML file. For some reason you have a menu tag inside a menu tag. So there is sort of a sub menu if you get my point? But you're not inflating that actually.
Modify the XML as follows
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".YourActivityName"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/change_name"
android:title="Change name"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_edit_24"
/>
<item
android:id="#+id/new_photo"
android:title="New photo"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_add_a_photo_24"
/>
<item
android:id="#+id/log_out"
android:title="Log out"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_login_purp_24"
/>
</menu>
I have updated your menu as per you code. If you need, add more items to it.
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.
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"
I have this menu filters.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single" android:id="#+id/menuGroup_1" >
<item android:id="#+id/a_1" android:title="a1" />
<item android:id="#+id/a_2" android:title="a2" />
</group>
<group android:checkableBehavior="single" android:id="#+id/menuGroup_2" >
<item android:id="#+id/b_1" android:title="b1" />
<item android:id="#+id/b_2" android:title="b2" />
</group>
</menu>
and here are the methods in the .java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.filters, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
item.setChecked(!item.isChecked());
return true;
}
This works fine checkableBehavior="single", but the item doesn't uncheck when I press. Still checked. (item.isChecked()->true)
I try to edit the .xml:
<item android:id="#+id/..." android:checkable = "true" />
Now when a select any item they check/uncheck correcltry,
but this checkableBehavior="single" doesn't work. In other words, I can check two items of the same group.
you should let onOptionsItemSelected return true;
I tried to add two action buttons on the action bar, for visibility one is defined as:
android:showAsAction="ifRoom"
another one is defined as:
android:showAsAction="never"
The problem is I can see the ic_action_search icon but I could not see the ic_action_overflow icon. This is the main_activity_actions.xml in menu folder:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MyFirstApp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
MyFirstApp:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:icon="#drawable/ic_action_overflow"
android:title="#string/action_settings"
MyFirstApp:showAsAction="never" />
</menu>
and this is the Java code that includes the buttons:
#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_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
//return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
// openSearch();
return true;
case R.id.action_settings:
// openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
So whats wrong with my code?
cheers
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
it is work