Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button with rounded corners and border with color

I need your help with something im trying to do, I've been trying to make a button with rounded corners and showing just the border of it, I need to be able to change the color programmatically depending on what I get from a web service, so far I tried to add the shape with a drawable and it gave the rounded shape with the border color, but I don't if is possible to change the color of it because its added by default in the drawable

<?xml version="1.0" encoding="UTF-8"?>

<stroke android:width="3dp"
    android:color="#ff000000"
    />

<padding android:left="1dp"
    android:top="1dp"
    android:right="1dp"
    android:bottom="1dp"
    />

<corners android:bottomRightRadius="7dp"
    android:bottomLeftRadius="7dp"
    android:topLeftRadius="7dp"
    android:topRightRadius="7dp"/>

that is the drawable I was using, then I tried to add the shape creating a custom class for the button and changing the onDraw method, and im getting a shape but is kinda weird

Rounded shape

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(strokeColor);
    paint.setStrokeWidth(5.0f);

    int  h = this.getHeight();
    int  w = this.getWidth();
    //final RectF rect = new RectF();

    RectF oval1 = new RectF(0, 0, w, h);
    canvas.drawRoundRect(oval1, 40, 40, paint);

}

and for some reason besides the weird shape im adding the text programmatically with set text method and its not showing, it gets the color for the stroke but not the text

buttonCTA = ButterKnife.findById(this, R.id.btnCTA);
        buttonCTA.setTextColor(Color.parseColor(valueColor));
        buttonCTA.setStrokeColor(valueColor);
        buttonCTA.setText("test");
like image 320
Diego Andres Vierma Chata Avatar asked Jan 12 '17 13:01

Diego Andres Vierma Chata


People also ask

How to make the buttons on the app rounded corners?

The app has a panel with buttons. Task: All buttons should have rounded corners. It is desirable that this is done via the UIManager. Also, it is worth considering that the buttons are set to the background color and the color when the button is pressed. I tried to do this, but then the background color goes out of the corners:

How to round buttons with CSS?

How TO - Round Buttons Step 1) Add HTML: Example <button class="button button1"> 2px </button> <button class="button button2"> 4px... Step 2) Add CSS: Add rounded corners to a button with the border-radius property: Example .button { background-color:... W3.CSS Tutorial

How to add a colored border to a button with CSS?

How to add a colored border to a button with CSS? To add colored border, use the CSS border property. You can try to run the following code to add a colored border.

How do I add rounded corners to elements in CSS?

The CSS border-radius property defines the radius of an element's corners. Tip: This property allows you to add rounded corners to elements! Here are three examples: 1. Rounded corners for an element with a specified background color: Rounded corners!


2 Answers

create round_background.xml in drawable folder.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="3dp"
        android:color="#ff000000" />

    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />

    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />
</shape>

set as background

 <Button
        android:id="@+id/mybutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/round_background"
        android:text="Hello World!" />

Change it at runtime with any view.

 Button button = (Button) findViewById(R.id.mybutton);
 GradientDrawable drawable = (GradientDrawable)button.getBackground();
 drawable.setStroke(2, Color.YELLOW);
like image 187
Sohail Zahid Avatar answered Nov 01 '22 22:11

Sohail Zahid


This one is what you need.

    public static void setRoundedDrawable(Context context, View view, int backgroundColor, int borderColor) {
    GradientDrawable shape = new GradientDrawable();
    shape.setShape(GradientDrawable.RECTANGLE);
    shape.setCornerRadius(20f);
    shape.setColor(backgroundColor);
    if (borderColor != 0){
        shape.setStroke(2f, borderColor);
    }
    view.setBackgroundDrawable(shape);
}

You can change corner radius and stroke width depends on what you need. Hope it helps!

like image 32
Roman Pozdnyakov Avatar answered Nov 01 '22 23:11

Roman Pozdnyakov