Android drawable: state_selected working, superseding state_pressed? - android

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

Related

how to adding border to checkbox which is using png

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"/>

How to apply state press for a button in android xml, when I have already used up option of style and background?

I have a num keypad that I have created using buttons and have hide my android soft keyboard.
This is how my every button looks like for different numbers
<Button
android:id="#+id/seven"
android:text="7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
style="#style/CustomStyle"
android:background="#drawable/gridlayout_border"
/>
Notice that I am using CustomStyle whose code looks like this with the filename button_style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CustomStyle">
<item name="android:textSize">24dp </item>
<item name="android:textColor">#fdfdfd</item>
<item name="android:textStyle">bold </item>
<item name="android:padding">20dp</item>
</style>
</resources>
Also, I am using gridlayout_border as a background
gridlayout_border.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="-10dp" android:right="0dp" android:top="-10dp"
>
<shape android:shape="rectangle">
<!--apply button background transparent, full opacity-->
<solid android:color="#00ffffff"/>
<stroke android:color="#ffffff" android:width="2dp"/>
</shape>
</item>
</layer-list>
Since, I have used up the style and background tag in button. How can I now apply one more style/color when my button is being pressed on num keypad?
Thanks in advance
You can use selector to determine different states of a view. Something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<!-- Write your focused state code-->
</item>
<item android:state_pressed="true">
<!-- Write your pressed state code-->
</item>
<item>
<layer-list>
<item android:left="-10dp" android:right="0dp" android:top="-10dp"
>
<shape android:shape="rectangle">
<!--apply button background transparent, full opacity-->
<solid android:color="#00ffffff"/>
<stroke android:width="2dp" android:color="#ffffff"/>
</shape>
</item>
</layer-list>
</item>
</selector>

How do I apply the default "blue button"-style used on android first start?

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

Android: make list item with background clickable

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.

Override onClick color / drawable

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.

Categories

Resources