4

My Question is how i can move my menu dropdown to the space with pink area(see second image).As if now it is appearing in the Action Bar area too that i don't want.I want my menu to be appear just below the Action bar.

Please guys help me out to solve this situation.

enter image description here

This is how menu is coming.

enter image description here

I want my menu to appear just below the Action Bar.

This is how my menu.xml looks like

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.mobileinventorysuit.MainActivity" >

    <item
        android:id="@+id/login_mnu_imports"
        android:icon="@drawable/import_menu"
        app:showAsAction="always"
        style="@style/OverflowMenu"
        android:title="Import"/> 
   <item
        android:id="@+id/login_mnu_settings"
        android:icon="@drawable/settings_menu"
        style="@style/OverflowMenu"
        app:showAsAction="always"
        android:title="Settings"/>
     <item
        android:id="@+id/login_mnu_dev"
        android:icon="@drawable/dev"
        style="@style/OverflowMenu"
        app:showAsAction="always"
        android:title="Device Info"/>
    <item
        android:id="@+id/login_mnu_exit"
        style="@style/OverflowMenu"
        app:showAsAction="always"
        android:icon="@drawable/ic_lock_power_off"
        android:title="Exit"/>


</menu>
Pranesh Sahu
  • 595
  • 5
  • 26
  • You can make a new xml layout fro menu. Then you can include that layout in the currnet main xml. Set it to hidden first. Add a button or any image view adn upon clicking on that view display your menu. I ahve done it many items. – RajSharma Aug 24 '15 at 06:50
  • Can you please send me some links or reference as i'm little new bee to it...that will be helpful for me. @Raj Sharma – Pranesh Sahu Aug 24 '15 at 06:52
  • this link would be helpful http://stackoverflow.com/questions/26979476/why-is-my-overflow-dropdown-menu-on-top-of-the-actionbar – RajSharma Aug 24 '15 at 07:04

4 Answers4

6

Actually the ActionBar menu should display on top of the action bar. From the Documentation

A menu is a temporary sheet of paper that always overlaps the app bar, rather than behaving as an extension of the app bar.

If you still want that behaviour you can use this. But its not recommended.

<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow" >
         <item name="overlapAnchor">false</item>
         <item name="dropDownVerticalOffset">?attr/actionBarSize</item>
</style>

Check this answer Why is my overflow dropdown menu on top of the actionbar?

EDIT

If you want icons placed in your ToolBar

add this in menu.xml

 xmlns:app="http://schemas.android.com/apk/res-auto"

and add this line in your menuitems

 app:showAsAction="always"
Community
  • 1
  • 1
Emil
  • 2,786
  • 20
  • 24
1

Go to res => values => styles and add those two lines I indicated in the comment below.It will help you

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

     //add these two line scripts and it will work properly

    <item name="overlapAnchor">false</item>
    <item name="android:dropDownVerticalOffset">?attr/actionBarSize</item>
</style>

  • Welcome to SO! Code only answers are generally not accepted. Always try to explain your solution and why this adds something to the current answers. – Erich Apr 16 '20 at 20:14
0

We can use this in PopUpWindow:

   mWindow.showAsDropDown(anchor, getScreenWidth((Activity) mContext) - getPixelFromDp(mContext, 465), getPixelFromDp(mContext, 0));

  public static int getScreenWidth(Activity context){
     Display display = context.getWindowManager().getDefaultDisplay();
     return display.getWidth();
  }

   public static int getPixelFromDp(Context context, int dpUnits) {
      float pixel = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpUnits, getDisplayMetrics(context));
      return (int) pixel;
 }
Emil
  • 2,786
  • 20
  • 24
Rotomac17
  • 239
  • 3
  • 16
0

I think this is correct

  <item name="overlapAnchor">false</item>
    <item name="android:dropDownVerticalOffset">?attr/actionBarSize</item>
Ness Tyagi
  • 2,008
  • 24
  • 18