Android Stock Button Make It Solid - android

Hi Im trying to make the android stock button a solid but still contain the little animation when press to change colour. Any help would be awesome. This is what I have so far.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="0dp" android:color="#000000" />
<solid android:color="#ffffff"/>
<corners android:radius="1px"/>
<padding android:left="5dp" android:top="3dp" android:right="5dp" android:bottom="3dp" />
</shape>

For the slection and click you can use selector exemple :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/clr_main_green_pressed" android:state_selected="true" android:state_window_focused="false"/>
<item android:drawable="#color/clr_main_green_pressed" android:state_selected="true"/>
<item android:drawable="#color/clr_main_green_pressed" android:state_pressed="true" android:state_selected="false"/>
<item android:drawable="#android:color/white" android:state_selected="false"/>
</selector>
Some shape/color/ changes exemple :
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="12dp" />
<solid android:color="#android:color/white" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
</shape>
And use the two together exemple :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/rounded_border"/>
<item android:drawable="#drawable/selector_button"/>
</layer-list>

Related

Use drawable and <shape> in <item> XML

Is it possible to use a XML-Background ressource with an drawable and a <shape> attribute?
So I have this button
<Button
android:layout_marginRight="5dp"
android:id="#+id/send_button"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/send_button" />
Which has a background send_button.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_send_white_48dp" android:state_focused="true"/>
<item android:drawable="#drawable/ic_send_white_48dp" android:state_pressed="true"/>
<item android:drawable="#drawable/ic_send_black_48dp"/>
</selector>
This works fine right now. But I also want to add a background-color behind the drawable and rounded corners like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="4dp" />
<gradient
android:angle="270"
android:endColor="#88b823"
android:startColor="#b0dc54" />
</shape>
So is it possible to combine these two XML ressources?
What I've tried so far, just displays the drawable and not the shape:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_send_white_48dp" android:state_focused="true"/>
<item android:drawable="#drawable/ic_send_white_48dp" android:state_pressed="true"/>
<item android:drawable="#drawable/ic_send_black_48dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="4dp" />
<gradient android:angle="270"
android:endColor="#88b823"
android:startColor="#b0dc54" />
</shape>
</item>
</selector>
Thanks to #CommonsWare for pointing me in the right direction. This is the working code (currently only for the default state):
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_send_white_48dp" android:state_focused="true"/>
<item android:drawable="#drawable/ic_send_white_48dp" android:state_pressed="true"/>
<item>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="4dp" />
<solid android:color="#color/action_bar" />
</shape>
</item>
<item>
<bitmap android:src="#drawable/ic_send_black_48dp" />
</item>
</layer-list>
</item>
</selector>

Shape with selector as color doesn't works

I want to create a shape with selector as solid color.
On Android 5.0+, this code works perfectly, but on 4.X, it doesn't works.
shape.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/my_selector"/>
<size android:height="40dp" android:width="40dp"/>
<corners android:radius="10dp" />
</shape>
my_selector.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:color="#color/color_1"
android:state_checked="false"/>
<item
android:color="#color/color_2"
android:state_checked="true"/>
</selector>
This drawable is applied to CheckBox background.
you should change order to write state like this and use your shape in item:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:shape="rectangle" >
<solid android:color="#color/my_selector"/>
<size android:height="40dp" android:width="40dp"/>
<corners android:radius="10dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/my_deselec"/>
<size android:height="40dp" android:width="40dp"/>
<corners android:radius="10dp" />
</shape>
</item>
</selector>
Use in this manner
<?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>

Button with selector for background color - when clicked the background color is extending out of the button

To begin with.. the button is added dynamically through code and hence cannot apply the styles in xml.
I have few buttons in my activity and i am using a selector to change the background color. The button also has a "shape" attached to it, for border.
image_border.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#color/dark_grey" />
<stroke
android:width="4dp"
android:color="#color/light_grey" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
button_background_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/blue" android:state_pressed="true"/>
<item android:drawable="#color/dark_grey" android:state_focused="true"/>
<item android:drawable="#drawable/image_border"/>
</selector>
Now when i click on the button, the background color, changes just fine. But, the background color is extending beyond the button's size. I am not sure where it is happening.
Please refer to the images below...
Before clicking the button :
After clicking the button :
I think the color is extending into the padding or something, but i am really not pretty sure, why this is occuring.
This should be your selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/image_border_blue" android:state_pressed="true"/>
<item android:drawable="#drawable/image_border_dark_grey" android:state_focused="true"/>
<item android:drawable="#drawable/image_border"/>
</selector>
And just add these drawables:
image_border_blue
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#color/blue" />
<stroke
android:width="4dp"
android:color="#color/light_grey" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
image_border_dark_grey
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#color/dark_grey" />
<stroke
android:width="4dp"
android:color="#color/light_grey" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
Create another shape using those color you used in selector...suppose for Blue color...create shape named btn_pressed.xml...
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#color/blue" />
<stroke
android:width="4dp"
android:color="#color/blue" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
Then place this shape in place of #color/blue in the selector as follows...
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_pressed" android:state_pressed="true"/>
<item android:drawable="#color/dark_grey" android:state_focused="true"/>
<item android:drawable="#drawable/image_border"/>
</selector>
Follow same process for #color/dark_grey.

How do I create a selector with drawable and color

Here is the code I have that is presently not working. How do I get it to work? I don't want to use two files such as a shape file in addition to the selector file. Can I do this in one file?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/img1" android:state_selected="true"/>
<item android:color="#color/my_col"/>
</selector>
I also tried
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/img1" android:state_selected="true"/>
<item><shape android:shape="rectangle">
<solid android:color="#color/my_col" />
</shape></item>
</selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#3f68c2" />
<corners android:radius="20dp" />
</shape></item>
<item android:state_pressed="true"><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#3f68c2" />
<corners android:radius="20dp" />
</shape></item>
It turns out mine works. I was calling the wrong drawable into the layout file.

How to apply shape and selector simultaneously for Button?

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.

Categories

Resources