Search code examples
androidxamarin.androidandroid-tablayoutfloating-action-button

Floating Action Button doesn't appear correctly


I have a floating action button in my Fragment inside a TabLayout.

When the page loads, the button stays like this:

enter image description here

After I click on "Quilometragem final" field, the keyboard shows up and then my button stays in correct position:

enter image description here

How can I fix this to show my floating action button in correct position when page loads?

This is my layout's xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true">
  <android.support.design.widget.CoordinatorLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="@dimen/common_margin"
        android:layout_marginRight="@dimen/common_margin">

      <!-- another fields -->

      <!-- KM FINAL -->
      <LinearLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dp">
        <TextView
            android:text="@string/end_exam_mileage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="25sp"
            android:fontFamily="sans-serif-condensed"
            android:textColor="@color/primary" />
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
          <EditText
              android:id="@+id/txtFinalKM"
              android:fontFamily="sans-serif-condensed"
              android:inputType="number"
              android:hint="@string/end_exam_insert_mileage"
              android:textSize="25sp"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />
        </android.support.design.widget.TextInputLayout>
      </LinearLayout>
    </LinearLayout>
          <!-- NEXT BUTTON -->
        <com.github.clans.fab.FloatingActionButton
            android:id="@+id/fab_next_information"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginRight="@dimen/common_margin"
            android:layout_marginBottom="@dimen/common_margin"
            android:src="@drawable/icon_fab_mini_next"
            android:clickable="true"
            app:fab_shadowColor="#00FFFFFF"
            app:fab_colorNormal="#272B35"
            app:fab_colorPressed="@color/accent"
            app:fab_colorRipple="#272B35" />
  </android.support.design.widget.CoordinatorLayout>
</LinearLayout>

Solution

  • Thanks to Aswin P Ashok comment, I was able to fix this bug reading this.

    The solution was to add the following code inside OnCreateView in my Fragment:

    _linearLayout.Post(new Runnable(() =>
    {
        _linearLayout.RequestLayout();
    }));
    

    Why this works?

    As told here,

    Posting requestLayout() works because the dependency graph is being built in the onMeasure() method of CoordinatorLayout. After building the graph once, the findAnchorView() method was called for every child. In the next pass when building the graph in onMeasure() the method dependsOn() returns correct result (unless some new views were added, for them it will not work) and the graph builds correctly. It has to be called in post(), because calling requestLayout() before the previous measure/layout pass begins does not cause new pass.