i am setting an actionLayout on a menu item and setting background color and image, but it's not respected. in my activity, i have:
getMenuInflater().inflate(R.menu.submit_action, menu);
my submit_action is:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_submit"
android:actionLayout="#layout/check"
app:showAsAction="always" />
</menu>
my check layout is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/actionButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#e8e8e8"
android:clickable="true"
android:contentDescription="lol" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#null"
android:scaleType="centerInside"
android:src="#drawable/ic_action_tick" />
</RelativeLayout>
but even with all of this setup, this is how the action bar appears, not showing my menuitem at all (but it's there, because it responds to the click, but doesn't appear):
Try app:actionLayout="#layout/check" instead of android:actionLayout="#layout/check".
If you're using ActionbarSherlock or AppCompat, the android: namespace will not work for MenuItems. This is because these libraries use custom attributes that mimic the Android APIs since they did not exist in earlier versions of the framework.
when using Appcompact ,menu item will be like
<item android:id="#+id/cart"
app:actionLayout="#layout/actionbar_cart"
android:title="#string/action_cart"
app:showAsAction="always"
/>
The answer from Ben Harris is absolutely correct. However, in some case like when using attributes like:
app:showAsAction="ifRoom|collapseActionView"
used in SearchView (in my case), the layout view isn't shown and that caused a lot of headache to me. It seems that collapseActionView is not supported with action view in appcombat. So consider this as well while doing your stuffs.
use app namespace instead of android and it will work fine.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_submit"
app:actionLayout="#layout/check"
app:showAsAction="always" />
</menu>
Related
I'm assembling an app with a Botton navigation bar in Android with Android Studio. With two icons it looked fine, but when I added four, the icons look cropped. I can't find any other error like this. Any suggestions are welcomed.
Cropped icons bottom nav 4 options
Here is the code for the Bottom Navigation (bottom_navigation.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">
<item
android:id="#+id/action_lines"
android:enabled="true"
android:icon="#drawable/ic_lines_off"
android:title="Lines"
android:background="#android:color/transparent"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_contacts"
android:enabled="true"
android:icon="#drawable/ic_contacts_off"
android:title="Contacts"
android:background="#android:color/transparent"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_conversations"
android:enabled="true"
android:icon="#drawable/ic_conversations_off"
android:title="Conversations"
android:background="#android:color/transparent"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_settings"
android:enabled="true"
android:icon="#drawable/ic_settings_off"
android:title="Settings"
android:background="#android:color/transparent"
app:showAsAction="ifRoom" />
</menu>
In my activity.xml I call the bottom menu like this:
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:itemIconTint="#drawable/bnv_tab_item_foreground"
app:itemTextColor="#drawable/bnv_tab_item_foreground"
app:menu="#menu/bottom_navigation" />
Question Update:
I tried changing
app:showAsAction="ifRoom"
for
app:showAsAction="always"
And it doesn't solve the problem. There is no change to the issue. And it does not show the tag in every icon as it should. Only on the selected one.
One way of solving it is adding a Theme to the bottom navigation call in activity, like this:
android:theme="?attr/toolbarNavigationButtonStyle"
I have no clue why it works, but it solves the issue apparently in lots of devices exept in Samsung Galaxy Note 8
Here is "bnv_tab_item_foreground"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#color/colorAccent" />
<item android:color="#android:color/darker_gray" />
</selector>
Try changing a value for the property below:-
app:showAsAction="always"
and if still, your problem persists, please provide the file
"bnv_tab_item_foreground"
Use Support Library 28. Then, just add app:labelVisibilityMode="labeled" to your BottomNavigationView XML declaration.
Hope this helps
I believe this is being caused by your layout not fitting into the System window, and you can easily fix this by going to the Layout you have the Bottom Navigation and at the root layout add android:fitsSystemWindows="true" just like I did below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!--things come here -->
</RelativeLayout>
i am setting an actionLayout on a menu item and setting background color and image, but it's not respected. in my activity, i have:
getMenuInflater().inflate(R.menu.submit_action, menu);
my submit_action is:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_submit"
android:actionLayout="#layout/check"
app:showAsAction="always" />
</menu>
my check layout is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/actionButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#e8e8e8"
android:clickable="true"
android:contentDescription="lol" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#null"
android:scaleType="centerInside"
android:src="#drawable/ic_action_tick" />
</RelativeLayout>
but even with all of this setup, this is how the action bar appears, not showing my menuitem at all (but it's there, because it responds to the click, but doesn't appear):
Try app:actionLayout="#layout/check" instead of android:actionLayout="#layout/check".
If you're using ActionbarSherlock or AppCompat, the android: namespace will not work for MenuItems. This is because these libraries use custom attributes that mimic the Android APIs since they did not exist in earlier versions of the framework.
when using Appcompact ,menu item will be like
<item android:id="#+id/cart"
app:actionLayout="#layout/actionbar_cart"
android:title="#string/action_cart"
app:showAsAction="always"
/>
The answer from Ben Harris is absolutely correct. However, in some case like when using attributes like:
app:showAsAction="ifRoom|collapseActionView"
used in SearchView (in my case), the layout view isn't shown and that caused a lot of headache to me. It seems that collapseActionView is not supported with action view in appcombat. So consider this as well while doing your stuffs.
use app namespace instead of android and it will work fine.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_submit"
app:actionLayout="#layout/check"
app:showAsAction="always" />
</menu>
hi there I try set ifroom in my menu item. you can see in this code I try all possible senario (please see each showAsAction item
<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_forward"
android:title="forward"
android:icon="#drawable/ic_action_arrow_forward"
android:showAsAction="ifRoom"
></item>
<item
android:id="#+id/action_zoom_in"
android:title="zoom in"
android:icon="#drawable/ic_action_zoom_in"
android:showAsAction="ifRoom|withText"
></item>
<item
android:id="#+id/action_home"
android:title="home"
android:icon="#drawable/ic_action_home"
app:showAsAction="ifRoom|withText"
></item>
<item
android:id="#+id/action_refresh"
android:title="refresh"
android:icon="#drawable/ic_action_refresh"
app:showAsAction="ifRoom"
></item>
<item
android:id="#+id/action_zoom_out"
android:title="zoom out"
android:icon="#drawable/ic_action_zoom_out"
showAsAction="ifRoom"
></item>
<item
android:id="#+id/action_back"
android:title="back"
android:icon="#drawable/ic_action_arrow_back"
app:showAsAction="always"
></item>
</menu>
but it just show action_back and action_home in main page.
I am noob in android studio but I think this error related to width of menu.
(It is not 100% width)
Please see my main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="#+id/tool_bar_bottom"
layout="#layout/tool_bar_bottom"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="center" />
</LinearLayout>
and
tool_bar_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#color/ColorPrimary"
android:elevation="2dp"
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark"
xmlns:android="http://schemas.android.com/apk/res/android" />
I think the structure is true and values of all layout_width is match_parent so why ifroom do not work for me?
Android will show menu always if you set "showAsAction" attribute as "always". If you set "ifRoom", that will be displayed only if the actionbar has room to display you menu item, else it will display a overflow icon.
In your case, you are seeing "action_back" because it has "always". then "action_home" becuase there is room for one icon and that is the first item which has "app:showAsAction" attribute. others have "android:showAsAction" attribute. More explanation on this behaviour is below.
If your app is using the Support Library for compatibility on versions
as low as Android 2.1, the showAsAction attribute is not available
from the android: namespace. Instead this attribute is provided by the
Support Library and you must define your own XML namespace and use
that namespace as the attribute prefix. (A custom XML namespace should
be based on your app name, but it can be any name you want and is only
accessible within the scope of the file in which you declare it.)
I've been experimenting with simplifying some navigation drawer code by making use of the new NavigationView class in the Android design support library. It works great if you just want icons on the left, and text on the right like in the example in the documentation, but what if I want to add a single custom view to the layout which has an android.support.v7.widget.SwitchCompat like in the Google Play Movies app (see screenshot below)?
I've tried using the actionLayout attribute to specify a custom layout file like in the example code below, however that attribute appeared to be ignored as it didn't work.
res/menu/navigation_drawer.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="#+id/group1"
android:checkableBehavior="single">
<item
android:id="#+id/nav_screen1"
android:icon="#drawable/ic_list_black_24dp"
android:title="Screen 1" />
<item
android:id="#+id/nav_screen2"
android:icon="#drawable/ic_search_black_24dp"
android:title="Screen2"/>
</group>
<group android:id="#+id/group2">
<item
android:id="#+id/nav_custom"
android:icon="#drawable/custom_icon_24dp"
app:actionLayout="#layout/nav_drawer_switch"
android:title="Custom item with switch"/>
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings_black_24dp"
android:title="Settings"/>
</group>
</menu>
res/layout/nav_drawer_switch.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.SwitchCompat
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="Custom item with switch"/>
</LinearLayout>
How can I get this to work? Ideally I'd like to add the custom view while still making use of my Menu layout, but if that's not possible without using a crude hack (like tapping into an existing layout generated by the support library) then I'd like to know the solution with the least amount of code, and which still makes it worth transitioning to NavigationView.
The actionLayout attribute is now supported in Android Support Library 23.1:
NavigationView provides a convenient way to build a navigation drawer, including the ability to creating menu items using a menu XML file. We’ve expanded the functionality possible with the ability to set custom views for items via app:actionLayout or using MenuItemCompat.setActionView().
So the code in the question should just work now.
NavigationView is a subclass of FrameLayout, which can have multiple children:
You can, however, add multiple children to a FrameLayout and control
their position within the FrameLayout by assigning gravity to each
child, using the android:layout_gravity attribute.
That means you can add a custom view to your NavigationView:
<android.support.design.widget.NavigationView
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/side_menu">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="bottom">
<android.support.v7.widget.SwitchCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="night_mode"/>
</LinearLayout>
</android.support.design.widget.NavigationView>
I had the same problem and found that NavigationView behaves differently depending on:
Whether you set the android:title and android:icon attributes on item or not
What kind of view your action layout contains (e.g. TextView or others)
Not setting the attributes worked for completely customizing the item:
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<item
android:id="#+id/nav_custom"
android:title="#null"
app:actionLayout="#layout/nav_drawer_switch" />
</menu>
(Note that you can also just not set android:title, but that results in a warning in Android Studio.)
The result:
(This is with version 27.1.1 of com.android.support:design, not sure about other versions.)
create a layout with switchcompat and mention it in menu item as
<item android:id="#+id/notification"
android:title="Notification"
app:actionLayout="#layout/notify" />`
then notify layout add this
<android.support.v7.widget.SwitchCompat
android:id="#+id/switch_compat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:checked="false"
android:onClick="notify"/>
onclick is the keyPoint,implement handler in your Activity,then it will work.
You might want to take a look at this library: https://github.com/mikepenz/MaterialDrawer. It makes it easy to use Navigation Drawer and it supports custom views.
I have a menu layout with only one item in it.
<?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/toolbar_right_button"
android:icon="#drawable/ic_toolbar_locked"
android:orderInCategory="1"
android:title="Logout"
app:showAsAction="always|withText"/>
</menu>
As you can see I've included the withText attribute but I still only get the icon.
How can I get both the icon and text to show?
I agree with your answer, but I had to do some digging to get it to work in my code, ultimately, I had to create an onClickListener for the button.
In my code, I wrote in the onCreateOptionsMenu(Menu menu) method:
menu.getItem(indexInMenu).getActionView().findViewById(R.id.button_logout).setOnClickListener(...);
This makes the menuItem responsive when it's replaced with a button in app:actionLayout
I'm not sure why withText wouldn't work for me so I just created an actionLayout to use instead.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/toolbar_right_button"
android:icon="#drawable/ic_toolbar_locked"
android:orderInCategory="1"
android:title="Logout"
app:actionLayout="#layout/menu_item_logout"
app:showAsAction="ifRoom|withText"/>
</menu>
and the actionLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/logout_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/ic_toolbar_locked"
android:gravity="center_vertical"
android:text="Logout"
android:textColor="#color/PrimaryColour"/>
</RelativeLayout>
I have try your menu xml, and it worked on android 4.1.1, 5.0.0
but it not worked on 4.3
to fix this, please refer to How to get text on an ActionBar Icon?
it use custom layout for the item
You could try:
<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" >
<!-- "Mark Favorite", should appear as action button if possible -->
<!-- Settings, should always be in the overflow -->
<item
android:title="Settings"
android:id="#+id/mainMenu"
android:icon="#drawable/ic_3d_rotation_black_24dp"
app:showAsAction="always">
<menu>
<item
android:id="#+id/menu_logout1"
android:icon="#drawable/ic_3d_rotation_black_24dp"
android:title="logout1"/>
<item
android:id="#+id/menu_logout3"
android:icon="#drawable/ic_3d_rotation_black_24dp"
android:title="logout3"/>
</menu>
</item>
<item
android:id="#+id/menu_logout2"
android:icon="#drawable/ic_3d_rotation_black_24dp"
android:title="logout2"
app:showAsAction="always"/>
</menu>
Note, that this example will also paint some icons, so if you don´t want the icons, just remove the icon attribute.
Remove icon from <item
android:icon="#drawable/ic_baseline_add_24"
Remove this line then it will show hope it worked for u
When the text in your toolbar menu items is not showing, this is probably related to your style settings. For example, when you have organized your style in a way that app title in toolbar is shown in white color, your menu items may become white on white.
To solve this, you need to set two attributes to your toolbar:
This is the general Style for the toolbar. Textcolor is white. Maybe you are using something like this when you are facing the "no text"-issue
<style name="ToolBarStyle" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar">
<item name="android:textColorPrimary">#android:color/white</item>
<item name="android:textColorSecondary">#android:color/white</item>
</style>
To make sure that text in menu is showing, not only set the app:theme of your toolbar but also the app:popuptheme. In this example popuptheme is set to a light theme because this leads to black text.
<androidx.appcompat.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#color/supplyon_blue"
app:theme="#style/ToolBarStyle"
app:popupTheme="#style/ThemeOverlay.MaterialComponents.Light"
android:id="#+id/toolbar" />
Play around with this setting and it will probably solve your problem.