I want a button to look activated (transparent) from the start.
My selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" >
<shape>
<solid
android:color="#android:color/transparent" />
</shape>
</item>
<item android:state_pressed="false" android:state_focused="false" android:state_activated="false">
<shape>
<gradient
android:startColor="#ef4444"
android:endColor="#992f2f"
android:angle="270" />
</shape>
</item>
And the code of the button:
<Button
android:background="#drawable/my_bg"
android:state_activated="true"
/>
The default color of the button is red, so I guess the problem is in android:state_activated="true". How can I solve this?
please use this it will help you
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#2d7ebb" />
<corners android:radius="5dp">
<stroke android:width="2dip" android:color="#2d7ebb" />
</corners>
</shape></item>
<item android:state_pressed="true"><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#2d7ebb" />
<corners android:radius="5dp">
<stroke android:width="2dip" android:color="#2d7ebb" />
</corners>
</shape></item>
<item><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#55acee" />
<stroke android:width="2dip" android:color="#55acee" />
<corners android:radius="5dp" />
</shape></item>
</selector>
Related
How can I create shape with state in Kotlin(or java)
for example I want create below shape with Kotlin(or java)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="999dp" />
<stroke android:width="6dp" android:color="#bfbfbf" />
<solid android:color="#f44336" />
</shape>
</item>
<item android:state_pressed="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="999dp" />
<stroke android:width="2dip" android:color="#E0E0E0" />
<solid android:color="#f44336" />
</shape>
</item>
<item android:state_checked="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="999dp" />
<stroke android:width="2dip" android:color="#E0E0E0" />
<solid android:color="#f44336" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="999dp" />
<stroke android:width="6dp" android:color="#bfbfbf" />
<solid android:color="#f44336" />
</shape>
</item>
I see this link1 and link2 but i wonder how to create state to drawable
I am creating buttons dyanmically for a gridview, however I want to apply a drawable background presented below. However the background is black, but on pressed appears correctly.
I have a button created called roundedge_button_dark.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="6dip" />
<gradient android:angle="-90" android:startColor="#3f3f3f" android:endColor="#a6a6a6" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="6dip" />
<solid android:color="#ff0000"/>
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<corners android:radius="6dip" />
<gradient android:angle="-90" android:startColor="#0F0F0F" android:endColor="#000000" />
</shape>
</item>
</selector>
I'm accessing said drawable button like so:
button.setBackgroundResource(R.drawable.roundedge_button_dark);
The problem I have is that the image shows black, which is the end colour for the non state defined item; What am I missing or is this not possible?
Try this,
<item android:state_pressed="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp" android:color="#ff0000" />
<solid android:color="#ffff00" />
<padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp" android:color="#ff0000" />
<solid android:color="#FF6699" />
<padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" />
<corners android:radius="5dp" />
</shape>
</item>
or try this,
first create your drawable xml file for button states:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Button Pressed-->
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/rounded_button_click_effect"
/>
<!-- Button Default Image-->
<item android:drawable="#drawable/rounded_button"/>
</selector>
after that create rounded_button_click_effect.xml file in case of button click:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<corners android:radius="6dip" />
<gradient android:angle="-90" android:startColor="#3f3f3f" android:endColor="#a6a6a6" />
</shape>
</item>
</layer-list>
and finally create rounded_button.xml file in case of default state:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<corners android:radius="6dip" />
<gradient android:angle="-90" android:startColor="#0F0F0F" android:endColor="#000000" />
</shape>
</item>
</layer-list>
i am trying to add a click-effect on my xml-created button. So far i got the clickeffect running, but i cant manage to supply the click-effect the same rounded corners i have on the unclicked button; it looks horrible this way. In my gradient.xml I am supplying the same imagelayout with the rounded corners, but it seems to ignore this setting.
Can someone point me in the right direction?
< --- >
my box with clickeffect.xml: (box_with_click_effect.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/rounded_box" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/gradient" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/gradient" />
<item android:drawable="#drawable/rounded_box" />
</selector>
my button-drawable-xml: (rounded_box.xml)
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#color/my_orange"/>
<corners android:radius="15px"/>
<padding android:left="1dp" android:top="1dp" android:right="1dp" android:bottom="1dp" />
</shape>
my gradient.xml for the clicked-state: (gradient.xml)
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<!-- THIS DOESN'T WORK: <bitmap android:src="#drawable/rounded_box"/>
SO I TRY THIS: -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#color/cocus_orange"/>
<corners android:radius="15px"/>
<padding android:left="1dp" android:top="1dp" android:right="1dp" android:bottom="1dp" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="90"
android:startColor="#880f0f10"
android:centerColor="#880d0d0f"
android:endColor="#885d5d5e"/>
</shape>
</item>
</layer-list>
Change your code to this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<!-- THIS DOESN'T WORK: <bitmap android:src="#drawable/rounded_box"/>
SO I TRY THIS: -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#color/cocus_orange"/>
<corners android:radius="15px"/>
<padding android:left="1dp" android:top="1dp" android:right="1dp" android:bottom="1dp" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="90"
android:startColor="#880f0f10"
android:centerColor="#880d0d0f"
android:endColor="#885d5d5e"/>
<corners
android:radius="15px" />
</shape>
</item>
</layer-list>
You can use this website to generate you buttons: android button maker
please try if needed:
By this we can avoid adding individual files for state pressed and normal. but serves perfectly.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="5dip" />
<solid android:color="#a4c639"/>
<stroke android:width="1px" android:color="#0dbcbf" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="5dip" />
<stroke android:width="1dip" android:color="#a4c639" />
<solid android:color="#a4c639"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="5dip" />
<solid android:color="#0dbcbf"/>
</shape>
</item>
</selector>
I have a custom button button.xml. Its default (unpressed) drawable is button_unclicked.xml and the button_clicked.xml is called when the button is clicked. The problem is the button doesn't display its clicked and unclicked drawables, but only a text inside the button (there is no borders).
I'm setting my custom button as a background:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/apply"
android:id="#+id/button_apply"
android:onClick="Apply"
android:layout_marginBottom="10dp"
android:layout_gravity="center_horizontal"
android:background="#drawable/button" />
button_clicked:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#FFE75C" />
</shape>
button_unclicked:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
</selector>
button.xml:
<item android:state_pressed="true"
android:drawable="#drawable/button_clicked">
</item>
<item android:state_pressed="false"
android:drawable="#drawable/button_unclicked">
</item>
output (there should be borders but there aren't):
// try this
drawable/selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="false" >
<shape
android:shape="rectangle" >
<gradient android:startColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
</item>
<item android:state_enabled="true" android:state_focused="false" android:state_pressed="false" >
<shape android:shape="rectangle" >
<gradient android:startColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#FFE75C" />
</shape>
</item>
<item android:state_pressed="true" >
<shape
android:shape="rectangle" >
<gradient android:startColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
</item>
<item android:state_focused="true" >
<shape
android:shape="rectangle" >
<gradient android:startColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
</item>
</selector>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Button"
android:padding="5dp"
android:background="#drawable/selector"/>
Try following this template:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_default_normal_holo_light" android:state_enabled="true" android:state_window_focused="false"/>
<item android:drawable="#drawable/btn_default_disabled_holo_light" android:state_enabled="false" android:state_window_focused="false"/>
<item android:drawable="#drawable/btn_default_pressed_holo_light" android:state_pressed="true"/>
<item android:drawable="#drawable/btn_default_focused_holo_light" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="#drawable/btn_default_normal_holo_light" android:state_enabled="true"/>
<item android:drawable="#drawable/btn_default_disabled_focused_holo_light" android:state_focused="true"/>
<item android:drawable="#drawable/btn_default_disabled_holo_light"/>
</selector>
You don't have to put all of the <item> tags.
I have applied a shape for a button like:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient android:startColor="#DD000000" android:endColor="#DD2d2d2d" android:angle="90"></gradient>
<corners android:radius="15dip"></corners>
</shape>
Now I want to use a selector like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/active"
android:state_pressed="true" />
<item android:drawable="#drawable/passive"/>
for this Button as well. Is it possible ...???
use this way:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>.......</shape>
</item>
..........
..........
</selector>
Detailed to the point answer
Create a color resource in
res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="yellow" type="color">#F7B500</item>
<item name="yellow_dark" type="color">#AC7E00</item>
<integer-array name="androidcolors">
<item>#color/yellow</item>
<item>#color/yellow_dark</item>
</integer-array>
</resources>
Create a drawable at
res/drawable/bg_yellow_round.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#color/yellow" />
<stroke
android:width="2dp"
android:color="#color/yellow" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
Create another drawable, you want for transition at same place and name it
res/drawable/bg_yellow_dark_round.xml
<solid android:color="#color/yellow_dark" />
<stroke
android:width="2dp"
android:color="#color/yellow_dark" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
Now create a color state list at
res/color/btn_selector_yellow.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="#android:integer/config_shortAnimTime">
<item android:drawable="#color/yellow" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="#drawable/bg_yellow_dark_round" android:state_pressed="true"/>
<item android:drawable="#drawable/bg_yellow_round"/>
</selector>
Now set it to your button as following
<Button
android:id="#+id/button1"
android:layout_width="248dp"
android:layout_height="44dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp"
android:background="#color/btn_selector_yellow"
android:text="AZ_ is so cool" />
Now this will do transition from
to
.
shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/star_off"/>
<corners android:radius="5dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:drawable="#color/tab_focused" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="#color/tab_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/shape"/>
</selector>
You can also create a shape that is using a selector inside. If your shape is just changing its color in different states, this is a lot cleaner.
color/color_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/blue_dark" android:state_pressed="true" />
<item android:color="#color/blue_light" />
</selector>
drawable/shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/color_selector" />
<corners android:bottomLeftRadius="6dip" android:bottomRightRadius="6dp" />
<padding android:bottom="0dip" android:left="0dip" android:right="0dip" android:top="0dip" />
</shape>
Well I know it's way too late
But here is a solved example
<TextView
android:id="#+id/txt_out_going_calls"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="04dp"
android:layout_weight="1"
android:background="#drawable/header_text_view_selector"
android:gravity="center"
android:text="#string/outgoing_calls_tab_button_text"
android:textColor="#color/home_text_color" />
and my header_text_view_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/home_fragment_tab_color_selected"/>
<corners android:radius="25dip" />
<padding android:bottom="08dip" android:left="9dip" android:right="9dip" android:top="08dip" />
</shape>
</item>
<item android:state_selected="false">
<shape>
<solid android:color="#color/home_fragment_tab_color_simple"/>
<corners android:radius="25dip" />
<padding android:bottom="08dip" android:left="9dip" android:right="9dip" android:top="08dip" />
</shape>
</item>
</selector>
So basically i m creating a rounded textview with selector. Here i m handling only state_selected and not_selected. Hope it helps
This is my way, and it works!
<item android:state_pressed="true">
<shape android:shape="oval">
<gradient android:centerX=".6"
android:centerY=".40"
android:endColor="#color/colorPrimary"
android:gradientRadius="20"
android:startColor="#color/colorPrimary"
android:type="radial" />
<stroke android:width="1dp"
android:color="#FFFFFF" />
<size android:width="55dp"
android:height="55dp" />
</shape>
</item>
<item android:state_focused="false">
<shape android:shape="oval">
<gradient android:centerX=".6"
android:centerY=".40"
android:endColor="#android:color/transparent"
android:gradientRadius="20"
android:startColor="#android:color/transparent"
android:type="radial" />
<stroke android:width="1dp"
android:color="#FFFFFF" />
<size android:width="55dp"
android:height="55dp" />
</shape>
</item>
My example is a circle button with state_pressed.
code bellow:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="#color/light_primary_color" />
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#color/accent_color" />
</shape>
</item>
</selector>
Use your shape name as you use any image and use it selector as you use image. Try out you will not face any problem. Is that what you were asking?
To be more reusable, you can set states on single properties. Avoid duplicating your shapes
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item>
<shape android:shape="rectangle" >
<corners android:radius="5dp"/>
<solid
android:state_enabled="false"
android:color="#color/transparent"
/>
<solid
android:state_enabled="true"
android:color="#color/background"
/>
<stroke
android:width="#dimen/dividerHeight"
android:color="#color/dividerLight"
/>
</shape>
</item>
</selector>
I was able to set background programmatically once disabled using this method.