Search code examples
androidmaterial-designandroid-buttonmaterial-componentsmaterial-components-android

How to fix programmatically round corners buttons on android?


I am going to want to rounded corners on button like this below code

        button.setWidth(buttonWidth);
        button.setHeight(buttonHeight);

How to define rounded buttons with java code, no xml


Solution

  • Use the Material Components for Android.

    Just add the dependency:

    implementation ‘com.google.android.material:material:1.0.0’
    

    Add the MaterialButton in your layout:

    <com.google.android.material.button.MaterialButton
       ....
       style="@style/Widget.MaterialComponents.Button.OutlinedButton"
       app:cornerRadius=".."
       app:strokeColor="@color/colorPrimary"/>
    

    and use the method setCornerRadius.
    Something like:

    button.setCornerRadius(..);