Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

floating action button unexpected anchor gravity change

I've got an activity and several fragments. One of the fragments contains FAB with an anchor gravity bottom|right|end. It looks good, but when I start another fragment and then press back button my FAB appears at the top left corner. I use Coordinator layout but it doesn't help. Here is 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">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="@+id/fragment_main_page">

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/main_page_listview"
    android:layout_marginTop="5dp">

</ListView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="3dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:src="@drawable/info_main_page"
        android:layout_gravity="center_vertical"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center_vertical"
        android:id="@+id/how_to_use"
        android:text="@string/how_to_use"
        android:textAllCaps="false"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="italic"
        android:background="@android:color/transparent"/>

</LinearLayout>

</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/floating_promos"
    android:layout_width="42dp"
    android:layout_height="42dp"
    android:layout_margin="16dp"
    android:clickable="true"
    android:src="@drawable/floating_promos"
    app:layout_anchor="@id/fragment_main_page"
    app:layout_anchorGravity="bottom|right|end"
    app:backgroundTint="#fb9f12"
    app:fabSize="mini"/>
   </android.support.design.widget.CoordinatorLayout>
like image 379
fregomene Avatar asked Nov 09 '22 00:11

fregomene


1 Answers

I did a small tweak to make it work as I needed I simply added the listener to check if the recycler view is updated and made changes to FAB inside it. Hope this will help you.

listWorkData.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        listWorkData.removeOnLayoutChangeListener(this);

        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) btnSubmitConfirm.getLayoutParams();
        lp.anchorGravity = Gravity.BOTTOM | GravityCompat.END;
        btnSubmitConfirm.setLayoutParams(lp);
    }
});

listWorkData.getAdapter().notifyDataSetChanged();
like image 167
Niroj Shr Avatar answered Nov 15 '22 06:11

Niroj Shr