Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show menu popup exact below actionbar?

I am working on popup-menu in actionbar. But I am stuck to display exact below of actionbar(cut-to-cut).I am putting two snapshot.

My issue screen shot:

enter image description here

I want exact popup menu below of actionbar as below screenshot

Correction screenshot:

enter image description here

My code snippet:

<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_filter"
    android:icon="@drawable/ic_filter_white_18dp"
    android:title="@string/action_filter"
    app:showAsAction="ifRoom" />
<item android:id="@+id/action_label"
    android:icon="@drawable/ic_add_circle_outline_white_18dp"
    android:title="@string/action_label"
    app:showAsAction="ifRoom" />
<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    app:showAsAction="never" />

like image 284
Vasant Avatar asked Jan 12 '16 08:01

Vasant


People also ask

What is the android Action bar?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.

How do I replace my action bar toolbar?

You'll want to add android:fitsSystemWindows="true" to the parent layout of the Toolbar to ensure that the height of the activity is calculated correctly. When using the support library, make sure that you are importing android. support. v7.

Where the action bar is present in the activity screen by default?

In Android applications, ActionBar is the element present at the top of the activity screen.


5 Answers

When working with AppCompat Theme, below code help you.Either Kitkat or Lollipop

Make your style.xml like below

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/black</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="actionOverflowMenuStyle">@style/OverflowMenu</item>
</style>


<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
    <item name="android:windowDisablePreview">true</item>
    <item name="overlapAnchor">false</item>
    <item name="android:dropDownVerticalOffset">5.0dp</item>
    <!--<item name="android:popupBackground">#FFF</item>-->
</style>
like image 177
Ness Tyagi Avatar answered Sep 30 '22 12:09

Ness Tyagi


If you want to keep using ActionBar and AppCompat theme rather than ToolBar or Holo theme, you can use this code:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="actionOverflowMenuStyle">@style/MyOverflowMenu</item>
</style>

<style name="MyOverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
    <item name="overlapAnchor">false</item>
    <item name="android:overlapAnchor" tools:ignore="NewApi">false</item>
    <item name="android:dropDownVerticalOffset">4.0dip</item>
</style>

This worked for me.

like image 28
Sanket B Avatar answered Sep 30 '22 14:09

Sanket B


As per my code this is exact sotution by changing style.

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionMenuTextColor">@color/text_white</item>
    <item name="android:popupMenuStyle">@style/PopupMenu.Example</item>
    <item name="actionOverflowMenuStyle">@style/OverflowMenu</item>
</style>

<style name="PopupMenu.Example" parent="@android:style/Widget.Holo.Light.ListPopupWindow">
    <item name="android:popupBackground">#efefef</item>
</style>

<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
    <!-- Required for pre-Lollipop. -->
    <item name="overlapAnchor">false</item>

    <!-- Required for Lollipop. -->
    <item name="android:overlapAnchor">false</item>
    <item name="android:dropDownVerticalOffset">4.0dip</item>

</style>
like image 24
Vasant Avatar answered Sep 30 '22 13:09

Vasant


You can achieve this by style property overlapAnchor= false

 <style name="toolBarStyle" parent="AppTheme">
        <item name="popupTheme">@style/toolbarPopup</item>
    </style>

    <style name="toolbarPopup" parent="@android:style/Widget.Holo.ListPopupWindow">  <!--ThemeOverlay.AppCompat.Light-->
        <item name="android:popupBackground">#AF0000</item>
        <item name="overlapAnchor">false</item>
        <item name="android:dropDownVerticalOffset">5dp</item>

    </style>

and set that style in AppTheme like below

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!--   to avoid overlay of popup window in toolbar -->
        <item name="actionOverflowMenuStyle">@style/toolBarStyle</item>
</style>
like image 45
Ram Prakash Bhat Avatar answered Sep 30 '22 14:09

Ram Prakash Bhat


Set the theme at android manifest to

android:theme="@android:style/Theme.Holo.Light"
like image 31
Naheel Avatar answered Sep 30 '22 13:09

Naheel