how to create custom button using gradient effects? - android

how to create such
type of button baground using button selector ?
i add button selector code hare please help me ..
<item android:state_enabled="false">
<shape>
<gradient
android:startColor="#B2D570"
android:endColor="#79B903"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#4C5E2E" />
<corners
android:radius="2dp" />
<padding
android:left="5dp"
android:top="4dp"
android:right="5dp"
android:bottom="4dp" />
</shape>
</item>

this is the code for selector: this will be useful for u, name this file and put this in xml drawable folder,and set the background of ur button with this filename....
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/focused" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/focusedpressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/pressed" />
<item android:drawable="#drawable/defaultbutton" />
</selector>

Related

Shape And Selector is not working

In my android application want my button to be green and rounded rectangle and when someone press the button it should change color to grey. So I have created an XML file with selector and give it to shape of rounded rectangle but problem is when I start my application the color is not green by default its transparent. Moreover when I press the button it shows rectangle shape so I think shapre is also not working. This is my Primary color:
<color name="colorPrimary">#669900</color>
My custom_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<item android:state_focused="true" android:state_pressed="false" android:color="#color/colorPrimary" android:drawable="#color/colorPrimary">
<shape
android:shape="rectangle" android:padding="10dp">
<solid android:color="#color/colorPrimary"/>
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
</item>
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/gradient" >
<shape
android:shape="rectangle" android:padding="10dp">
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
</item>
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/gradient" >
<shape
android:shape="rectangle" android:padding="10dp">
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
</item>
</selector>
Also I have made a XML file for gradient which is gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<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>
To get your rectangles to be rounded, try something like the following code:
<?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="5dp"/>
<stroke
android:width="2dp"
android:color="#color/border_of_rectangle"/>
<solid
android:color="#color/color_inside_rectangle"/>
</shape>
</item>
</layer-list>
I've made something similar before here is how i did it
button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_default" android:state_pressed="false" android:state_focused="false"/>
<item android:drawable="#drawable/button_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/button_pressed" android:state_focused="true" />
</selector>
button_default.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="#dimen/radius" />
<solid
android:color="?attr/colorAccent" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"
/>
</shape>
button_pressed.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="#dimen/radius" />
<solid
android:color="?attr/colorAccent" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"
/>
</shape>
values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:buttonStyle">#style/BlueButton</item>
</style>
<style name="BlueButton" parent="android:style/Widget.Button">
<item name="android:background">#drawable/button</item>
<item name="android:textColor">#drawable/button_text_color</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">18sp</item>
</style>
</resources>
I'm also guessing that you might need to change the text color so here is the file
button_text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimaryDark" android:state_pressed="false" android:state_focused="false"/>
<item android:color="?attr/colorAccent" android:state_pressed="true"/>
<item android:color="?attr/colorAccent" android:state_focused="true" />
</selector>

Add border to button using xml in Android

I have an XML file called playnext. I added this file as a background to my button.XML include two images that is for button press and button release.I need to add border to these two images using the XML.
XML code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="#drawable/ic_remote_playnext" ></item>
<item android:state_pressed="true" android:drawable="#drawable/next_pressdown" ></item>
</selector>
Please tell me how to add a border to this? Is there is any other way to add borders to button?
make a drawable xml as selector
<?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="yoursolidcolor for state pressed" />
<stroke
android:width="1dp"
android:color="#171717" />
<corners
android:radius="3dp" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
</shape>
</item>
<item>
<shape>
<solid
android:color="#yoursolidcolor for default" />
<stroke
android:width="0dp"
android:color="#171717" />
<corners
android:radius="4dp" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
</shape>
</item>
</selector>
Then
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/youraboveselectoe"
android:textColor="#color/white"
android:text="Button"/>
you can also check on this
Is it possible to specify border in android button?
Please add the following code in drawable xml and call the this xml like using
android:background="#drawable/border"
border.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFad eDuration="#integer/short_anim_time">
<item android:drawable="#color/orange" android:state_pressed="true" />
<item android:drawable="#color/white" android:state_focused="true" />
<item android:drawable="#color/white" />
<item>
<shape>
<stroke android:width="2dp" android:color="#color/orange" />
<padding android:bottom="#dimen/gap" android:left="#dimen/gap" android:right="#dimen/gap" android:top="#dimen/gap" />
</shape>
</item>

How to add a selector to a View's border

