Ali
Ali

Reputation: 9994

Set border color for FloatingActionButton in support library

In support library FloatingActionButton is introduced. There is an option to set border width for button with app:borderWidth="2dp", but how about setting border color?

I create a custom drawable for that porpuse, such as:

<item>
     <shape android:shape="oval">
         <solid android:color="@color/color1" />
         <stroke android:width="2dp" android:color="@android:color/white" />
     </shape>
</item>

and try both android:background and app:backgroundTint properties of FloatingActionButton to set background. Non of them worked. Note that app:backgroundTint seems just to accept color and not drawable

Anyone know any workaround?

Upvotes: 8

Views: 5587

Answers (1)

nomanr
nomanr

Reputation: 3775

Have you tried android:src attribute? Try this

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/your_drawable"
    andriod:scaleType="center"  /> <!-- You need to scale your src. ->

I hope it will work.

EDIT

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/your_icon" /> <!-- include your icon here -->
    <item>
        <shape android:shape="oval"> <!-- or rectangle -->

            <stroke
                android:width="2dp"
                android:color="@android:color/white" />
        </shape>
    </item>
</layer-list>

You can add your icon in drawable shape xml. And add a border to your icon and apply it to FAB.

I added a hdpi icon in my shape file and this is the result I got.

And the actual drawable size should be 24dp according to the Google design specs.

enter image description here

Upvotes: 0

Related Questions