Search code examples
androidlayoutfloating-action-button

Button goes under a layout which is defined below it


Hi I have a fab button in my layout "addfab" which is showing correctly in most phones but in android 4.4 kitkat hides under a Relative layout that is defined under it in xml.They both are defined to be below one object Here is my xml.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fbutton="http://schemas.android.com/tools"
android:weightSum="1">


<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bar1"

    >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:textAlignment="center"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        >

    </android.support.v7.widget.Toolbar>



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


<android.support.design.widget.FloatingActionButton
    android:id="@+id/addfab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@id/bar1"
    android:layout_marginRight="20dp"
    android:layout_marginTop="130dp"
    android:clickable="true"
    android:elevation="2dp"
    android:src="@drawable/logo_green"
    app:fabSize="normal"
    />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/pic"
    android:layout_below="@id/bar1"
    android:background="@color/colorPrimary">
    <com.mikhaellopez.circularimageview.CircularImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/image_gol"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        app:civ_border_color="@color/colorPrimary"
        app:civ_border_width="5dp"
        app:civ_shadow="false"
        app:civ_shadow_radius="5"
        app:civ_shadow_color="#9e9e9e"/>

</RelativeLayout>

What should I do? Why androids act differently?


Solution

  • Instead of RelativeLayout, use CoordinatorLayout:

    <android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.AppBarLayout>
    //Toolbar here
    </android.support.design.widget.AppBarLayout>
    
    // your fab and relativelayout here
    </android.support.design.widget.CoordinatorLayout>
    

    As the root level tag. You'll be able to anchor it(The FloatingActionButton) to the views you want by using:

    app:layout_anchor="@id/viewpager"
    app:layout_anchorGravity="bottom|right|end"
    

    Attributes. This also allows you to have more functionality like hiding Toolbar or etc in future. You'll also need to change-remove old RelativeLayout's attributes later in order to use CoordinatorLayout.

    Take a look: https://guides.codepath.com/android/floating-action-buttons