7

I want to Create the Layout like the Following Image with Material CardView and Custom Floating Action Button:enter image description here

And I was developed upto this Layout xml file as follows :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/mainview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.CardView
            android:id="@+id/cv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardCornerRadius="2dp"
            app:cardElevation="2dp"
            android:layout_marginBottom="@dimen/cardMarginVertical"
            android:layout_marginLeft="@dimen/cardMarginHorizontal"
            android:layout_marginRight="@dimen/cardMarginHorizontal"
            android:layout_marginTop="@dimen/cardMarginVertical"
            app:cardPreventCornerOverlap="false"
            app:contentPadding="0dp">

            <com.github.florent37.materialviewpager.sample.ExpandableTextView
                android:id="@+id/expandingTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="15dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="10dp"
                android:text="@string/longText" />

            <RelativeLayout
                android:id="@+id/buttonlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/expandingTextView"
                android:layout_gravity="bottom"
                android:background="@android:color/transparent">

                <com.getbase.floatingactionbutton.FloatingActionButton
                    android:id="@+id/action_c"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    fab:fab_colorNormal="#fff"
                    android:layout_alignParentRight="true"
                    fab:fab_colorPressed="#fff"
                    android:visibility="visible" />
            </RelativeLayout>
        </android.support.v7.widget.CardView>
    </RelativeLayout>
</FrameLayout>
Edric
  • 24,639
  • 13
  • 81
  • 91
Rajan Bhavsar
  • 1,977
  • 11
  • 25

1 Answers1

9

To implement UI like your picture, some code like below can help.

Parameters layout_anchor and layout_anchorGravity in FloatingActionButton are the point.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.CardView
            android:id="@+id/cv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="The Android Support Library package is a set of code libraries that provide backward-compatible versions of Android framework APIs as well as features that are only available through the library APIs. Each Support Library is backward-compatible to a specific Android API level. This design means that your applications can use the libraries' features and still be compatible with devices running Android 1.6 (API level 4) and up." />

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

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

    <android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_anchor="@id/cv"
        app:layout_anchorGravity="right|end|bottom"
        android:layout_margin="16dp"
        android:clickable="true"/>

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

Edit:

Early answer had some bug in Android 5.0.1 and 5.0.2, FAB would show below CardView.

Add NestedScrollView with layout_behavior outside CardView to work in Android 5.0.1 and 5.0.2

Ellie Zou
  • 2,021
  • 2
  • 16
  • 21
  • Are you able to do made changes a code in my posted code as above so i am able to do it easily. Thanks For Support. – Rajan Bhavsar Jul 06 '15 at 08:45
  • 1
    I removed some unhelpful code from my layout. You can have a look. – Ellie Zou Jul 06 '15 at 08:59
  • I got android.view.InflateException: Binary XML file line #8: Error inflating class android.support.design.widget.CoordinatorLayout in my layout by replacing the RelativeLayout to CoordinatorLayout as above question's xml file please help me. – Rajan Bhavsar Jul 06 '15 at 09:08
  • 1
    Oh, I forgot you didn't use support design library. Update your support library and support repository and add `com.android.support:design:22.2.0` to your build.gradle. – Ellie Zou Jul 06 '15 at 09:14
  • I was added com.android.support:appcompat-v7:21.0.3 in my build.gradle Is it not working ? @Ellie Zou – Rajan Bhavsar Jul 06 '15 at 09:17
  • It's `com.android.support:design:22.2.0`, not `com.android.support:appcompat` – Ellie Zou Jul 06 '15 at 09:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82467/discussion-between-rajan-bhavsar-and-ellie-zou). – Rajan Bhavsar Jul 06 '15 at 09:21
  • Please help me in this CardView's post as i was worked but not able to solve the Adapter Issue Please check this post and Thanks in Advance.:http://stackoverflow.com/questions/31319225/expandabletextview-in-cardview-to-open-and-close-with-click-android – Rajan Bhavsar Jul 10 '15 at 04:43