If I press a button in the action bar, then its background color is not what I want. The background color of my item doesn't respond to my click event. How can I change this and change the background color when it's pressed?
You need to declare android:actionBarItemBackground attribute which is a:
Custom item state list drawable background for action bar items.
Then, in your styles do as follows:
<style name="CustomStyle" parent="#style/Theme.Holo.Light" >
<item name="android:actionBarItemBackground">#drawable/ab_item_background</item>
<item name="actionBarItemBackground">#drawable/ab_item_background</item>
</style>
So, put your own drawable with a selector and every state (pressed, focused, disabled, etc) to have the expected background. For example, the drawable ab_item_background.xml declared above might be like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<!-- focused/pressed: color=red -->
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#color/red" />
<!-- pressed: color=red -->
<item
android:state_pressed="true"
android:drawable="#color/red" />
<!-- normal: color=transparent -->
<item
android:drawable="#android:color/transparent" />
</selector>
In Styling the Action Bar, you can find all the customization possibles and all the attributes to do so.
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0a0a0a")));
this might help
Related
I'm having trouble changing the color of my Navigation drawer switch items. I want each one to have a custom color associated with it's action.
I've defined my nav drawer with a menu like this:
<item
android:title="#string/nav_drawer_services_title">
<menu>
<group android:checkableBehavior="none">
<item
android:id="#+id/Red_selector"
android:title="#string/nav_drawer_red_title"
app:actionLayout="#layout/switch_layout"/>
<item
android:id="#+id/Blue_selector"
android:title="#string/nav_drawer_blue_title"
app:actionLayout="#layout/switch_layout"/>
</group>
</menu>
</item>
My switch_layout.xml is like this:
<Switch
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:onClick="toggleService"
android:checkable="true"
android:checked="true"
style="#style/MyCustomSwitchTheme"
/>
I've tried changing MyCustomSwitchTheme as suggessted here: Change "on" color of a Switch, yet the switch items remain the same color.
Why doesn't my custom theme style override the default? I can't seem to change these switches from the colorAccent defined in my colors.xml and AppTheme.
Anyone know a way around this?
Edit: Here is the styles code, sorry about not including that with the OP.
<style name="MyCustomSwitchTheme">
<!-- active thumb & track color (30% transparency) -->
<item name="colorControlActivated">#46bdbf</item>
<!-- inactive thumb color -->
<item name="colorSwitchThumbNormal">#color/light_gray_color</item>
<!-- inactive track color (30% transparency) -->
<item name="android:colorForeground">#42221f1f</item>
</style>
just use android:theme="#style/MyCustomSwitchTheme"
instead style="#style/MyCustomSwitchTheme"
I use a spinner to display a dropdown list.
I want to have the items in the list to have rounded corners.
So I use 9-patch containing an image with rounded corners (transparent on the outside of the corners) for the views items background and a selector to display a different color 9-patch when pressed.
Problem: When I press the items in the spinner's list, I can see a blue background in the corners, where the 9-patch is transparent.
I can't seem to get rid of this blue background that appears when the items are pressed. If I remove the 9-patches and any setting in the spinner, I can see that the items views in the list are by default grey, and blue when pressed.
I tried also to not use 9 patches as the background but just a color selector, and set the pressed color in the selector to be transparent. Then when I press the item, it's not transparent, but blueish. I think the view in the list is really transparent, but there is still the blue color in the background when pressed...
I use a custom SpinnerAdapter to create the items view.
Here is the simplified code:
private class MySpinnerAdapter implements SpinnerAdapter {
#Override
public View getDropDownView(int i, View recycledView, ViewGroup viewGroup) {
View view = new View(context);
view.setBackground(context.getResources().getDrawable(R.drawable.testspinner));
view.setMinimumHeight(100);
return (View) view;
}
}
The selector used for the background. Here with just a color, and no 9-patch. The pressed color should be transparent:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#android:color/transparent" />
<item
android:drawable="#android:color/holo_purple" />
</selector>
I set the custom adapter on the spinner:
spinner.setAdapter(new MySpinnerAdapter());
And the spinner is fetched from XML layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="wrap_content">
<Spinner
android:id="#+id/myDropDown"
android:spinnerMode="dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dropDownWidth="match_parent"/>
</LinearLayout>
I have tried setting many different attributes on the Spinner, and tried some styling attributes, but I could not get rid of this blue background...
Thank you #Vikram for pointing out the use of styles.
However, after investigations it turns out that the background of the pressed item in the Spinner doesn't come from the attribute you suggested, but from android:listSelector. So I could fix the problem like this:
Define a new style in styles.xml:
<style name="MyListView">
<item name="android:listSelector">#android:color/transparent</item>
</style>
Define a new theme in themes.xml:
<style name="MyTheme" parent="#android:style/Theme.NoTitleBar.Fullscreen">
<item name="android:dropDownListViewStyle">#style/MyListView</item>
</style>
Apply the theme to my activity in AndroidManifest.xml:
<activity android:name=".MyActivity"
...
android:theme="#style/MyTheme">
Problem: When I press the items in the spinner's list, I can see a
blue background in the corners, where the 9-patch is transparent.
I think the blue background comes from android:selectableItemBackground attribute. To change this attribute, add the following to your application's theme in styles.xml:
<item name="android:selectableItemBackground">#drawable/whicheverDrawable</item>
For reference: By default, selectableItemBackground points to the following drawable in API 19 (for Theme.Holo). You should define whicheverDrawable in a similar way:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/list_selector_disabled_holo_dark" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/list_selector_disabled_holo_dark" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/list_selector_background_transition_holo_dark" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/list_selector_background_transition_holo_dark" />
<item android:state_focused="true" android:drawable="#drawable/list_focused_holo" />
<item android:drawable="#color/transparent" />
</selector>
In your case, you can define rounded corners for another drawable, which is used for statePressed in whicheverDrawable.
I have the following dropdown:
<style name="SpinnerDropdown" parent="android:style/Widget.ListView.DropDown">
<item name="android:background">#04384f</item>
<item name="android:divider">#000000</item>
<item name="android:dividerHeight">1dp</item>
</style>
I want to change the color of the currently pressed item. This color is currently orange (#feba21) and I want it to be something else. I haven't set it anywhere so I assume it's inherited from Widget.ListView.Dropdown
How can I change it, and more importantly where can I find Windget.ListView.Dropdown in the SDK so I can see what properties it has?
I tried adding the selector_spinner_option:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/bright_positive" />
<item android:drawable="#color/dimmer_positive" />
</selector>
as the background of SpinnerDropdown, but what it does is change all the other items when one item is pressed - while that item remains orange!!
Orange is not the background of dropdown item. It is a color of selector. It is easy to change it by the attribute listSelector
The following code will change orange to green
<style name="SpinnerDropdown" parent="android:style/Widget.ListView.DropDown">
<item name="android:background">#04384f</item>
<item name="android:divider">#000000</item>
<item name="android:listSelector">#00ff00</item>
<item name="android:dividerHeight">1dp</item>
</style>
Hope it help.
I want to create a ImageButton and set an image as background mantaining original size. So I use this few lines of code find on StackOverflow too:
ImageButton retry = new ImageButton(this);
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.button2);
retry.setImageBitmap(image);
retry.setMinimumWidth(image.getWidth());
retry.setMinimumHeight(image.getHeight());
But unfortunately I obtain following result:
Obviously I don't want the "background button", but only the image. How can I do?
It depends on your image but. You can achieve this by writing style to your button.
Firstl you should define your shape for your button in drawables folder(if it doesnt exist dont hesitate to create and define button_shape.xml).
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" /> <!-- **pressed button pressed is name of image...** -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="#drawable/button_focused" /> <!-- hovered -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
After you define your style in style file.
<style name="button_style">
<item name="android:textColor">#ffffff</item>
<item name="android:background">#drawable/button_states</item>
</style>
and set style property of Button.
set button's background to transparent color or null.
I am creating an actionbar with custom looking buttons that I put in the icon section of menu.xml.
The problem is that, when I press them, I see both the selected version of the image of the button and the blue background of the holo theme.
This is my menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/current_position"
android:icon="#drawable/ab_location_layer"
android:menuCategory="container"
android:showAsAction="ifRoom"
android:title="Current position">
</item>
</menu>
The ab_location_layer is this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ab_location_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/ab_location" android:state_pressed="false"/>
</selector>
The ab_location_layer contains an image that is smaller than the actionBar.
How is possible to remove the blue background on actionbar? I am also using actionbarsherlock.
Thanks
I found the answer on ActionBarSherlock Mailing List:
you have to put this
<item name="android:selectableItemBackground">#null</item>
<item name="android:actionBarItemBackground">#null</item>
<item name="actionBarItemBackground">#null</item>
to your theme and not in ActionBar theme