why `ifroom` does not work - android studio - android

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.)

Related

Image as background in menu item

As said in the question.
What I currently have:
Desired output:
On each column there would be a background image. I'm going to remove the text and just fill items with images. Is this possible? If yes, do I need different resolutions for background images?
Activity_main_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">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_ITEM1"
android:title="#string/ITEM1"
/>
<item
android:id="#+id/nav_ITEM2"
android:title="#string/ITEM2" />
<item
android:id="#+id/nav_ITEM3"
android:title="#string/ITEM3" />
</group>
</menu>
Depending on what exactly you are looking to do, you could try add an icon:
<item
android:id="#+id/nav_ITEM1"
android:icon="#drawable/item1_image"
android:title="#string/ITEM1" />
EDIT / UPDATE: to set the 'background image' of a menu entry, try:
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemBackground="#drawable/YOUR_DRAWABLE_HERE". <<<<<<<<<
app:headerLayout="#layout/nav_header_home"
app:menu="#menu/activity_home_drawer" />
Unfortunately, this sets the same image for each entry - but, it is a good starting point. Will update if I see a way of customising each entry.
LAST UPDATE, This allows you to define a unique background for each entry. Its not perfect, but maybe you can tweak it to work:
include 'app' in your xml for your menu:
xmlns:app="http://schemas.android.com/apk/res-auto"
Then, make a simple layout file that has an image:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/announce"/>
</FrameLayout>
In menu, update the item to point to the layout file you made above
<item
android:id="#+id/nav_gallery"
android:title=""
app:actionLayout="#layout/test" /> <<<<< HERE
Source: found some methods and ideas here: https://stablekernel.com/article/using-custom-views-as-menu-items/

Bottom Navigation Icons are Cropped Android

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>

android - Set switch button into action bar with showAsAction="never"

I want to put a switch button into MenuItem with showAsAction="never" but it's not working. Here is my layout
<?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:context="com.trietnhm.multipleactivities.MainActivity"
>
<item
android:id="#+id/action_about"
android:orderInCategory="0"
app:showAsAction="never"
android:title="About"/>
<item
android:id="#+id/action_change_theme"
android:orderInCategory="3"
app:actionLayout="#layout/switcher"
app:showAsAction="never"
android:title="" />
<item
android:id="#+id/action_change_delimeter"
android:orderInCategory="2"
app:showAsAction="never"
android:title="Change Delimeter"/>
</menu>
This my Switch button
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.SwitchCompat
android:id="#+id/switchForActionBar"
android:layout_width="70dp"
android:layout_height="50dp"
android:scaleX="1.3"
android:scaleY="1.3"
android:switchMinWidth="45dp"
android:paddingTop="6dp" />
</RelativeLayout>
When I run the app and it's not show anything. Here the image https://drive.google.com/file/d/0B5zsq6J-qyY5bF8tYnBKVy11OHM/view?usp=sharing
Any help I would be appreciated. Thanks all.
android:showAsAction
never :
Never place this item in the app bar. Instead, list the item in the
app bar's overflow menu.
Of course, It won't show up. Perhaps you should use other options to show, always or ifRoom.
This line code
android:orientation="vertical"
is used with LinearLayout not with RelativeLayout
why are you using app:showAsAction="Never"?? for that you can also use app:showAsAction="ifRoom" or app:showAsAction="ifRoom|collapseActionView"

ActionBar menuItem not showing text

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.

actionlayout on menuitem does nothing

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>

Categories

Resources