I am trying to show textview on the top of Floating Action Button. Inside FrameLayout, I have 1 FAB and 1 TextView:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|start"
android:padding="#dimen/fab_margin">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabAddSeller"
android:layout_width="70dp"
android:layout_height="70dp"
app:backgroundTint="#3780f4"
android:stateListAnimator="#null"/>
<TextView
android:id="#+id/tvSellRecordCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/sell_record_counter"
android:layout_gravity="top|end"
android:text="12"
android:textColor="#FFFFFF"
android:textSize="10sp" />
</FrameLayout>
According to this answer, I have added
android:stateListAnimator="#null"
to FAB - no difference. I have put TextView after FAB - no effect.
How to show another view on top of FloatingActionButton?
In Android Studio, I could fix after adding an elevation to the text view:
android:elevation="6dp"
It seems that FAB has 5dp of elevation.
However, I'm not sure if this totally works to you since elevation is available for API>=21.
Try to make some tests in real devices...
Maybe, android:stateListAnimator="#null" is enough in real devices/emulator.
UPDATE
After adding app:elevation, this fix started to work with older APIs as well
app:elevation="6dp"
android:elevation="6dp"
Replace FrameLayout with RelativeLayout and add
<android.support.design.widget.FloatingActionButton
...
android:layout_below="#+id/tvSellRecordCount"
...
/>
Related
I have an app with an expanded button implemented.
The button shrinks and expands perfectly fine in constraint layout or any other layout except in motion layout.
In which the button does not get expanded after shrink and in the extended state when the state of the button change to shrink on the click of a button then the text gets disappears but the size remains the same.
Version of material design link "implementation 'com.google.android.material:material:1.3.0-beta01'"
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="#+id/btn_call_helpline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/dimen_20dp"
android:text="#string/txt_emergency"
android:textColor="#color/color_white"
app:backgroundTint="#color/red"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:cornerRadius="#dimen/dimen_20dp"
app:elevation="#dimen/dimen_10dp"
app:icon="#drawable/ic_baseline_call_24"
app:iconTint="#color/color_white"
app:rippleColor="#color/color_white" />
I have verified the code and it does not have any such constraint that prevents shrinking and expanding.
The FAB seems to be having trouble with being measured twice. (motionLayout needs to do that)
A Simple work around is to wrap the fab in a container
Like linearLayout
put the LinearLayout in MotionLayout
<LinearLayout
android:id="#+id/fabwrap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textColor="#color/white"
app:backgroundTint="#F00"
app:icon="#drawable/ic_battery"/>
</LinearLayout>
i have problem with FloatingActionButton in RelatieLayout. I use DP for margin top and left. When i change device button is not on it's place. You can see on screens. First one is from editor, second from device.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="3dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingTop="3dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewFloor5"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="430dp"
android:layout_marginLeft="300dp"
app:backgroundTint="#color/primaryColor"
android:src="#drawable/ic_action_calendar"
app:fabSize="normal"
android:id="#+id/floorActionButton"/>
</RelativeLayout>
[
I also tried android:gravity="center|bottom" but nothing happens.
I also tried android:gravity="center|bottom" but nothing happens.
Since you are using RelativeLayout - gravity won't work. Instead use android:layout_alignParentBottom along with the margins
<RelativeLayout
....
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="16dp"
app:backgroundTint="#color/primaryColor"
android:src="#drawable/ic_action_calendar"
app:fabSize="normal"
android:id="#+id/floorActionButton"/>
</RelativeLayout>
Try this...Don't use margin to set FloatingActionButton position always use gravity.
Follow this tutorial from androidhive.
https://www.androidhive.info/2015/12/android-material-design-floating-action-button/
I have set layoutgravity but use appropriately according to the parentlayout properties for gravity.
If you follow the tutorial above it will be easy for in my case i placed the fab in a </android.support.design.widget.CoordinatorLayout> and included the layout inside it like in the tutorial.
Just follow it it will be easy just don't use margin to set its position it will break in different screens like your above error .
<android.support.design.widget.FloatingActionButton
android:id="#+id/floorActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:backgroundTint="#color/primaryColor"
android:src="#drawable/ic_action_calendar"
app:fabSize="normal"/>
I am doing some operation on floating action button . I have a list view and showing google ad at bottom of my screen, The floating action button is showing at same place (bottom|end). so action button is hiding the ad.
I want shift the Floating action button little above the ad linear layout.
Please have a look the image:
My code is look like:
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="bottom|end"
android:src="#drawable/fav"
android:adjustViewBounds="false" />
adLayoutId is ad layout id
Please help.
Thanks in advance.
You should use RelativeLayout, and as we know, later children in a RelativeLayout tend to float over earlier children in a RelativeLayout.
So, to have a FAB float over of any layout, make sure that the FAB is defined after all views (at the end of the XML layout).
If you use android:layout_margin="#dimen/fab_margin" the best way to manipulate FAB is set dependencies between views, for example:
<?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">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/adLayoutId"/>
<LinearLayout
android:id="#+id/adLayoutId"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#android:color/black"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/adLayoutId"
android:layout_alignParentRight="true"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/fav"/>
</RelativeLayout>
Floating action button always will be above the ad linear layout, and independent from ad layout size.
Hope it helps.
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="100dp"
android:layout_marginRight="30dp"
/>
These two lines inside the fab button helped me solved this problem:
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Here's the entire code I used for the button alone:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_newStudent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
android:contentDescription="#string/todo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="#drawable/ic_baseline_add_24" />
THESE TWO LINES ARE IMPORTANT.
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
Add a bottom Margin of 55dp which is the size of Admob Banners
This line in either the floating button or enclosing layout.
android:layout_marginBottom="55dp
In my app I am using a material design floating action button. Whenever I change the backgroundTintColor property of the button to something with an alpha below 255, it creates this strange circle within the button. I have provided a picture of the button below with my xml code... Any help would be greatly appreciated.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right|end"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="55dp"
android:layout_height="55dp"
android:clickable="true"
android:layout_margin="12dp"
android:src="#drawable/ic_play"
app:backgroundTint="#64a9a9a9"
android:layout_gravity="center"
/>
</FrameLayout>
Its most likely because background is used to create shadow or elevation and src to show the main content of fab but you are changing it take a look at this source code of fab https://android.googlesource.com/platform/frameworks/support/+/master/design/src/android/support/design/widget/FloatingActionButton.java?autodive=0%2F%2F
I am getting light grey color below the FloatingActionButton as shown in image. Any one know how to remove it. I tested it on Lollipop version.
My xml code
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/ic_add_black"
app:borderWidth="0dp"
app:fabSize="mini"
app:rippleColor="#color/colorPrimaryDark" />
Try set elevation:
app:elevation="0dp"
It happens when you set an elevation and the fab element has not space enough to display the "shadow".
Then you can set the elevation as 0dp as suggested above, or set a margin like
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_build_white_36dp"
android:id="#+id/build"
app:elevation="4dp"
android:clickable="true"
android:layout_margin="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
In this way the "elevation-effect" will appear correctly