I have an edittextview with a black colored border. How can I change this border color depending on the state, using a selector.
I tried this:
<EditText
android:layout_width="fill_parent"
android:layout_height="48dp"
android:background="#drawable/black_rounded_borders"
/>
black_rounded_borders.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFFFFFFF" />
<stroke
android:width="2dp"
android:color="#drawable/selector_black_border" />
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
</shape>
selector_black_border.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#FF269ff5"/>
<item android:state_focused="true" android:color="#FF269ff5"/>
<item android:color="#FF000000"/>
</selector>
EDIT:
I changed selector_black_border.xml to color folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#FF269ff5"/>
<item android:state_pressed="true" android:color="#FF269ff5"/>
<item android:state_selected="true" android:color="#FF269ff5"/>
<item android:state_focused="false" android:color="#FF000000"/>
<item android:state_pressed="false" android:color="#FF000000"/>
<item android:state_selected="false" android:color="#FF000000"/>
</selector>
<stroke
android:width="2dp"
android:color="#color/selector_black_border" />
But then it always remain in blue color("#FF269ff5") even when not focused/pressed.
Thank You.
you have to put the selector inside color/ and
<stroke
android:width="2dp"
android:color="#color/nameofcolor">

Android button select and press drawable

I'm trying to make a button with state press and select,
I already did the same with tabs and it works but I don't know why here does not work.
I have done it like this:
button_sel.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#color/azulado"
android:endColor="#color/azulBrillante"
android:angle="270" />
<corners android:radius="#dimen/corner_radius" />
<stroke android:width="2px"
android:color="#color/blanco" />
</shape>
button_unsel.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#color/botonesD"
android:endColor="#color/botones"
android:angle="270" />
<corners android:radius="#dimen/corner_radius" />
<stroke android:width="2px"
android:color="#color/blanco" />
</shape>
And the selector, button.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_sel"
android:state_selected="true"
android:state_pressed="true"/>
<item android:drawable="#drawable/button_unsel"
android:state_selected="false"
android:state_pressed="false"/>
</selector>
And here I call the drawable as a background:
<style name="button">
<item name="android:background">#drawable/button</item>
<item name="android:textSize">#dimen/text_size</item>
<item name="android:padding">#dimen/padding_button</item>
<item name="android:textColor">#color/blanco</item>
</style>
Thank you!!!!
The first item in your selector is only used, when the button is pressed AND selected. If you want to use button_sel when your button is pressed OR selected, you should do something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_sel" android:state_selected="true" />
<item android:drawable="#drawable/button_sel" android:state_pressed="true" />
<item android:drawable="#drawable/button_unsel" />
</selector>
The items are evaluated from top to bottom, the last one is the default. Though I am not sure if state_selected makes sense for buttons.
Can use shape inline item.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape....>
</item>
<item android:state_selected="true" >
<shape....>
</item>
<item android:state_pressed="true" android:state_selected="true" >
<shape...>
</item>
</selector>
For sample :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" >
<shape
android:shape="rectangle">
<gradient android:startColor="#color/md_amber_300"
android:endColor="#color/md_amber_50"
android:angle="270" />
<corners android:radius="#dimen/fab_margin" />
<stroke android:width="2px"
android:color="#color/primaryColorDark_orange" />
</shape>
</item>
<item android:state_pressed="true" >
<shape
android:shape="rectangle">
<gradient android:startColor="#color/md_amber_300"
android:endColor="#color/md_amber_50"
android:angle="270" />
<corners android:radius="#dimen/fab_margin" />
<stroke android:width="2px"
android:color="#color/primaryColorDark_orange" />
</shape>
</item>
<item android:state_pressed="true" android:state_selected="true" >
<shape
android:shape="rectangle">
<gradient android:startColor="#color/md_teal_500"
android:endColor="#color/md_blue_400"
android:angle="270" />
<corners android:radius="#dimen/fab_margin" />
<stroke android:width="2px"
android:color="#color/md_amber_A400" />
</shape>
</item>
</selector>

I need to show a button press on a shape which is my background for my button

This is my shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#80000000"
android:endColor="#80FFFFFF"
android:angle="45"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="10dp" />
</shape>
This is what I want for button pressed
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#80FFFFFF"
android:endColor="#80000000"
android:angle="45"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="10dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
I tried this code to link to the two shapes but when I set it as background it just made my button default gray.
and I added the selector to the background of my button, it has the right opacity as is but I need it to show a different colour (like white or something) when it is clicked.
anyone any idea?
I do it in this way:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/btn_bg_pressed"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/btn_bg_pressed"/>
<item android:state_focused="true" android:drawable="#drawable/btn_bg_selected"/>
<item android:state_focused="false" android:state_pressed="false" android:drawable="#android:color/transparent"/>
</selector>
This is what i usually use for my theme

Categories

Resources