I was trying to build something like the below image:
Here is my xml code:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:src="#drawable/vector_nav_qr_scanner"
app:backgroundTint="#android:color/white"
app:layout_anchor="#id/bottom_app_bar" />
I was searching a lot but couldn't figure out how can I add the light blue background with a white circle border and also an icon in the center. Can any one help?
Try this:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#B1E4F6" //inner background color
android:src="#drawable/scan"
app:backgroundTint="#color/white" //outer background color (border color)
app:borderWidth="8dp"
app:maxImageSize="16dp" //inner icon size
app:tint="#android:color/holo_blue_dark" /> //inner icon color
Result:
Add
app.strokeColor = "#000033"
app.strokeWidth = "3dp"
in your floating action button xml..
for example:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:src="#drawable/vector_nav_qr_scanner"
app:backgroundTint="#android:color/white"
app:layout_anchor="#id/bottom_app_bar"
app:strokeColor = "#000033"
app:strokeWidth = "3dp" />
edit: app:strokewidth and app:strokeColor is working for ExtendedFloatingActionButton. Sorry, that's my fault.
You can use
app:borderWidth="4dp" in stead of app:strokeWidth = "3dp". remove strokeColor. there showes little border.
i hope it will help you.
please let me know if it doesn't work.
OR
define custom shape in drawable folder
fab_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false" >
<solid android:color="#android:color/transparent" />
<!-- here set the width and color of your border -->
<stroke
android:width="5dp"
android:color="#android:color/darker_gray" />
</shape>
then wrap your FloatingActionButton with a layout. use fab_bg.xml file in layout's background.
for example:
<LinearLayout
android:id="#+id/fabWrapperId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/fab_bg"
android:padding="5dp">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:src="#drawable/vector_nav_qr_scanner"
app:backgroundTint="#android:color/white"
app:layout_anchor="#id/bottom_app_bar"
app:fabSize="normal" />
</LinearLayout>
NB: make sure your stroke width from fab_bg and layout padding keep the same.
Related
I have FAB and trying to fill a drawable(round_drawable.xml) to my entire fab but the drawable appears currently as in the top left corner as shown here,
I tried a solution from Setting src and background for FloatingActionButton but it did not work, added most attributes from other sources but neither scaleType, src, background did not work.
How do I get to fill the FAB with the round_drawable ?
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="100dp"
android:layout_height="95dp"
android:scaleType="fitXY"
app:backgroundTint="#color/white"
android:src="#drawable/round_drawable"
android:backgroundTintMode="src_atop"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
round_drawable.xml
#drawable/right is a png image of right arrow.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:innerRadius="50dp"
android:shape="oval">
<corners android:radius="10dp" />
<gradient
android:angle="45"
android:endColor="#D425B5"
android:startColor="#EBF928" />
</shape>
</item>
<item
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp">
<bitmap
android:gravity="center"
android:src="#drawable/right" />
</item>
</layer-list>
Just add this line in you dimen.xml file
<dimen name="design_fab_image_size" tools:override="true">56dp</dimen>
NOTE : 56dp is default size of FAB buttom
Default - 56dp
mini - 40dp
custom - whatever you have specified in fabCustomSize
TIP : use app:fabCustomSize rather giving custom height width to FAB button.
Example
in your layout
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="#color/white"
<!--inmprtent-->
app:fabCustomSize="100dp"
android:src="#drawable/round_drawable"
android:backgroundTintMode="src_atop"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
now create dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<dimen name="design_fab_image_size" tools:override="true">100dp</dimen>
</resources>
keep your round_drawable.xml as it is and you are good to go
Result
cheers..
You don't have to give specific size to FAB.And it won't require background drawable also for any specific color. It has attribute fabSize in which you can pass predefined sizes like mini(40dp), normal(56dp). And still if you want to add size remove sizes from drawable <item> tag. That tag actually making your drawable small.
check this sample code for creating Fab with image in center
<android.support.design.widget.FloatingActionButton
android:id="#+id/fbtn_addNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="#dimen/margin_16"
android:layout_marginRight="#dimen/margin_16"
app:srcCompat="#drawable/ic_add_white_28dp"
app:backgroundTint="#color/colorPrimary"
app:borderWidth="#dimen/margin_0"
app:elevation="#dimen/margin_8"
app:fabSize="normal"
android:visibility="visible"
app:pressedTranslationZ="#dimen/margin_5" />
It will look like this. Hope it will help...
Updated answer of 2019 with androidX:
Dependency : implementation 'com.getbase:floatingactionbutton:1.10.1'
XML:
<com.getbase.floatingactionbutton.FloatingActionsMenu
android:id="#+id/multiple_actions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_margin="#dimen/fab_margin"
android:layout_marginBottom="20dp"
app:fab_addButtonColorNormal="#color/colorPrimaryDark"
app:fab_addButtonColorPressed="#color/colorPrimaryDark"
app:fab_colorPressed="#color/colorPrimaryDark"
app:fab_labelStyle="#style/menu_labels_style">
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/btn_langauge_translation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_language_icon"
app:fab_size="normal"
android:paddingBottom="15dp"
app:fab_colorNormal="#color/colorPrimaryDark"
app:fab_colorPressed="#color/colorPrimaryDark"
app:fab_title="" />
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/btn_facebook_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_fb_icon"
app:fab_size="normal"
android:paddingBottom="15dp"
app:fab_colorNormal="#0184FF"
app:fab_colorPressed="#0184FF"
app:fab_title="" />
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/btn_whats_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_whatsapp_icon"
app:fab_colorNormal="#4EC248"
android:paddingBottom="15dp"
app:fab_size="normal"
app:fab_colorPressed="#4EC248"
app:fab_title="" />
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/btn_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_email_icon"
app:fab_colorNormal="#848484"
android:paddingBottom="15dp"
app:fab_colorPressed="#848484"
app:fab_size="normal"
app:fab_title="" />
</com.getbase.floatingactionbutton.FloatingActionsMenu>
Output:
I used this code and it worked:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_add"
android:scaleType="center"
android:backgroundTint="#android:color/transparent"
android:outlineAmbientShadowColor="#android:color/transparent"
android:outlineSpotShadowColor="#android:color/transparent"
/>
and in the dimens.xml file:
<dimen name="design_fab_image_size" tools:override="true">40dp</dimen>
Just add this line in you style.xml file
<style name="FullImageFloatingButton" parent="Widget.Design.FloatingActionButton">
<item name="fabSize">normal</item>
<item name="maxImageSize">56dp</item>
</style>
<style name="FullImageMiniFloatingButton" parent="Widget.Design.FloatingActionButton">
<item name="fabSize">mini</item>
<item name="maxImageSize">40dp</item>
</style>
Use FullImageFloatingButton for normal FloatingButton and FullImageMiniFloatingButton for mini FloatingButton.
i have a strange issue. I created a simple Button which looks like that :
<com.google.android.material.button.MaterialButton
android:id="#+id/continue_button"
android:background="#drawable/green_button_selector"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/landing_margin"
android:layout_marginEnd="#dimen/landing_margin"
android:layout_marginBottom="#dimen/landing_margin"
android:text="#string/login_button_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
I want to make Button green with rounded corners, so I created a xml file called green_button_selector and set is as button background. This file code's is posted below
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="#color/colorGreenButton" />
<corners android:radius="10dp" />
</shape>
But instead of my button getting green it has colorAccent of my app, any ideas what am I doing wrong?
If you are using material button you don't need to create custom drawable.
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
app:backgroundTint="#android:color/holo_green_dark"
app:cornerRadius="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="BUTTON" />
More: https://material.io/develop/android/components/material-button/
<com.google.android.material.button.MaterialButton
android:id="#+id/material_text_button"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/outlined_button_label_enabled"/>
You can below property for same.
app:cornerRadius
app:backgroundTint
Read more from
Material Button official document
This post has plenty of answers that explain how to create a rounded button in Android. But almost all of the answers end up expanding the size of the button.
For instance, in this layout, notice how the top button (the one with the rounded corners) has significantly more padding then the rest of the buttons.
Here's the code for a button without rounded corners;
<Button
android:id="#+id/useKeyboardButton"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/use_keyboard_msg"
app:layout_constraintBottom_toTopOf="#+id/privacyPolicyButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/enableKeyboardButton"
style="#style/Widget.AppCompat.Button.Colored"
/>
And here's the code for a button with rounded corners:
<Button
android:id="#+id/enableKeyboardButton"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/enable_keyboard_msg"
app:layout_constraintBottom_toTopOf="#+id/useKeyboardButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
style="#style/Widget.AppCompat.Button.Colored"
android:background="#drawable/rounded_corners"
/>
where the background is this drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/colorAccent"/>
<corners android:radius="10dp"/>
</shape>
Try removing the style attribute and only setting the background drawable.
This could be done with newer support library (v28) MaterialButton.
<android.support.design.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MATERIAL BUTTON"
android:textSize="18sp"
app:icon="#drawable/ic_android_white_24dp" />
Take a look at this great article
Use MaterialButton
and add app:cornerRadius="8dp"for rounded corner
I created a simple circular button to create - and + buttons for a custom dialog picker.
As seen in the screenshot the - and the + are shifted a little bit down and not centered in the middle of the custom background.
My custom background in the drawable/circular_button.xml looks like this.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:color="#color/colorPrimaryDark" android:width="1dp" />
<solid android:color="#color/colorPrimary" />
<size android:width="30sp" android:height="30sp"/>
</shape>
The button is configured like this inside the LinearLayout
<Button
android:id="#+id/decrease_one"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_marginRight="20sp"
android:background="#drawable/rounded_button"
android:text="-"
android:textColor="#color/white"
android:textSize="30sp"
android:textStyle="bold" />
In one of the posts I read about the tag minHeight but it didn't solve my problem.Any ideas ?
Batter to use FloatingActionButton mini
<android.support.design.widget.FloatingActionButton
android:id="#+id/activity_my_digital_executor_fabDigitalex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:fabSize="mini"
card_view:srcCompat="#drawable/ic_new_plus" />
Add 'com.android.support:design:26.0.+' in your App dependencies
This is happen because of your Button's android:textSize="30sp" if you set 50sp its goes down the line more,
Just convert your Buttons to Textviews and set android:gravity="center" also keep your textsize 30sp
<TextView
android:id="#+id/decrease_2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:background="#drawable/rounded_button"
android:text="+"
android:gravity="center"
android:textColor="#color/white"
android:textSize="30sp"
android:textStyle="bold" />
If you are use a Imageview instead of Textview than your problem is solved.
I want to create a view like image below. My implementation has some linearLayouts. One root with a custom drawable to get the rounded edges and then others for the two textviews and view divider. Is there a faster and easier way to do this?
You can do this with just 1 LinearLayout, the root one. The LinearLayout is used just to order other views. So, what you need to do is to use the vertical orientation and add two text views.
On the first one you set the background color to a light gray. And remember to use the gravity as center so your text will be placed on the center of the text view.
I dont have much time, just tried to give a quick sample. Here's my code for your question:
your_main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/your_bg.xml">
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="my text" />
<View
android:id="#+id/seperator
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/tv1" />
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2nd text here"
android:layout_below="#+id/seperator" />
</RelativeLayout>
your_bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="5dp"
/>
<solid android:color="#fff" />
<stroke android:color="#000" />
</shape>