Pages

Wednesday, 28 January 2015

Android v7 compatability PopupMenus, PopUp Window,

Here I will compare some of the solution I tried for the following task: 

I had to create a menu that has icons and is not part of ActionBar so I made the following conclusions:

PopupMenu - seems very nice, but unfortunately you can not just simply add icons to it. There were some possibilities in v7 and you will probably find them on the internet, but I would not recommend you using them. If you anyway want to do this you will have to add v7 compatability and this is how you can do this:

Android Manifest
    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="20" />

project.properties
android.library.reference.1=../appcompat_v7

Either choose this copy in the libs folder v7 v4 libraries (recomended)
or
<path to ADT bundle>\sdk\extras\android\support\v7\appcompat

Styles:

<resources>

    <style name="app_theme" parent="android:Theme">
        <item name="android:listViewStyle">@style/TransparentListView</item>
        <item name="android:expandableListViewStyle">@style/TransparentExpandableListView</item>
    </style>

    <style name="TransparentListView" parent="@android:style/Widget.ListView">
        <item name="android:cacheColorHint">@android:color/transparent</item>
    </style>

    <style name="TransparentExpandableListView" parent="@android:style/Widget.ExpandableListView">
        <item name="android:cacheColorHint">@android:color/transparent</item>
    </style>


</resources>

Another more reasonable option is PopupWindow - here you will be able to get icons, but of course you should not forget to add scroll to the menu. And this is the closest I got to what I wanted.

However the best solution for my case was this project:
https://github.com/haiwen/NewQuickAction

Have a look at the code, it is pretty simple and organized also it handles different cases.