Using custom view for overflow menu items

I had a real difficulty doing anything on the sherlock overflow menu in the action bar.

screenshot

Ideally, I would like to use a custom TextView for each element to set a different font on it and change the color of the pressed state.

I tried (all without success):

Change the action bar overflow style
Actionbar style overflow menu items
listview as action overflow in sherlock action bar
https://groups.google.com/forum/#!msg/actionbarsherlock/5lHOKNlXn_4/f9XicMXbFFAJ

My application will have different fragments, all expanding BaseFragment with different elements in the overflow menu. I also use v4 support package. I create my menu as follows:

 @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.activity_borrow, menu); } 

activity_borrow.xml:

 <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_filter" android:title="test" android:orderInCategory="100" android:showAsAction="never" /> </menu> 

My application uses a theme that inherits from Theme.Sherlock .

How can I use a custom view inside this menu? Or at least how can I change the default blue pressed state?

+4
source share
1 answer

To change the colors of overflow list elements, add two elements to the application theme, which are usually defined in res/values/styles.xml :

 <item name="android:dropDownListViewStyle">@style/DropDownListView</item> <item name="dropDownListViewStyle">@style/DropDownListView</item> 

In the same file, add the style you just assigned:

 <style name="DropDownListView" parent="@style/Widget.Sherlock.ListView.DropDown"> <item name="android:listSelector">@drawable/selectable_background</item> </style> 

And finally, create a drawable selectable_background.xml selector in the drop-down folder containing the following:

 <selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_mediumAnimTime" > <item android:state_pressed="false" android:state_focused="true" android:drawable="@color/focussed_color" /> <item android:state_pressed="true" android:drawable="@color/pressed_color" /> <item android:drawable="@android:color/transparent" /> </selector> 

Finally, define the colors that usually fit in colors.xml :

 <resources> <color name="pressed_color">#FF8E4067</color> <!-- your purple tone already ;) --> <color name="focussed_color">#DD8E4067</color> </resources> 

In my application, I used the "ActionBar Style Generator" as the proposed baboo, which handles everything for you conveniently. For this answer, I simply extracted and simplified the parts that, in my opinion, make up the overflow menu style.

I think there is some secret about the stylization effects of three different objects:

  • In my opinion, android:dropDownListViewStyle includes an overflow menu that hides behind the “three dots” in the ActionBar.
  • Not to be confused with android:actionDropDownStyle , which is used to style the application’s drop-down list if you used actionBar.setNavigationMode(NAVIGATION_MODE_LIST)
  • However, some Android devices with a hardware menu button (for example, Nexus S or Galaxy S3 Mini) do not display the “three dots”, but the overlay menu, which is at the bottom of the screen if the hardware menu button is pressed. android:popupMenuStyle is the right attribute for the style.

Again, this is only as far as I remember from my own application development.

Also, make sure that other style files (for example, folders with configuration qualifiers ) overwrite the styles you just created.

In general, I understand that this will only change the background color of the list items. To use a completely non-standard view, you can create a custom counter view , add a dummy button with the "three dots" icon to your ActionBar and open the counter, click the button.

+4
source

All Articles