Mauro Piccotti
Mauro Piccotti

Reputation: 1847

Android: positioning of Floating Action Button unpredictable

Some of my floating action buttons don't remain where I want, and I cannot understand why. enter image description here The first is how they should be (and actually sometimes it works), the other two images show how they behave sometimes... and I cannot find a "pattern" in this behavior.

This is the layout (only one half):

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/lay_attivita_edit_ubicazioni"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingLeft="@dimen/list_padding_lateral"
        android:paddingRight="@dimen/list_padding_lateral">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:id="@+id/container_attivita_edit_origini">
            <TextView
                style="@style/FieldLabel"
                android:layout_marginTop="20dp"
                android:text="@string/attivita_edit_origini" />
            <ListView
                style="@style/ListView"
                android:id="@+id/list_attivita_edit_origini"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical"
                android:fadeScrollbars="false"
                android:background="@drawable/border_rectangle" />
        </LinearLayout>
    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        style="@style/FloatingActionButton.Small"
        android:id="@+id/fab_attivita_edit_ubicazioni_edit_origini"
        app:layout_anchor="@id/list_attivita_edit_origini"
        app:layout_anchorGravity="top|right|end" />
</android.support.design.widget.CoordinatorLayout>

I tried also to se anchor and anchorGravity programmatically but the result is the same. It could happen because depending on some logic one of the two sections could not be visible, in case I make them invisible at the end of OnCreateView.

It doesn't depend on the visibility of the "anchor list" because the fab move even if I always leave both the lists visible. I tried also to change programmatically anchor and anchorId of the fab at every change of visibility of them or of their lists, nothing.

CoordinatorLayout.LayoutParams editOrigineFabParams = (CoordinatorLayout.LayoutParams)editOrigineFab.LayoutParameters;
editOrigineFabParams.AnchorId = Resource.Id.list_attivita_edit_origini;
editOrigineFabParams.AnchorGravity = (int)(GravityFlags.Top | GravityFlags.Right | GravityFlags.End);
editOrigineFab.LayoutParameters = editOrigineFabParams;

With the same conditions/code sometimes they work and sometimes they don't. It looks like a bug in Android. I tried with Android 7.0 and 7.1, Xamarin.Android.Support.Compat.25.3.1. Anyone who had a similar problem or who has an idea of what could be the reason?

Upvotes: 1

Views: 1487

Answers (2)

yash darak
yash darak

Reputation: 368

use layout_gravity for positioning .

<android.support.design.widget.FloatingActionButton
   android:id="@+id/fab"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="bottom|right"
   android:src="@drawable/ic_edit"
   android:layout_marginRight="15dp"
   android:layout_marginBottom="15dp" / >

Upvotes: 0

yash darak
yash darak

Reputation: 368

Use FAB by james montemagno.

Add FAB by james montemagno from nuget to your project. https://www.nuget.org/packages/Refractored.FloatingActionButton/

<Refractored.Fab.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:src="@drawable/ic_action_content_new"
        app:app_colorNormal="@color/primary"
        app:app_colorPressed="@color/primary_pressed"
        app:app_colorRipple="@color/ripple" />

enter image description here

Upvotes: 1

Related Questions