How to add ripple effect with gradient to button? - android

I created a button and I want to add ripple effect and gradient to that button!
Here is my Button code:
<Button
android:id="#+id/percentageButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/gradient"
android:gravity="center"
android:text="%"
android:textColor="#android:color/white"
android:textSize="30dp">
</Button>
Here is my gradient.xml file code
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="0dp"/>
<gradient
android:gradientRadius="100"
android:centerX="49%"
android:centerY="50%"
android:centerColor="#434343"
android:startColor="#0F0F0F"
android:endColor="#141414"
android:type="radial"/>
</shape>
I have tried all the possibilities but not getting how to achieve this.

To get the ripple effect when you press a button, simply change your button xml to include:
android:foreground="?attr/selectableItemBackground"
and to have a basic gradient on your button you can have:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="-90"
android:startColor="COLORHERE"
android:endColor="COLORHERE"
android:type="linear" />
</shape>

android:foreground didn't work for me in Lollipop and here are my xml drawable for the button.
Add button_background.xml to your drawable.
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorPrimaryDark">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimaryDark" />
<corners android:radius="#dimen/button_radius_large" />
</shape>
</item>
<item android:id="#android:id/background">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#color/colorPrimaryLight"
android:startColor="#color/colorPrimary"
android:type="linear" />
<corners android:radius="#dimen/button_radius_large" />
</shape>
</item>
</ripple>
Add this to the background of your button.
<Button
...
android:background="#drawable/button_background" />
Note :
Attribute android:foreground has no effect on API levels lower than 23

Related

how to set border button with 2 colors

i'm trying to create a border for a Button with two color white and black. So I decide to create a shape. i trying to add gradient with white and black color my code doesn't work
i want border like this :
my xml :
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<gradient
android:angle="360"
android:centerColor="#000000"
android:endColor="#bfbfbf"
android:gradientRadius="360"
android:startColor="#ffffff"
android:type="sweep" />
<stroke
android:width="2dp"
android:color="#ffffff" />
</shape>
</item>
<item
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp">
<shape android:shape="rectangle" >
<solid android:color="#d2d2d2" />
</shape>
</item>
</layer-list>
and buttn :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:background="#drawable/border"
android:layout_alignBottom="#+id/progress"
android:layout_centerHorizontal="true"
android:id="#+id/okbtn" />
button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke
android:width="#dimen/stroke_width_outer"
android:color="#000" />
</shape>
</item>
<item
android:bottom="#dimen/stroke_width_outer"
android:right="#dimen/stroke_width_outer">
<shape android:shape="rectangle">
<stroke
android:width="#dimen/stroke_width_outer"
android:color="#FFF" />
</shape>
</item>
<item
android:bottom="#dimen/stroke_width_outer"
android:left="#dimen/stroke_width_outer"
android:right="#dimen/stroke_width_outer"
android:top="#dimen/stroke_width_outer">
<shape android:shape="rectangle">
<stroke
android:width="#dimen/stroke_width_outer"
android:color="#7F7F7F" />
</shape>
</item>
<item
android:bottom="#dimen/stroke_width_inner"
android:left="#dimen/stroke_width_outer"
android:right="#dimen/stroke_width_inner"
android:top="#dimen/stroke_width_outer">
<shape android:shape="rectangle">
<stroke
android:width="#dimen/stroke_width_outer"
android:color="#DFDFDF" />
</shape>
</item>
<item
android:bottom="#dimen/stroke_width_inner"
android:left="#dimen/stroke_width_inner"
android:right="#dimen/stroke_width_inner"
android:top="#dimen/stroke_width_inner">
<shape android:shape="rectangle">
<solid android:color="#BFBFBF" />
</shape>
</item>
</layer-list>
values/dimen.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="stroke_width_outer">3dp</dimen>
<dimen name="stroke_width_inner">6dp</dimen>
</resources>
stroke_width_inner should be twice of stroke_width_outer always
Apply background to button -
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_background"
android:text="OK"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
On Layout Editor -
On Device -
On Device you won't see the grayish left and top border as you see on Layout Editor
This appears to be a good place to use a 9-patch image. Any solution based on a regular .png file, a <shape>, or a <vector> will wind up scaling the white and black borders of your button.
Once you've created your button.9.png file, then you can set it to be your button's background using android:background="#drawable/button".
Edit
Here's a 9-patch based on your linked .png file:
Please look here about creating a shape drawable http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
Once you have done this, in the XML for your button set android:background="#drawable/your_button_border"

Android: selectableItemBackground not working on button with xml drawable

