0

I have went through every question on Stack Overflow to place a view at the intersection of two layouts (ultimate question: How can I add the new "Floating Action Button" between two widgets/layouts) but I am seeing strange behavior while using the suggested answer.

My circularView always gets placed at top left corner of cardView, and I am not sure why this is happening. Please find the xml below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="com.phandaarpit.androidrecyclertutorial.MainActivity">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="250dp"
        app:cardUseCompatPadding="true"
        app:cardCornerRadius="5dp">

        <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">
                <TextView
                    android:id="@+id/im_pic"
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:background="?attr/colorControlHighlight"
                    />
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <TextView
                        android:id="@+id/profile_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Hello World!" />
                    <TextView
                        android:id="@+id/profile_sub_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Hello World!" />
                </LinearLayout>
            </LinearLayout>

            <android.support.design.widget.FloatingActionButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:elevation="2dp"
                app:layout_anchor="@id/im_pic"
                app:layout_anchorGravity="bottom|left"/>
        </android.support.design.widget.CoordinatorLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

I am trying to populate this view into RecyclerView and i am getting the below output. FAB is completely out of order on some and placed correctly on some.

Output from the code

Community
  • 1
  • 1
KungFu_Panda
  • 111
  • 11
  • Those `layout_anchor*` attributes won't work inside the `CardView` itself. They'll only work on `View`s that are direct children of the `CoordinatorLayout`. Move the `CircularImageView` to outside of the `CardView` tags. – Mike M. Jan 29 '17 at 00:10
  • @MikeM. can i use coordinatorLayout inside cardview itself and then use anchor tags? – KungFu_Panda Jan 29 '17 at 00:20
  • Oh, I see what you're trying to do, now. Apologies. I thought you were trying to anchor to the `CardView`. You could put the `CoordinatorLayout` inside a `CardView`, but you might have to rearrange your overall layout. The anchor `View` should be a child of the `CoordinatorLayout`, too. – Mike M. Jan 29 '17 at 00:31
  • Tried that as well @Mike M but doesn't seem to be working. – KungFu_Panda Jan 29 '17 at 00:33
  • also, do the view, to which i will anchor and the one i am anchoring, need to be the direct child of coordinatorLayout? – KungFu_Panda Jan 29 '17 at 00:35
  • Yeah, that's what I meant by the last sentence in my comment. Sorry if that was ambiguous. – Mike M. Jan 29 '17 at 00:37
  • Well, hang on. Maybe that behavior has changed with a newer support library version. Have a look at this answer: http://stackoverflow.com/a/30990661. – Mike M. Jan 29 '17 at 00:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134283/discussion-between-kungfu-panda-and-mike-m). – KungFu_Panda Jan 29 '17 at 00:42

0 Answers0