I use the Sherlock actionbar library. I want to create a actionbar like this:
Two menu items on the right, and only one can be selected at one time (like a radio group).
My problem is, how can I keep selected state of menu item? Or can I set the menu item background programmatically?
I programmatically changed the menu item icon, like this:
Could anyone help me?
Edit
I'm using menuItem.setIcon(R.drawable.actionbar_icon1) to set the menu item icon.
And the drawable/actionbar_icon1.xml is defined as below:
<?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 like this:
<drawable name="actionbar_selected_bg">#0277ca</drawable>
#drawable/actionbar_icon1_img is an image.
This does not work, as the second picture shows.
Related
is it possible to add a button label like this as a toolbar action menu item?
I tried the following code below
<item
android:id="#+id/action_submit"
android:title="#string/submit_titlecase"
android:visible="false"
android:icon="#drawable/primary_btn"
app:showAsAction="always|withText" />
And it appears blank?
If i remove the android:icon, it appears the text but not the button sourrounding outline
Define a menu item in the following manner:
<item android:id="#+id/action_submit"
android:title="Submit"
app:showAsAction="always"/>
(Basically, if you show as action and you don't provide an icon, the title is shown as a button on the toolbar.)
But if you want it specifically to look like a button like you've put up in your question, make a drawable/mipmap file looking exactly like that and then add android:icon="#drawable/icon" (or #mipmap/icon) into your menu item code.
I've set up a simple menu with one position like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_language" android:title="#string/language"
android:orderInCategory="100" />
</menu>
using
Theme.AppCompat.Light.DarkActionBar
style. When I click the three dots the menu position appears instead of the icon, not below it? Is there any way to make it appear under the icon, not 'on' it?
Actually you can't do anything with this icon, but you can create menu item with android:actionLayout="#layout/custom_lauout". Then you can create Popup Window, which allows such positioning. In popupWindow you will show all items that must show under "three dots item".
Here rea a few links which can help you: http://developer.android.com/reference/android/widget/PopupWindow.html
https://stackoverflow.com/a/23516493/3864698
I want to set the background (red image) on selected tab bar.
Initially I set like this
When i change in the code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/selected_tab"
android:state_selected="true" />
</selector>
Its look like
My requirement to look like below this. Please help to achieve this.
you need to do like this way
For Tab background you need to create selector like this way
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/selected_tab"
android:state_selected="true" />
<item android:drawable="#drawable/default_tab"/>
</selector>
now you can create Button with different layout for your tab button like this way. For this requirement Button View to set your tab icon at drawable top with text.
tab_add_photo_btn.xml
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/tab_search"
android:background="#drawable/tab_selector"
android:drawableTop="#drawable/add_photo_icon"
android:text="ADD PHOTO"
android:textColor="#fff"
android:padding="10dp"/>
I feel its nothing to do with XML coding .. just make some design trick
you need to make two different twiky images for each tab icon .
make Image background of tab which have little top part transparent (Not selected resource )
make selected image with same size with contain selected background (Selected resource )
I am developing one app and in that i have one listview. In listview i want to change color of selected items only. It means if i clicked on 1st item it should change color and then again i click on 2nd item then color of 1st item will become normal and it will change color of 2nd item. Here i am using custom listview. and here position is selected item and CommonUtilities.getListPosition() is globally defined method for storing position. I am able to change color on select but when i click on 2nd item color doesn't change to its previous color.
if (position == CommonUtilities.getListPosition()) {
v.setBackgroundColor(Color.CYAN);
}else{
v.setBackgroundColor(Color.WHITE);
}
Apply the listSelector in the xml in the ListView like this:
android:listSelector="#drawable/list_selector"
And create list_selector.xml file in drawable folder with following content:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/row_not_selected" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="#color/row_selected" android:state_pressed="true" android:state_selected="true"/>
</selector>
and create two colors in the colors.xml with names row_not_selected, row_selected.
Thats it.Thanks.
Please let me know if anything else required.
ListView item background via custom selector.
This link will help you. Use a selector. Specify different color when pressed and in normal mode. I hope this is exactly what your looking for.
when you click a row in listview u can change the color of the listview on click. Then on release change back to normal state. Why do you want to change the 1st row color when you click 2nd row?. Each row in listview remains in normal state until it is clicked. On release goes back to normal state. The above link helps you achieve the same.
When i use the ActionBar i can add icons to the MenuItems as i would in earlier Android Versions. But the icons are always on the left side of the text. Can i place the icon on the right side somehow? Example below:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/somebutton"
android:title="mylabel"
android:icon="#drawable/icon"
android:showAsAction="ifRoom|withText"/>
</menu>
You can use an action view and arrange your own custom presentation and handling of the item, but that's really not recommended in this case. Honeycomb style guidelines are to place the icons on the left.