user4933560
user4933560

Reputation:

how to create or make or set custom background in floating button android

I have created custom floating button and use all library but not created custom floating button i have very tried to make to this button if you any idea how to make custom floating button.
Plz help me

My button look like this:

Floating Button image

My code

<Button
    android:id="@+id/btn_build_now"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_weight="0"
    android:background="@color/Background_Main"
    android:drawableBottom="@drawable/button_bg"
    android:paddingBottom="10dp" />

Upvotes: 1

Views: 7227

Answers (2)

Mori
Mori

Reputation: 4631

You can use the new version of FAB, Extended FAB in XML

The old one

<com.google.android.material.floatingactionbutton.FloatingActionButton/>

The newest:

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton/>

then you can define Icon, Text and Shape:

android:text="Hi" 
app:icon="@android:drawable/ic_media_play"
app:shapeAppearance="@style/ShapeAppearanceOverlay.Material3.FloatingActionButton"
app:shapeAppearanceOverlay="null"

Upvotes: 0

Jordan
Jordan

Reputation: 733

You could simply use Android support library to create a Floating Action Button. For example here is a floating action button example with custom background color, pressed and focused states.

XML

<android.support.design.widget.FloatingActionButton xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/fb"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:src="@drawable/fb_icon_play"
                    app:backgroundTint="@drawable/fb_play_bg" />

fb_icon_play.xml in Drawable Folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/floating_button_blue_play_icon_pressed" />
    <item android:state_focused="true" android:drawable="@drawable/floating_button_yellow_play_icon_pressed" />

    <item android:drawable="@drawable/floating_button_yellow_play_icon_pressed" />
</selector>

fb_play_bg.xml in Drawable Folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#3389b3"/>
<item android:state_focused="true" android:state_pressed="true" android:color="#3389b3" />
<item android:state_focused="false" android:state_pressed="true" android:color="#d1930f" />
<item android:color="#3389b3" />
</selector>

Here is another tutorial for creating a custom FloatingActionButton.

Upvotes: 4

Related Questions