I have looked for a solution to the problem on Stack Overflow and google, but nothing seems to help. To clarify, I am talking about the one with the id "favourite"
Here is my menu xml:
<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="com.rasmase.smbcbrowser.MainActivity" >
<item android:id="#+id/favourite"
android:icon="#drawable/ic_favourite"
android:title="#string/favourite"
app:showAsAction="ifRoom" />
<item
android:id="#+id/go_to_comic"
android:orderInCategory="100"
android:title="#string/Go_to_comic"
app:showAsAction="never"/>
And my onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
Anyone else have any idea what is wrong?
EDIT:
Turns out that I am retarded. The Icon was a black star on a black actionbar. Image for reference: http://i.stack.imgur.com/tVWm7.png
Related
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;
});
`
I am developing a navigation drawer activity in android studio, in menu folder there is a settings item at the left corner. I want to remove it and add an image button over there in action bar. Can anyone tell me how to add that image button and how to perform click event on that button in main activity.
Check below image for reference -
I hope this helps, add this inside your menu.xml layout -
The showAsAction attribute allows you to define how the action is displayed. For example, the ifRoom attribute defines that the action is only displayed in the action bar if there is sufficient screen space available.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/ic_settings"
android:title="Settings">
</item>
</menu>
just try this.
no need to add imagebutton you just need to add
android:showAsAction="always"
and icon as
android:icon="#drawable/Your_icon"
do this in the menu.xml file, in item setting
Navigate to main_menu.xml in your menu folder under res and modify it as
<?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">
<item android:id="#+id/image_button"
android:icon="#android:drawable/ic_menu_search"
android:title="Clickme"
app:showAsAction="always" />
</menu>
In your MainActivity
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId == R.id.image_button){
//do action on imagebutton click
return true;
}
return super.onOptionsItemSelected(item);
}
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 the standard code as it is for a menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
And what I want to do is have 2 options:
1 for settings
1 for about us
If you could please help me code this, I would appreciate it. Thanks!
You need to create activity_main.xml in your /res/menu dir. It would look something like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/settings"
android:title="Settings"
android:icon="#drawable/settings_icon" />
<item android:id="#+id/about"
android:title="About"
android:icon="#drawable/about_icon" />
</menu>
Note that you should also include icon resources settings_icon.png and about_icon.png in your /res/drawable dir.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.add_account, menu);
return true;
}
I try to disable a menu group with the following code, but it doesn't work, menu items are still enabled. Can you tell me what's wrong please?
res/menu/menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/a" android:title="A"></item>
<item android:id="#+id/b" android:title="B">
<menu>
<item android:id="#+id/c" android:title="C" />
<item android:id="#+id/d" android:title="D" />
<group android:id="#+id/group_1">
<item android:id="#+id/e" android:title="E" />
<item android:id="#+id/f" android:title="F" />
</group>
</menu>
</item>
</menu>
Java:
public boolean onPrepareOptionsMenu (Menu menu) {
menu.setGroupEnabled (R.id.group_1, false); // does not work
menu.setGroupVisible (R.id.group_1, false); // does not work either
return super.onPrepareOptionsMenu (menu);
}
public boolean onCreateOptionsMenu (Menu menu) {
getMenuInflater ().inflate (R.menu.menu, menu);
return true;
}
Thanks to user432209's info, here is the answer:
menu.findItem (R.id.b).getSubMenu ().setGroupVisible (R.id.group_1, false);
I'm not sure if you can use a group like this, but try this (its worth a shot):
MenuItem item = menu.findItem(R.id.group_1);
item.setVisible(true);
item.setEnabled(false);
Edit: Your problem is your menu structure and how you create the menu inside onCreateOptionsMenu due to using parent/child menus.
You create a menu for the parent menu, not the child menu, so that is why the call to setGroupEnabled and setGroupVisible fail. You need to create an object in onCreateOptionsMenu that represents the child in order for that object to be passed into onPrepareOptionsMenu and your code to work.
The above-mentioned solution works well but it should be written within onCreateOptionsMenu, here is the example of it:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu
inflater.inflate(R.menu.menu, menu);
// Make the menu item visible and enable it.
MenuItem item = menu.findItem(R.id.group_1);
item.setVisible(true);
item.setEnabled(false);
}