I'm trying to find out, how to override standard onClick color (yellow) for Button with orange color?! (during writing this question I've seen, that these are no colors, but images)
Is there the easy way to do that? Or should I write a new style in that case?
I found in GIT, how com.android.internal.R.attr.buttonStyle looks like. And I would like just copy and modify that button style a bit. But if I do it on that way, I get XML-Errors
<?xml version="1.0" encoding="utf-8"?>
<selector>
<item
android:state_window_focused="false"
android:state_enabled="true"
android:drawable="#drawable/btn_default_normal"/>
<item
android:state_window_focused="false"
android:state_enabled="false"
android:drawable="#drawable/btn_default_normal_disable" />
<!-- Modified item -->
<item
android:state_pressed="true"
android:drawable="#drawable/btn_default_selected" />
<item
android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_default_selected" />
<item
android:state_enabled="true"
android:drawable="#drawable/btn_default_normal" />
<item
android:state_focused="true"
android:drawable="#drawable/btn_default_normal_disable_focused" />
<item
android:drawable="#drawable/btn_default_normal_disable" />
</selector>
Ok, I guess, I must fill selector's attribute xmlns:android. If I fill it with "http://schemas.android.com/apk/res/android", I get other errors. This time, because the android's drawable resources cann't be found.
Any suggestions?!
Thank you,
Mur
One working example. This xml is set as background drawable. Adjust the colors as you like.
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#FFAAAAAA"
android:endColor="#FFAAAAAA"
android:angle = "180" />
<corners
android:radius="10dip" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:startColor="#FF888888"
android:endColor = "#FF888888"
android:angle = "180"/>
<corners
android:radius="10dip" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#FFFFFFFF"
android:endColor = "#FFFFFFFF"
android:angle = "180" />
<corners
android:radius="10dip" />
</shape>
</item>
</selector>
Additionally, you can define Stroke (button border).
Mur, did you copy these resources (e.g. btn_default_normal) into your drawables folder? You'll have to get these resources (they can be found in your sdk folder under platforms/android-8/data/res/ then the different drawable folders) and then modify them to be the style that you need for each selector state.
Related
Having defined drawable with given png for each items in the drawable for different states, if want to having border around the png for that item, how to do it? re-cut the png is not option here.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_focused="true"
android:drawable="#drawable/box_checked_png” />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/white_box_png” />
<item android:state_checked="false"
android:drawable="#drawable/white_box_png” />
<item android:state_checked="true"
android:drawable="#drawable/box_checked_png” />
</selector>
The method which i find best is to add a background to the check box.
This is a rectangle drawable background.xml (put into res/drawable folder).
Let's say you are making a shape like this one :
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#ffffff" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>
And where you have your check box, you just set the background made before:
<CheckBox android:text="Some me" android:background="#drawable/background"/>
Newbie in Android.
I have the following defined in res/drawable/ for a button in a menu that's defined in style.xml
<style name="menu_icon">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">#dimen/menu_item_height</item>
<item name="android:background">#drawable/menu_item_bg_sel</item>
</style>
Now, menu_item_bg_sel in drawable has two different color gradients for 2 states that I am interested in- pressed and selected.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<layer-list>
<item>
<shape>
<gradient android:angle="180"
android:endColor="#color/background_menu_gray_selected2"
android:centerColor="#color/background_menu_gray_selected1"
android:startColor="#color/background_menu_gray_selected" />
</shape>
</item>
</layer-list>
</item>
<item android:state_selected="true">
<layer-list>
<item>
<shape>
<gradient android:angle="180" android:endColor="#color/background_menu_home2"
android:centerColor="#color/background_menu_home1"
android:startColor="#color/background_menu_home" />
</shape>
</item>
</layer-list>
</item>
<item android:drawable="#color/transparent"/>
However, when I press the button (that transient state) the button still creates the gradient taking colors from the selected_state only.
What am I doing wrong? Any ideas?
state_selected is commonly used when navigating using d-pads or maybe a pen like what Samsung Notes so I suggest not to use state_selected since when you press automatically it will be selected or gain focus although selected and focus are kind or different. I suggest to use state_pressed, state_focused and state_hovered instead.
For more info click here
You can use this code. You can copy paste this code into one drawable file. You can assign different drawable files or define shapes in this file itself for the different states.I have assigned a drawable file for the focused state as an example.
simple_button_states.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:drawable="#drawable/rounded_edittext_focused"
/>
<!--pressed -->
<item
android:state_pressed="true" />
<!--selected -->
<item
android:state_selected="true" />
<!-- focused -->
<item
android:drawable="#drawable/rounded_edittext_unfocused"/>
</selector>
round_edittext_focused.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<solid android:color="#FFFFFF" />
<stroke
android:width="2dp"
android:color="#color/CornflowerBlue" />
<corners
android:topLeftRadius="4dp"
android:topRightRadius="4dp"
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
/>
</shape>
Finally assign the background of the actual button as the drawable file that you have declared above.
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="#drawable/simple_button_states"
/>
Hope this helps
Namely, how do I apply this button style? I'm refering to the blue button in the lower right corner.
create a drawable folder in res folder then create XML file and call it button_background
within the XML file you will define the shape of the button, like what is the color, and corner, stroke and padding everything you can do here. The following code is similar to the button:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#56b7cc" />
<stroke
android:width="1dp"
android:color="#70daf1" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#68c8dd"
android:endColor="#70daf1"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#70daf1" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
then in your layout give the button the following:
<Button
android:id="#+id/Button1"
android:layout_width="50dip"
android:layout_height="40dip"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="107dp"
android:background="#drawable/button_background"
android:textColor="#ffffff"
android:text="ok" />
I made fixed width and height so it look as the picture, the most important attribute is
android:background="#drawable/button_background"
you can modify it as much as you want by changing the colors, padding read more about it here. hope this is what you are looking for
Its android ShowcaseView functionality. You can refer it here
If you want only button style, you need create own button style and based on, for example:
on nine-patch image (using nine-patch image as Background of Button android, Button with 9-patch image background doesn't stretch correctly),
or based on shape (How to create custom button in Android using XML Styles)
style.xml
<style name="Button.Blue" parent="android:Widget.Holo.Light.Button">
<item name="android:background">#drawable/xbg_blue_button</item>
<item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textStyle">bold</item>
</style>
xbg_blue_button.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 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/btn_default_disabled_holo_light" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/btn_default_disabled_focused_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/btn_default_pressed_holo_light" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/btn_default_normal_holo_light" />
<item android:state_focused="true" android:drawable="#drawable/btn_default_focused_holo_light" />
<item android:drawable="#drawable/btn_default_normal_holo_light" />
drawables:
https://dl.dropboxusercontent.com/u/54394631/buttons.rar
I have a list where I have defined my own list view items with a custom layout. This layout has a background with a custom drawable.
My custom layout for the ListView item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/item"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true" >
...
</RelativeLayout>
My custom drawable item.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- background: shadow -->
<item>
<shape
android:dither="true"
android:shape="rectangle" >
<corners android:radius="2dp" />
<solid android:color="#color/itemShadowColor" />
</shape>
</item>
<!-- foreground: surface -->
<item android:bottom="2dp">
<shape
android:dither="true"
android:shape="rectangle" >
<corners android:radius="2dp" />
<solid android:color="#color/itemBackgroundColor" />
</shape>
</item>
</layer-list>
Now this item is not clickable anymore.
Can you explain me why, and what I have to do to have the same behavior (selector with the blue background) like a button click?
Define a selector with its pressed and default states in res/drawable folder(one of the state will be your #drawable/item). Set it as the bg of your list row layout.
See similar question and answer :Selector on background color of TextView
Edit:
Best way to understand and apply something like google did is, to look into SDK and do similar things to that. For instance look at the btn_default_holo_dark drawable. It is a selector with states and yes it is a xml.
This is a selector taken from sdk (sdk\platforms\android-18\data\res\drawable\btn_default_holo_dark.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_holo_dark" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/btn_default_disabled_holo_dark" />
<item android:state_pressed="true"
android:drawable="#drawable/btn_default_pressed_holo_dark" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_default_focused_holo_dark" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_holo_dark" />
<item android:state_focused="true"
android:drawable="#drawable/btn_default_disabled_focused_holo_dark" />
<item
android:drawable="#drawable/btn_default_disabled_holo_dark" />
</selector>
These are the images taken from sdk (sdk\platforms\android-18\data\res\drawable-xhdpi):
When you apply this drawable/selector (#drawable/btn_default_holo_dark) to any view, you are going to have its states. I hope this sample makes my answer more clear.
With the default selector, long-pressing a list item causes its background to transition between two colors.
Replacing the selector with the one below removes the effect. According to this question, I need an animation to reproduce it. How would I go about doing that in xml?
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true">
<shape>
<solid
android:color="#color/state_pressed" />
</shape>
</item>
<item
android:state_focused="true">
<shape>
<solid
android:color="#color/state_focused" />
</shape>
</item>
<item>
<shape>
<solid
android:color="#color/state_idle_grey" />
</shape>
</item>
</selector>
Here is the code from list_selector_background :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#android:color/transparent" />
<!--
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_background_disabled" />
<item android:state_focused="true" android:state_enabled="false"
android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="true"
android:drawable="#+drawable/list_selector_background_focus" />
</selector>
Found on the web.
And it uses this transition for long press clicks :
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/list_selector_background_pressed" />
<item android:drawable="#drawable/list_selector_background_longpress" />
</transition>
Found on the web too .
There is no animation for that. And remember to keep you states in the same order, or at least think about it if you swap them, order is important.
Personnally, I like when things behave in a standard way, so I would just let the standard list selector.
Regards,
Stéphane