Last active
June 1, 2018 05:41
-
-
Save creativepsyco/8c2d05803a5193c187ef to your computer and use it in GitHub Desktop.
Custom Button Widget which applies a ripple onto buttons above API > 21 and normal state list selectors for API < 21. Also you can set the corner radius.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mohitkanwal.framework.helper; | |
import android.annotation.SuppressLint; | |
import android.graphics.*; | |
import android.graphics.drawable.Drawable; | |
import android.graphics.drawable.ShapeDrawable; | |
import android.graphics.drawable.shapes.RectShape; | |
import android.support.annotation.ColorInt; | |
import android.view.View; | |
import android.view.Window; | |
import android.widget.AbsListView; | |
import android.widget.ImageView; | |
import java.util.Locale; | |
/** | |
* A utility class for UI related helper functions. | |
* User: mohit | |
* Date: 18/12/14 | |
*/ | |
public final class UIUtils { | |
private UIUtils() { | |
} | |
/** | |
* Darkens or lightens a color by a given fraction. | |
* | |
* @param color The color to be darkened or lightened. | |
* @param fraction A value below 1.0 to darken, a value above 1.0 to lighten. | |
* @return The darkened or lightened color. | |
*/ | |
public static int darkenColor(final int color, final float fraction) { | |
final int red = Math.round(Math.min(Color.red(color) * fraction, 255)); | |
final int green = Math.round(Math.min(Color.green(color) * fraction, 255)); | |
final int blue = Math.round(Math.min(Color.blue(color) * fraction, 255)); | |
final int alpha = Color.alpha(color); | |
return Color.argb(alpha, red, green, blue); | |
} | |
/** | |
* Makes a rect shape with the given color and rounded corner radius. | |
* | |
* @param color The color of the shape. | |
* @param radius The rounded corner radius of the shape. | |
* @return The shape. | |
*/ | |
public static ShapeDrawable makeRectShapeWithColor(final @ColorInt int color, final float radius) { | |
final ShapeDrawable drawable = new ShapeDrawable(new RectShape()); | |
final Paint paint = drawable.getPaint(); | |
paint.setColor(color); | |
paint.setStyle(Paint.Style.FILL_AND_STROKE); | |
paint.setStrokeWidth(0); | |
if (radius > 0f) { | |
paint.setPathEffect(new CornerPathEffect(radius)); | |
} | |
paint.setDither(true); | |
paint.setAntiAlias(true); | |
return drawable; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<com.youapp.BaseButton | |
android:id="@+id/btn" | |
android:layout_width="match_parent" | |
android:layout_height="48dp" | |
android:layout_marginTop="24dp" | |
app:cornerRadius="2dp" | |
app:disabledStateColor="@color/lightGray" | |
app:normalStateColor="@color/colorPrimary"/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment