How do I center the image in the FloatingActionButton behind a transparent background?
I have a FloatingActionButton and I would like to make it transparent with an icon in the center.
I added the style:
<style name="ButtonTransparent">
<item name="colorAccent">@android:color/transparent</item>
</style>
which still works. FAB became transparent. Then I added the FAB:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activities.CameraActivity">
<TextureView
android:id="@+id/texture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:id="@+id/iv_last_image"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/btn_takepicture"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal"
android:layout_margin="20dp"
android:theme="@style/ButtonTransparent"
android:src="@drawable/selector_vector_camera_light" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/btn_back"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:layout_margin="20dp"
android:theme="@style/ButtonTransparent"
android:src="@drawable/selector_vector_go_back" />
</LinearLayout>
</RelativeLayout>
but as you can see in the screenshot below, it is not well aligned. How can I fix this?
source to share
Use this attribute: fab:fabCustomSize="100dp"
to make it equal the height and width of your fab. It helped me. This was my code.
<com.google.android.material.floatingactionbutton.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="60dp"
android:layout_height="60dp"
app:fabCustomSize="60dp"
android:backgroundTint="@color/colorPrimary"
android:src="@drawable/ic_insert_drive_file_24dp" />
Another way could be using the attribute android:foreground
instead of src and then setting android:foreground_gravity
to center.
source to share
Make sure the inverse image / drawing is square sized and this config layout worked for me
<android.support.design.widget.FloatingActionButton
android:id="@+id/btn_back"
android:layout_width="60dp"
android:layout_height="60dp"
app:backgroundTint="#0000"
android:scaleType="fitXY"
android:layout_gravity="center"
android:layout_margin="20dp"
android:src="@drawable/back_vector" />
if you use
app:backgroundTint="#200f"
The background color of the background color will appear as if the background will look much more transparent.
source to share
layout 2 files: for example content_main.xml file:
<RelativeLayout
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:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.testbd.com.myapplication.MainActivity"
tools:showIn="@layout/activity_main">
</RelativeLayout>
and
Activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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="@color/colorAccent"
android:fitsSystemWindows="true"
tools:context="com.testbd.com.myapplication.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:srcCompat="@android:drawable/ic_dialog_email"/>
</android.support.design.widget.CoordinatorLayout>
and finally res / values /colors.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#5a1f00</color>
</resources>
Good luck!
source to share
Try using below code. Hope this helps you. This code works fine with me.
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginRight="@dimen/scale_15dp"
android:layout_marginTop="@dimen/scale_130dp"
android:elevation="@dimen/scale_5dp"
android:src="@drawable/ic_directions"
app:backgroundTint="@android:color/transparent" />
source to share