Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set corner radiuses for the button in java code?

I want to set the rounded corners without xml. How can I do it in java code?

Button b = new Button (this);
b.set???? (??) ;

I tried to write b.setCornerRadius(3.0f), but it is undefined for button object. Thanks.

like image 405
tatiana_c Avatar asked Jan 03 '12 08:01

tatiana_c


2 Answers

Use GradientDrawable

GradientDrawable gdDefault = new GradientDrawable();
gdDefault.setColor(bgColor);
gdDefault.setCornerRadius(cornerRadius);
gdDefault.setStroke(strokeWidth, strokeColor);
like image 182
Ndroid Avatar answered Oct 18 '22 15:10

Ndroid


create a shape in your drawable folder and set the desired radius and set this drawable as background to your button:

<?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="5dip"/>
        </shape>
    </item>
</layer-list>
like image 34
Buda Gavril Avatar answered Oct 18 '22 15:10

Buda Gavril