Action-bar — sub menu item with text and image not working properly
Asked Answered
G

2

6

I am developing small Android application in which I am using action bar with some menu items. One menu item contains sub menu. Now what I want to do is to display menu item with text and icon always.

I define menu items in following manner.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/card_menu"
        android:icon="@drawable/abc"
        android:showAsAction="withText|always"
        android:title="cards">

        <menu>

            <item
                android:id="@+id/C1"
                android:title="C1"/>

            <item
                android:id="@+id/C2"
                android:title="c2"/>

            <item
                android:id="@+id/C3"
                android:title="C3"/>
        </menu>
    </item>

    <item
        android:id="@+id/notification"
        android:actionLayout="@layout/notification_icon"
        android:icon="@drawable/notification"
        android:showAsAction="always"
        android:title="Notifications"/>

    <item
        android:id="@+id/filter"
        android:icon="@drawable/filter"
        android:showAsAction="always"
        android:title="Filter"/>

</menu>

Now what happens is my action-bar displays my items properly; only thing is that when window is in portrait mode it shows only image, and when window is in landscape mode it shows image and text both. But I want image and text in both mode.

How to do that? Is there any way to solve this?

Geek answered 18/2, 2013 at 8:34 Comment(1)
Hi JOk thank you for reply I tried your edits but its not working for me. Ir shows text iand icon only in landscape mode not in portrait mode... Is there any other solution.. Need help.. Thank you ...Geek
M
1

If you are using the ActionBar from the Android Sopport Library (android.support.v7.app.ActionBar), you might need to change your menu and item declaration from:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/notification"
    android:actionLayout="@layout/notification_icon"
    android:icon="@drawable/notification"
    android:showAsAction="always"
    android:title="Notifications"/>

to

<!-- Your menu -->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourappname="http://schemas.android.com/apk/res-auto" >


<!-- Your item to be shown as action-->    
<item
    android:id="@+id/notification"
    android:actionLayout="@layout/notification_icon"
    android:icon="@drawable/notification"
    android:showAsAction="always"
    yourappname:showAsAction="always"
    android:title="Notifications"/>

I had a similar problem when providing the compatibility to older android versions.

Mulderig answered 18/9, 2013 at 18:23 Comment(0)
R
0

better using android:showAsAction="ifRoom|withText" than always.
In that case depends on your text size and screen width.

Robinetta answered 21/4, 2013 at 7:53 Comment(1)
That's not really an answer to the question.Planar

© 2022 - 2024 — McMap. All rights reserved.