How to set the selected state of an action item

I use the Sherlock action library. I want to create an action bar as follows: Designed

Two menu items on the right, and only one can be selected at a time (for example, a radio group). My problem is how can I save the selected state of a menu item? Or can I programmatically set the background of a menu item?

I programmatically changed the menu item icon, for example:

broken menubar

Can anyone help me?

Edit I use menuItem.setIcon(R.drawable.actionbar_icon1) to set the menu item icon. And drawable/actionbar_icon1.xml is defined as follows:

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/actionbar_selected_bg" /> <item android:drawable="@drawable/actionbar_icon1_img"></item> </layer-list> 

and @drawable/actionbar_selected_bg is defined as follows:

 <drawable name="actionbar_selected_bg">#0277ca</drawable> 

@drawable/actionbar_icon1_img is an image.

This does not work, as the second image shows.

+4
source share

Source: https://habr.com/ru/post/1499568/


All Articles