I have an activity with navigation drawer. The activity contain a view pager that hold 3 fragment. I've inflate the menu successfully. But, instead of showing the icon in action bar, it's hidden in three dots.
I've menu code below :
home_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/search_menu"
android:icon="#drawable/baseline_search_24"
android:title="Search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView">
</item>
</menu>
onCreateOptionsMenu() code :
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val inflater = MenuInflater(this)
inflater.inflate(R.menu.home_menu, menu)
val searchItem: MenuItem? = menu!!.findItem(R.id.search_menu)
searchItem?.let {
it.actionView?.let {
val searchView: SearchView = it as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(p0: String?): Boolean {
return false
}
override fun onQueryTextChange(p0: String?): Boolean {
return false
}
})
}
}
return true
}
What's wrong ?
Is another menu file will affected to that menu ?
Here's another menu files :
activity_home_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<item
android:id="#+id/admin_menu"
android:visible="true"
android:title="Admin Menu">
<menu>
<item
android:id="#+id/manage_user"
android:icon="#drawable/ic_group_gray_24dp"
android:title="Manage User" />
<item
android:id="#+id/manage_schedule"
android:icon="#drawable/baseline_calendar_today_24"
android:title="Manage Schedule" />
</menu>
</item>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_edit_profile"
android:icon="#drawable/baseline_edit_24"
android:title="Edit Profile" />
<item
android:id="#+id/nav_logout"
android:icon="#drawable/baseline_exit_to_app_24"
android:title="Logout" />
</group>
</menu>
bottom_navigation_drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/home_menu"
android:icon="#drawable/baseline_home_24"
android:title="Home"/>
<item
android:id="#+id/search_user_menu"
android:icon="#drawable/baseline_search_24"
android:title="Search User"/>
<item
android:id="#+id/schedule_request_menu"
android:icon="#drawable/baseline_inbox_24"
android:title="Request"/>
</menu>
If you want to show a menu icon then try this.
you should add showAsAction attribute to always or ifRoom
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_refresh"
android:showAsAction="ifRoom"
android:title="#string/action_refresh"/>
<item
android:id="#+id/action_filter"
android:icon="#drawable/ic_filter"
android:showAsAction="always"
android:title="#string/action_filter"/>
</menu>
Related
I have created a menu icon on action bar. When it is clicked, I expect it will show something like this
But it display on the left hand side instead.
What could be this issue ?
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
inflater!!.inflate(R.menu.fragment_menu, menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.getItemId()
if (id == R.id.more) {
var popup: PopupMenu? = PopupMenu(context!!, view!!)
popup?.menuInflater?.inflate(R.menu.pop_up_menu, popup.menu)
popup?.show()
popup?.setOnMenuItemClickListener({ item: MenuItem? ->
when (item!!.itemId) {
R.id.logOut -> {
}
}
true
})
} else
super.onOptionsItemSelected(item)
return true
}
fragment_menu
<?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/more"
android:title="menu"
app:showAsAction="always"
android:icon="#drawable/ic_more"/>
</menu>
pop_up_menu
<?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/logOut"
app:showAsAction="always"
android:title="Log Out"
/>
</menu>
The view I passing is this
#Nullable
public View getView() {
return this.mView;
}
You are passing the wrong view as what #CodeRed mentioned.
Replace
var popup: PopupMenu? = PopupMenu(context!!, view!!)
to
val vItem = getActivity().findViewById<View>(R.id.more)
var popup: PopupMenu? = PopupMenu(context!!, vItem)
Menu in ActionBar is populated automatically by system, you don't need to write code to populate it.
To achieve the menu like figure 1, fragment_menu should be like:
<menu>
<item android:title="Search" />
<item android:title="Locate" />
<item android:title="Refresh" />
<item android:title="Help" />
<item android:title="Check for update" />
</menu>
onOptionItemSelected is used to handle when above menu is clicked, not used to show the menu.
popup menu is free position menu like right click menus in windows, not the one you expect in figure 1.
I implemented the Navigation View (Support Library 24.0.0):
But a strange behavior occurred in which i have been trying to control since.
Here is my drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Categories">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/navigation_item_1"
android:icon="#drawable/ic_news"
android:title="Top Stories" />
<item
android:id="#+id/navigation_item_2"
android:icon="#drawable/ic_music"
android:title="Entertainment" />
<item
android:id="#+id/navigation_item_3"
android:icon="#drawable/ic_sport"
android:title="Sport" />
<item
android:id="#+id/navigation_item_4"
android:icon="#drawable/ic_lifestyle"
android:title="Lifestyle" />
<item
android:id="#+id/navigation_item_5"
android:icon="#drawable/ic_tech"
android:title="Technology" />
<item
android:id="#+id/navigation_item_6"
android:icon="#drawable/ic_world"
android:title="International" />
</group>
</menu>
</item>
</menu>
But the result is :
in which only one should be selected.
I solved it using :
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
for (int i = 0; i < 6; i++) {
MenuItem top = navigationView.getMenu().getItem(0).getSubMenu().getItem(i);
if (top.isChecked()) top.setChecked(false);
}
return true;
}
});
Unchecking all items in the menu
return true, that checks the clicked item.
I want to be able to show the free-dot menu always without using ActionBar. Just 3 dots menu + items in it, that's it.
How can I do that? Here's my code
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MyMainActivity">
<item
android:id="#+id/empty"
android:showAsAction="always"
android:title="#string/dot_menu"
android:icon="#android:drawable/ic_menu_more">
<menu>
<item
android:id="#+id/item1"
android:showAsAction="ifRoom"
android:title="#string/item1" />
<item
android:id="#+id/item2"
android:showAsAction="ifRoom"
android:title="#string/item2" />
<!-- ........... -->
And:
#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();
//.....
}
It doesn't show a menu at all.
Try this code, I hope it's helpful.
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/item_admin"
android:showAsAction="ifRoom|withText"
android:title="#string/item_admin"
android:visible="false"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/item_masterpass_preferences"
android:showAsAction="ifRoom|withText"
android:title="#string/text_1"
android:visible="true"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/item_shipping_addresses"
android:showAsAction="ifRoom|withText"
android:title="#string/text_2"
android:visible="true"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/item_switch_language"
android:showAsAction="ifRoom|withText"
android:title="#string/text_3"
android:visible="false"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/item_support_contact"
android:showAsAction="ifRoom|withText"
android:title="#string/text_4"
android:visible="true"
tools:ignore="AppCompatResource" />
I want to show icon in Option Menus. I tried many methods but I can't find success.In my toolbar some icon already put but i want icon in my Option menu.
I want same as below image:-
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=".MainActivity">
<item
android:id="#+id/action_search"
android:title="#string/action_search"
android:orderInCategory="1"
android:icon="#drawable/search"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_cart"
android:title="#string/action_search"
android:orderInCategory="2"
android:icon="#drawable/shoppingcart"
android:actionLayout="#layout/feed_update_count"
app:showAsAction="ifRoom" />
<item
android:id="#+id/menu_overflow"
android:title="#string/my_cart"
android:icon="#drawable/ic"
android:orderInCategory="11111"
app:showAsAction="always">
<menu>
<item
android:id="#+id/login"
android:title="#string/login"
android:orderInCategory="1"
android:icon="#drawable/account"
app:showAsAction="always|withText"/>
<item
android:id="#+id/my_order"
android:title="#string/my_order"
android:orderInCategory="2"
android:icon="#drawable/track"
app:showAsAction="withText"/>
<item
android:id="#+id/wishlist"
android:title="#string/wishlist"
android:orderInCategory="3"
android:icon="#drawable/wishlist"
app:showAsAction="withText"/>
</menu>
</item>
</menu>
I am new in android developing anyone can help me how to show icon in option menu. Thanks in advance!
By default the overflow menu will disable the icon and only show text, you need some workaround.
1st way
You can enable it by adding the following codes into your class
#Override
public boolean onMenuOpened(int featureId, Menu menu)
{
if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
try{
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
}
catch(NoSuchMethodException e){
Log.e(TAG, "onMenuOpened", e);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}
2nd way
Create your own overflow item and a nested menu to show both the icon and the text.
Example,
<item
android:id="#+id/overflow"
android:icon="#drawable/ic_overflow_white"
android:orderInCategory="201"
android:title="#string/overflow"
app:showAsAction="always">
<menu>
<item
android:id="#+id/cab_menu_select_all"
android:icon="#drawable/ic_select_all_grey"
android:orderInCategory="100"
android:title="#string/cab_menu_select_all"
app:showAsAction="always|withText"></item>
</menu>
</item>
try this code its works for you
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="always"
android:title="#string/action_search"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_location_found"
android:icon="#drawable/ic_action_location_found"
android:showAsAction="always"
android:title="#string/action_location_found"/>
<item
android:id="#+id/menu_overflow"
android:icon="#drawable/ic_action_menu"
android:orderInCategory="11111"
android:showAsAction="always">
<menu>
<!-- Refresh -->
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="ifRoom"
android:title="#string/action_refresh"/>
<!-- Help -->
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:showAsAction="never"
android:title="#string/action_help"/>
<!-- Check updates -->
<item
android:id="#+id/action_check_updates"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="never"
android:title="#string/action_check_updates"/>
</menu>
</item>
</menu>
- See more at: http://www.theappguruz.com/blog/android-working-android-actionbar#sthash.ArNQTEBF.dpuf
Create your menu xml as follow
<item
android:id="#+id/menu_item_action_parameters"
android:title="#string/text_parameters"
android:icon="#drawable/ic_menu_parameter"
app:showAsAction="ifRoom|withText"/> >
<menu>
<item
android:id="#+id/action_dropdown1"
android:title="#string/dropdown_1" />
<item
android:id="#+id/action_dropdown2"
android:title="#string/dropdown2" />
<item
android:id="#+id/action_dropdown3"
android:title="#string/dropdown3" />
</menu>
</item>
<item
more item
</item>
Then
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_dropdown1:
.....
return true;
case R.id.action_dropdown2:
.....
return true;
.....
default:
return super.onOptionsItemSelected(item);
}
}
this is completely working code.Mark this answer up if it helps. :) Happy coding.
you Need to add tools:context="your class" to menu tag
<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=".activities.BaseActivity">
<item
android:id="#+id/action_notification1"
android:icon="#drawable/one"
android:title="action_notification"
app:showAsAction="always"/>
<item
android:id="#+id/action_notification2"
android:icon="#drawable/two"
android:title="action_notification"
app:showAsAction="always"/>
<item
android:id="#+id/action_notification3"
android:icon="#drawable/three"
android:title="action_notification"
app:showAsAction="always"/>
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
you shold write tool:context to menu tag then run you will get icons to your text
I have two menu items in menu/contacts_menu.xml as :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="#+id/pm_action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="ifRoom|collapseActionView"
android:title="#string/action_search"/>
<item
android:id="#+id/show_online"
android:icon="#drawable/online_icon"
android:orderInCategory="2"
android:showAsAction="ifRoom"
android:title="Show Online"/>
</menu>
Where , in my Fragment :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.contacts_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.show_online) {
// DO SOMETHING
return true;
}
return super.onOptionsItemSelected(item);
}
I have done the same procedure in my others apps, and action items are showing. But in my current app, they are not showing. only if i press the menu button, action items are showing only with text. i want to show the icons on my action bar.
I guess you are using appcompat library? Try this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/pm_action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="always|collapseActionView"
appcompat:showAsAction="always|collapseActionView"
android:title="#string/action_search" />
<item
android:id="#+id/show_online"
android:icon="#drawable/online_icon"
android:orderInCategory="2"
android:showAsAction="always"
appcompat:showAsAction="always"
android:title="Show Online" />
</menu>
This should definitely work, but for simplicity you can also try this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/pm_action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:orderInCategory="1"
appcompat:showAsAction="always|collapseActionView"
android:title="#string/action_search" />
<item
android:id="#+id/show_online"
android:icon="#drawable/online_icon"
android:orderInCategory="2"
appcompat:showAsAction="always"
android:title="Show Online" />
</menu>