Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect fab position in fragment on startup

I have a single fragment activity. fragment has a coordinator layout, fab as direct child of it and other views.

Activity Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar_layout" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Fragment Layout

<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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.github.aakira.expandablelayout.ExpandableLinearLayout
        android:id="@+id/statistic_expand_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/toolbar"
        android:background="@color/colorPrimary"
        android:orientation="vertical"
        app:ael_duration="150"
        app:ael_interpolator="accelerateDecelerate">

        <com.github.mikephil.charting.charts.BarChart
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/overall_statistic_bar_chart"
            android:layout_width="match_parent"
            android:layout_height="@dimen/statistic_small"/>

        <TextView
            style="@style/SmallTextTheme"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/spacing_small"
            android:gravity="center"
            android:text="@string/overall_statistic"
            android:textColor="@color/colorTextPrimary"/>

    </com.github.aakira.expandablelayout.ExpandableLinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/statistic_expand_layout"
        android:scrollbars="vertical"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/spacing_tiny"
        android:layout_below="@id/statistic_expand_layout"
        android:background="@drawable/shadow_updown"/>

</RelativeLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/activity_main_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/spacing_standard"
    android:src="@drawable/ic_add"
    app:layout_anchor="@id/recycler_view"
    app:layout_anchorGravity="bottom|end"/>

</android.support.design.widget.CoordinatorLayout>

When I start activity with this fragment fab position is incorrect

fab

but when I press any button or view fab goes to its correct position in the right bottom corner. When I put this layout directly into activity fab position is always correct. Could anyone help me to figure out what is the problem please?

like image 745
Olexii Muraviov Avatar asked Nov 08 '22 04:11

Olexii Muraviov


1 Answers

but when I press any button or view fab goes to its correct position in the right bottom corner. When I put this layout directly into activity fab position is always correct.

Solution: Here is the documentation of AppBarLayout and CoordinatorLayout which says how they should look like:

    <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.support.v4.widget.NestedScrollView
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             app:layout_behavior="@string/appbar_scrolling_view_behavior">

         <!-- Your scrolling content -->

     </android.support.v4.widget.NestedScrollView>

     <android.support.design.widget.AppBarLayout
             android:layout_height="wrap_content"
             android:layout_width="match_parent">

         <android.support.v7.widget.Toolbar
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

         <android.support.design.widget.TabLayout
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

     </android.support.design.widget.AppBarLayout>

 </android.support.design.widget.CoordinatorLayout>

So now, RelativeLayout in your design shouldn't be there. Put the CoordinatorLayout in the main layout of the Activity then, Try to use AppBarLayout when using Toolbar with CoordinatorLayout Because now CoordinatorLayout imagines the RelativeLayout as it's AppBarLayout.

And that's it. One more thing, Don't forget to add : app:layout_behavior="@string/appbar_scrolling_view_behavior" to your view under the AppBarLayout.

Let me know if you need any help or asking question.

like image 168
ʍѳђઽ૯ท Avatar answered Nov 24 '22 01:11

ʍѳђઽ૯ท