How to Create Borderless Button in Android

If you are really on this page I understand you are looking for information on how to create a borderless button in android.

Initially, I was thinking to just show you only one solution to achieve a borderless button in android but then I decided to highlight other means.

I have also add a snippet on how to create borderless colored button in android should in case you want to add solid color to borderless button.

Please note that depending on your requirement some might not actually work the way you anticipated.

The default android button comes with a border when it is rendered. To remove the border and make it borderless we can apply the solutions below.

Use Shape Drawable

Create a rectangle shape drawable inside your project drawable folder. Set the shape drawable to the background property of android button.

btn_borderless.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
</shape>

activity_borderless_button.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".button.BorderlessColorButtonActivity">

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_borderless"
        android:text="@string/borderless_button"
        android:padding="12dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Assign android:attr/borderlessButtonStyle to button style

style="?android:attr/borderlessButtonStyle"

Adding it to button widget like this

<Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/borderless_button"
        android:padding="12dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Assign ?android:attr/selectableItemBackground to button background

<Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/borderless_button"
        android:background="?android:attr/selectableItemBackground"
        android:padding="12dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Assign @android:color/transparent to button background

<Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/borderless_button"
        android:background="@android:color/transparent"
        android:padding="12dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Finally, you have to decide which one works best for you.

android borderless button

 

Add a Comment