Show overflow menu on Image button click
Asked Answered
P

1

0

I want to show the Overflow menu on ImageButton click.

menu/menu_scrollable_tab.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context="com.example.scrollingtab.activity.ScrollableTabsActivity">

    <item android:id="@+id/action_settings" 
        android:title="@string/action_settings"
        android:orderInCategory="100" 
        app:showAsAction="always"
        android:icon="@drawable/info" />

      <item android:id="@+id/action_settings1" 
        android:title="@string/action_settings"
        android:orderInCategory="100" 
        app:showAsAction="never" />

        <item android:id="@+id/action_settings2" 
        android:title="@string/action_settings"
        android:orderInCategory="100" 
        app:showAsAction="never" />
</menu>

I have disabled the default Action bar and created Action bar using layout. I am not using default action bar or toolbar since i need to customize the action bar more.

layout/actionbar.xml

<?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="50dp" >
 <LinearLayout      
     android:layout_width="fill_parent"
     android:layout_height="50dp"
     android:orientation="horizontal"
     android:background="@color/actionbar_bg"
     android:weightSum="2">

     <ImageButton android:id="@+id/ib_navigation"
         android:background="@drawable/nv_drawer_24x24"
         android:layout_width="0dp"
         android:layout_weight=".3"
         android:layout_height="50dp"/>

     <View 
         android:layout_width="0dp"
         android:layout_height="50dp"
         android:layout_weight="1.1"/>

     <ImageButton android:id="@+id/ib_navigation1"
         android:background="@drawable/info"
         android:layout_width="0dp"
         android:layout_weight=".3"
         android:layout_height="50dp"/>

     <ImageButton android:id="@+id/ib_navigation2"
         android:background="@drawable/ic_drawer"
         android:layout_width="0dp"
         android:layout_weight=".3"
         android:layout_height="50dp"/>     
 </LinearLayout>
</RelativeLayout>

As per the client requirement I have designed the action bar using layout and the look and feel are good.

The default OnCreateOptionsMenu(Menu menu) method is not populating the overflow menu. Now i want to populate the overflow menu on clicking the ImageButton with id= ib_navigation2. I don't have any idea to implement this.

Can any one help me on doing this.

Thanks

Proprietor answered 23/10, 2015 at 7:32 Comment(0)
T
0

You need to override the method for creating you own menu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}

The buttonclick can call openOptionsMenu();

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        openOptionsMenu();
    }
});

Or else

public myOnClickMethod(View v) {
    openOptionsMenu();
}

See here for more information about menus http://developer.android.com/guide/topics/ui/menus.html

Hope this will work for you

Tigon answered 23/10, 2015 at 7:38 Comment(2)
I have added the code for button onclick inside the onCreate method. This is working but the menu is shown at the bottom of the mobile screen. Is it possible to show the overflow menu just below the button i am clicking.Proprietor
Hi Any idea how to make it below the button i am going to click.Proprietor

© 2022 - 2024 — McMap. All rights reserved.