-1

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?

atefe
  • 1
  • 1
  • 1
    hope it helps https://stackoverflow.com/a/41032921/2993388 – bbadawee Oct 22 '18 at 08:03
  • But I use RelativeLayout not CoordinatorLayout – atefe Oct 22 '18 at 08:21
  • @atefe `RelativeLayout` doesn't seem to be the best choice in there since you have a layout which has Fab Button-`AppBarLayout` and `Toolbar`. The reasonable way is to design that using `CoordinatorLayout` since it allows more functionality and the best behavior. – ʍѳђઽ૯ท Oct 22 '18 at 08:24
  • In newer versions, it is working because elevation is set to 02. On older devices, there's no elevation. So, you have to update your layout and add the FAB at the end of the file. RelativeLayout was placed after the FAB in your XML. So, android will render the RelativeLayout above the FAB. – guipivoto Oct 22 '18 at 11:33
  • Thanks everyone it helped a lot – atefe Oct 23 '18 at 14:41

1 Answers1

0

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

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108