How to create a custom shape for a Button? Can I set different colors to button background colors for many conditions.Any help will be appreciated.Thanks.
Asked
Active
Viewed 916 times
1

W4R10CK
- 5,502
- 2
- 19
- 30

Htoo Aung Hlaing
- 2,173
- 15
- 26
-
1You can use this png as background of button and can use tint property to change color of background image – Kamran Ahmed Khan Feb 01 '17 at 04:32
-
3make nine patch image for this shape – Vishal Thakkar Feb 01 '17 at 04:33
-
2look here: http://stackoverflow.com/a/26162158/5241603 – K.Sopheak Feb 01 '17 at 04:35
-
@ Kamran Ahmed Khan, How can I use for tint method for to change background image – Htoo Aung Hlaing Feb 01 '17 at 05:08
-
@HtooAungHlaing, check the answer, I posted man – W4R10CK Feb 01 '17 at 05:14
-
Check this http://stackoverflow.com/a/32291407/5239819 – Kamran Ahmed Khan Feb 01 '17 at 10:32
2 Answers
3
How to create a custom shape for a Button?
Create a test.xml
file in drawable and add code:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/one"> //<---- id is must for color change
<shape android:shape="rectangle">
<size
android:width="150dp"
android:height="40dp" />
<solid android:color="@color/colorPrimary" />
<corners android:radius="10dp"/> //<---- remove radius if not needed
</shape>
</item>
<item
android:top="10dp"
android:bottom="-12dp"
android:left="-55dp"
android:width="75dp">
<rotate
android:fromDegrees="-47">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</rotate>
</item>
</layer-list>
In button:
<Button
.....
android:background="@drawable/test"
...../>
Output
Can I set different colors to button background colors for many conditions?
Yes, you can change at run-time. For that you have to assign id to each item to know where you want to make changes. As I did on my answer code.
LayerDrawable layerDrawable = (LayerDrawable) ContextCompat.getDrawable(Your_Activity.this, R.drawable.your_shape_test);
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.id_of_drawable-one);
gradientDrawable.setColor(your_color);

W4R10CK
- 5,502
- 2
- 19
- 30
-
it seems to be work in DesignView, but when i really run on device, it cannot get the real result – Htoo Aung Hlaing Feb 01 '17 at 05:49
-
-
-
@HtooAungHlaing, I'm not sure why its not working for you. I have added same layout on my app also. See the output image from real device. – W4R10CK Feb 01 '17 at 06:04
1
Try this code:
public class ButtonBackGroundDrawable extends Drawable {
private Paint paint;
public ButtonBackGroundDrawable() {
paint = new Paint();
paint.setAntiAlias(true);
}
@Override
public void draw(Canvas canvas) {
int height = getBounds().height();
int width = getBounds().width();
RectF rect = new RectF(0.0f, 0.0f, width, height);
Point point0_draw = new Point(0, 0);
Point point1_draw = new Point(width, 0);
Point point2_draw = new Point(width, height);
Point point3_draw = new Point(0, height);
Point point4_draw = new Point(width / 10, height / 2);
Path path = new Path();
path.moveTo(point0_draw.x, point0_draw.y);
path.lineTo(point1_draw.x, point1_draw.y);
path.lineTo(point2_draw.x, point2_draw.y);
path.lineTo(point3_draw.x, point3_draw.y);
path.lineTo(point4_draw.x, point4_draw.y);
path.lineTo(point0_draw.x, point0_draw.y);
path.close();
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.CYAN);
// paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawPath(path, paint);
}
@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
paint.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}

W4R10CK
- 5,502
- 2
- 19
- 30

noobEinstien
- 3,147
- 4
- 24
- 42
-
Thanks for your reply. If you want to change the width change w/10 to your value. and accept my answer if you saved time. – noobEinstien Feb 01 '17 at 09:26
-
yes, width is OK, add setColor method for custom color set and already accepted that one is mine ,Thanks – Htoo Aung Hlaing Feb 01 '17 at 09:35