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"/>
Related
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
How do I give a custom style that would enable me to put in corners and a drawable in the same in a button. The current way I am implementing this gives me an error:
Main Implemented background:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/comment"
android:state_focused="false"
android:state_pressed="false"
android:state_selected="false"/>
<item android:drawable="#drawable/comment_pressed"
android:state_focused="false"
android:state_pressed="true"
android:state_selected="false"/>
</selector>
comment.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#color/purple"/>
<corners android:radius="4dp"/>
</shape>
comment_pressed:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#drawable/abs__list_selector_background_transition_holo_light"/>
<corners android:radius="4dp"/>
</shape>
Here the #drawable/abs__list_selector_background_transition_holo_light is a drawable from the Sherlock library. And I do know that the error is from here. Is there anyway that I could achieve this ?? Curved edges plus a drawable item ?
If you look at the resources used in the Sherlock library you will see that abs__list_selector_background_transition_holo_light contains the following:
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/abs__list_pressed_holo_light" />
<item android:drawable="#drawable/abs__list_longpressed_holo" />
</transition>
Which refers to the following 9Patch images:
abs__list_pressed_holo_light
abs__list_longpressed_holo
The <corners android:radius="x"/> attribute is used for <shape> items, and has no meaning for 9Patch drawables.
To get around this, you could either;
create your own 9Patch drawables with radiused corners in a graphic editor, and use these instead of the default ones in the Sherlock library.
or
replace the 9Patch items in the <transition> with your own "rectangle" <shape> items. Set the color of these <shape> items to mimic the colors used in the 9Patches. You would then be able to use the <corners> attribute with these '' items.
Edit:
You need to understand that the abs__list_pressed_holo_light and abs__list_longpressed_holo files in the Sherlock library are not solid color hex values. They are nine-patch png bitmaps. Where you wrote;
<solid android:color="#drawable/abs__list_selector_background_transition_holo_light"/>
...in your xml, you were actually pointing at two bitmaps instead of at a color value.
Of the two possible solutions I suggested previously, I think the second option is probably best in your case. Here is a step by step of how you would do it.
1) Create two <shape> drawables and save them in your drawable folder:
my_holo_light_blue_shape
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#9933B5E5"/>
<corners android:radius="4dp"/>
</shape>
my_holo_dark_blue_shape
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#CC0099CC"/>
<corners android:radius="4dp"/>
</shape>
2) Create your own <transition> drawable and once again save it in your drawable folder:
my_holo_blue_transition
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/my_holo_light_blue_shape" />
<item android:drawable="#drawable/my_holo_dark_blue_shape" />
</transition>
3) You can then use your new <transition> drawable for the pressed state in your selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/comment"
android:state_focused="false"
android:state_pressed="false"
android:state_selected="false"/>
<item android:drawable="#drawable/my_holo_blue_transition"
android:state_focused="false"
android:state_pressed="true"
android:state_selected="false"/>
</selector>
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.
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.