Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a square button (90 degree angles) Android Studio

So while fiddling around in Android Studio I came with the question: 'How do you make the corners of the button square?. Looking around the internet and stack overflow I got many answers, but I wondered if there was a much simpler way... and then poof! Under Common attributes in XML design, I saw background, curious I put in '@android:color/white' inside, and then the rounded square button became a 90-degree square button.

This was a nice surprise. Since I didn't see anyone talk about doing this I decided to post here. Hope it was helpful and a quick hack to having 90-degree corners on your buttons!

enter image description here

Code difference Back(with background) and home(no background)

<Button
    android:id="@+id/back"
    android:background="@color/white"
    android:text="Back"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.008" />

<Button
    android:id="@+id/home"
    android:text="Home"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.007" />

enter image description here

like image 719
RainbowUnicorn Avatar asked Oct 31 '25 21:10

RainbowUnicorn


1 Answers

To achieve a square button just use the attribute app:cornerRadius="0dp":

    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BUTTON"
        app:cornerRadius="0dp"/>

Just note that if you are using a Material Components Theme the Button is replaced at runtime by a MaterialButton.

enter image description here

Note about the android:background="@color/white". Using the android:background attribute the MaterialButton doesn't use its own MaterialShapeDrawable for the background. This means that features like shape appearance, stroke and rounded corners are ignored.
It is the reason because you are obtaining a square button.

like image 143
Gabriele Mariotti Avatar answered Nov 02 '25 10:11

Gabriele Mariotti