partiz
partiz

Reputation: 1216

why `ifroom` does not work - android studio

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?

Upvotes: 0

Views: 1106

Answers (1)

Durga Mohan
Durga Mohan

Reputation: 1110

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

Upvotes: 1

Related Questions