I made a rounded button with xml drawable and applied android:foreground="?attr/selectableItemBackground" in hope of getting a ripple effect. However, ripple effect does not work. Is there anyway to make the ripple effect work? As a workaround I wrapped the button with a FrameLayout and applied android:foreground="?attr/selectableItemBackground" to it, but in that case the ripple is rectangular and goes beyond button (as it is rounded).
xml:
<Button
android:id="#+id/reg_next"
android:layout_width="125dp"
android:layout_height="40dp"
android:text="#string/next"
android:layout_gravity="center"
android:textAllCaps="false"
android:textColor="#color/colorPrimary"
android:textSize="18sp"
android:background="#drawable/rounded_button_background"
android:foreground="?attr/selectableItemBackground"/>
rounded_button_background:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#ebeff1"/> <!-- this one is ths color of the Rounded Button -->
<stroke android:width="1px" android:color="#979797" />
<corners
android:radius="20dp"
android:bottomLeftRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>
</shape>
You need to create ripple drawable and set it as button background (without foreground attribute):
rounded_button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#2793c9">
<item>
<shape
android:padding="10dp"
android:shape="rectangle">
<solid android:color="#ebeff1" /> <!-- this one is ths color of the Rounded Button -->
<stroke
android:width="1px"
android:color="#979797" />
<corners
android:bottomLeftRadius="20dp"
android:radius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
</item>
</ripple>
If you want support api bellow 21 (without ripple effect), add this drawable to directory drawable-v21 and in directory drawable create these files:
rounded_btn_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="#ebeff1" /> <!-- this one is ths color of the Rounded Button -->
<stroke
android:width="1px"
android:color="#979797" />
<corners
android:bottomLeftRadius="20dp"
android:radius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
rounded_btn_active.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="#2793c9" /> <!-- this one is ths color of the Rounded Button -->
<stroke
android:width="1px"
android:color="#979797" />
<corners
android:bottomLeftRadius="20dp"
android:radius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
rounded_button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/rounded_btn_active" android:state_enabled="true" android:state_pressed="true" />
<item android:drawable="#drawable/rounded_btn_normal" android:state_enabled="true" />
</selector>

Android Custom Button Ripple/pulse Effect

I have created a custom circular button using the following :
<Button
android:id ="#+id/StartButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:background="#drawable/button_bg_round"
android:padding="15dp"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
Here button_bg_round is defined as below :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:color="#ffffff" android:width="5dp" />
<solid android:color="#3aeaee"/>
<size android:width="200dp" android:height="200dp"/>
</shape>
</item>
</selector>
Now, I want to add a circular ripple/pulse effect/animation when the button is clicked. I tried to look for a possible solution on google and SO but couldn't find any related to such a button. Any help would be much appreciated.
You can make you drawable like this:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#color/colorAccent"
tools:targetApi="lollipop"> <!-- ripple effect color -->
<item android:id="#android:id/background">
<shape android:shape="oval">
<stroke android:color="#ffffff" android:width="5dp" />
<solid android:color="#3aeaee"/>
<size android:width="200dp" android:height="200dp"/>
</shape>
</item>
</ripple>
where android:color inside ripple is the color of the effect.

Android rectangle shape with two different color

how can I create rectangle shape by using two different colors with shadow? like above image.
Please create a drawable file and put the below code in it.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<size android:height="20dp" />
<solid android:color="#F66D08" />
</shape>
</item>
<item android:top="50dp">
<shape android:shape="rectangle" >
<gradient android:endColor="#AD1B1D"
android:startColor="#E2360A"
android:angle="270" />
</shape>
</item>
</layer-list>
layer-list can be used to solve this
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<size
android:width="40dp"
android:height="40dp" />
<solid android:color="#F86F05" />
</shape>
</item>
<item android:top="10dp">
<shape android:shape="rectangle">
<size
android:width="30dp"
android:height="30dp" />
<solid android:color="#B31F19" />
</shape>
</item>
</layer-list>
And the screenshot of the same.
If you want gradient instead of solid color, change solid to gradient and set startColor, endColor and angle.
Place these code in your activity_main :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:gravity="center"
android:background="#F66D08"
android:layout_width="match_parent"
android:layout_height="20dp">
</LinearLayout>
<LinearLayout
android:background="#drawable/introslidergradiant"
android:layout_width="match_parent"
android:layout_height="100dp"></LinearLayout>
</LinearLayout>
And create drawble res file with introslidergradiant.xml name :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#A31720"
android:endColor="#E1350B"
android:type="linear" />
</shape>
</item>
</selector>
You have to make a xml file in the drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:type="linear"
android:startColor="#FFFF820D"
android:endColor="#FFA32815"
android:angle="90"/>
</shape>
use the following code or you can generate your own gradient here just set the color and select the android option and it will generate the gradient for you .

Android border bottom for custom view not working

What I'm trying to do is to have a relativelayout with a gradient on it and a gray bottom border, I'm using the below custom view in my application:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/list_item_image"
android:layout_width="match_parent"
android:layout_height="#dimen/SIZE_LIST_ITEM_LARGE"
android:scaleType="centerCrop"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/shape_list_item_gradient" />
<com.rahil.widgets.CustomTextView
android:id="#+id/list_item_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:gravity="bottom"
android:textColor="#color/COLOR_BLACK"/>
</RelativeLayout>
and this is shape_list_item_gradient:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#FFFFFFFF"
android:endColor="#00FFFFFF"
android:angle="90" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/COLOR_GRAY" />
</shape>
</item>
</layer-list>
but the above code applies border to all borders not just bottom border. I added the separate shape xml to the relativelayout but it didn't work which I assume the view with gradient covers it maybe?? I'm quiet lost. thanks for your help.
The android:bottom attribute is actually the "bottom offset in pixels" as noted here about layer-list drawables. There is no explicit way to create a rectangle that is stroked only on one side.
However you can use the same trick that Krylez suggested here: https://stackoverflow.com/a/19239478/1538674
So something like this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#FFFFFFFF"
android:endColor="#00FFFFFF"
android:angle="90" />
</shape>
</item>
<item android:top="-3dp" android:right="-3dp" android:left="-3dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke android:width="2dp" android:color="#color/COLOR_GRAY" />
</shape>
</item>
</layer-list>

Categories

Resources