Home > Back-end >  How to make a FloatingActionButton with semi transparent background
How to make a FloatingActionButton with semi transparent background

Time:08-12

I want to have a FloatingActionButton with a semi transparent color.

However, when I set the backgroundTint attribute of the FloatingActionButton with a alpha color not equal to 0xFF, the color in the center of the FAB looks good but there is a weird hexagonal shape with a distinct color on the outer side.

Screenshot

My layout file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:background="#03A9F4"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="#FF03DAC5"
            android:src="@drawable/icon"
            app:fabCustomSize="90dp"
            app:maxImageSize="40dp"
            />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="#8003DAC5"
            android:src="@drawable/icon"
            app:fabCustomSize="90dp"
            app:maxImageSize="40dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#8003DAC5"
            android:gravity="center"
            android:text="#8003DAC5" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#03DAC5"
            android:gravity="center"
            android:text="#03DAC5" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

What can I to to fix that?

CodePudding user response:

You have to remove effects and stroke from your FloatingActionButton.

To do that, you can add following code to your view:

app:borderWidth="0dp"
app:elevation="0dp"
app:pressedTranslationZ="0dp"

So, if need to explain what has been changed;

  • elevation, will fix the issue on normal state of the view.
  • borderWidth will remove any border effects and I would recommend you to add it. Because on my tests, if I make a zoom I am able to see some strokes.
  • pressedTranslationZ, will remove the same effect when you press on the button.

So, you can add your layout as below:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:background="#03A9F4"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="#FF03DAC5"
            android:src="@drawable/icon"
            app:fabCustomSize="90dp"
            app:maxImageSize="40dp"
            />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="#8003DAC5"
            android:src="@drawable/icon"
            app:borderWidth="0dp"
            app:elevation="0dp"
            app:pressedTranslationZ="0dp"
            app:fabCustomSize="90dp"
            app:maxImageSize="40dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#8003DAC5"
            android:gravity="center"
            android:text="#8003DAC5" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#03DAC5"
            android:gravity="center"
            android:text="#03DAC5" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

Got the same issue here. I tried to set alpha transparency in xml using backgroundTint but it didn't work and resulted in the same appearance as in your screenshots (two circles).

So I set it in code like this :

floatingButton = (FloatingActionButton) findViewById(R.id.fab);
floatingButton.setAlpha(0.25f);
  • Related