Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to align menu items horizontally [duplicate]

Tags:

android

How to create menu items aligned horizontally in the first row like google chrome

enter image description here

I created the first item with a menu child but by this way i got a row that send me to the other menu, So how to do that using menu file ? i don't want to use Dialog

Here is my menu file :

<?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">

    <group android:id="@+id/toolbar_menu"
        android:visible="true">
    <item
        android:id="@+id/item_tabs"
        android:icon="@drawable/ic_tabs_24dp"
        android:title="@null"
        app:showAsAction="always"/>
    <item
        android:id="@+id/icon_row_menu_id"
        android:title="@null"
        android:visible="true">
        <menu>
            <item
                android:id="@+id/item_back"
                android:icon="@drawable/ic_arrow_back_24dp"
                android:title="@string/go_back" />
            <item
                android:id="@+id/item_forward"
                android:icon="@drawable/ic_arrow_forward_24dp"
                android:title="@string/go_forward" />
            <item
                android:id="@+id/item_bookmark_page"
                android:icon="@drawable/ic_star_border_24dp"
                android:title="@string/bookmark_this_page" />
            <item
                android:id="@+id/item_download_page"
                android:icon="@drawable/ic_download_24dp"
                android:title="@string/download_page" />
            <item
                android:id="@+id/item_info"
                android:icon="@drawable/ic_info_outline_24dp"
                android:title="@string/view_site_information" />
        </menu>
    </item>
    <item
        android:id="@+id/item_new_tab"
        android:title="@string/new_tab" />
    <item
        android:id="@+id/item_new_incognito_tab"
        android:title="@string/new_incognito_tab" />
    <item
        android:id="@+id/item_bookmarks"
        android:title="@string/bookmarks" />
    <item
        android:id="@+id/item_history"
        android:title="@string/history" />
    <item
        android:id="@+id/item_downloads"
        android:title="@string/download" />
    <item
        android:id="@+id/item_settings"
        android:title="@string/settings" />
    </group>
</menu>

And here how i use it :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    mainMenu = menu;
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
}
like image 772
Amine Harbaoui Avatar asked Jul 21 '17 05:07

Amine Harbaoui


People also ask

How do you arrange items horizontally in CSS?

To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.

How do I make a horizontal div stack?

style="overflow:hidden" for parent div and style="float: left" for all the child divs are important to make the divs align horizontally for old browsers like IE7 and below. For modern browsers, you can use style="display: table-cell" for all the child divs and it would render horizontally properly.


1 Answers

You can use app:actionLayout="@layout/layout_name" property of item tag like

<item
android:id="@+id/menu_id"
android:title="@string/title"
 app:showAsAction="never"
app:actionLayout="@layout/layout_name"/>

and your layout_name is any layout file like

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent">

// add your horizontal menu item here

</LineearLayout>

and you can access this item

 @Override 
  public boolean onPrepareOptionsMenu(Menu menu) 
   {
    MenuItem item = menu.findItem(R.id.menu_id);
    Linearlayout rootView = (LinearLayout)item.getActionView();
    YourControlClass control = (YourControlClass) 
    rootView.findViewById(R.id.control_id); 
    return true; 
    } 
like image 69
Anil Avatar answered Oct 09 '22 07:10

Anil