Reputation: 196
I have this weird issue that when I replace a fragment and go back to it using the back stack, the FAB is sometimes showing on the upper left of the screen when I anchored it to bottom | end. Its perfectly anchored to bottom | end when just loaded from the activity. How can I solve this? Thanks in advance.
Edit There's like a 50% chance that it anchors correctly and 50% that it will not.
I have tried manually setting the gravity of the FAB in the java code using this but still does not work.
FloatingActionButton fab = (FloatingActionButton)view.findViewById(R.id.fab);
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
lp.anchorGravity = Gravity.BOTTOM | GravityCompat.END;
fab.setLayoutParams(lp);
Here's my code for the FAB
<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:orientation="vertical"
tools:context="com.berstek.gradius.fragments.RecordsFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recview_records0"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add_white"
app:layout_anchor="@id/container"
app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>
Upvotes: 1
Views: 566
Reputation: 1490
I have added only one string to CoordinatorLayout and issue disappeared.
android:fitsSystemWindows="true"
So I can conclude that sometimes the Coordinator layout is not on the full screen and than this misbehaviour comes up.
This is full code of my layout.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/accountTransactionList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<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_margin="16dp"
android:src="@drawable/ic_add_white_24dp"
app:layout_anchor="@id/accountTransactionList"
app:layout_anchorGravity="bottom|right|end"
/>
</android.support.design.widget.CoordinatorLayout>
Upvotes: 1
Reputation: 3916
One solution that works for me is to call RequestLayout of your view (the one that has the Floating action button) in the OnResume:
@Override
public void onResume() {
super.onResume();
getView().post(new Runnable() {
@Override
public void run() {
getView().requestLayout();
});
}
On Xamarin Android:
public override void OnResume()
{
base.OnResume();
this.View.Post(() => this.View.RequestLayout());
}
Hope it helps.
Upvotes: 1
Reputation: 196
I ended up using a Framelayout instead and it shows just fine. But if anyone knows why CoordinatorLayout is having that weird issue, I'd love to hear it.
Here's my code.
<FrameLayout 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="@android:color/white"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="vertical"
tools:context="com.berstek.gradius.fragments.RecordsFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/recview_records0"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
<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_margin="10dp"
android:clickable="true"
android:src="@drawable/ic_add_white"
app:backgroundTint="@color/colorAccent"
app:layout_anchorGravity="bottom|end" />
Upvotes: 0