0

I want to place the Floating Action button in between two editTexts inside a CardView like this: cardwithedittext

I tried the following but it isn't working.

How can I do this? Any help will be appreciated.

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    card_view:cardCornerRadius="25dp"
    android:layout_height="wrap_content"
    android:layout_margin="8dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"

        >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <EditText
                android:id="@+id/etOrigin"
                android:layout_width="204dp"
                android:layout_height="wrap_content"
                android:hint="Enter origin address" />

        </LinearLayout>
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/btnFindPath"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginBottom="16dp"
            android:layout_marginRight="16dp"
            android:onClick="fabClick"
            android:src="@mipmap/ic_add_white_24dp"
            app:fabSize="normal"
            app:layout_anchor="@id/fab_coordinator_layout"
            app:layout_anchorGravity="left|end" />


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <EditText
                android:id="@+id/etDestination"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Enter destination address" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >


        </LinearLayout>


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

3 Answers3

0

You can do this-

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    card_view:cardCornerRadius="25dp"
    android:layout_height="wrap_content"
    android:layout_margin="8dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <EditText
                android:id="@+id/etOrigin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Enter origin address" />
            <EditText
                android:id="@+id/etDestination"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Enter destination address" />
        </LinearLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/btnFindPath"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:onClick="fabClick"
            android:src="@drawable/ic_add"
            app:fabSize="normal"
            android:layout_gravity="center_vertical"
            app:layout_anchor="@id/fab_coordinator_layout" />

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

I have changed the padding and margin, set it according to your need.

Shivendra
  • 556
  • 3
  • 15
0

Use RelativeLayout as below:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
card_view:cardCornerRadius="5dp">

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

    <LinearLayout
        android:id="@+id/inputHolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/fabHolder"
        android:layout_toStartOf="@+id/fabHolder"
        android:orientation="vertical">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/fabHolder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/inputHolder"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/inputHolder">

        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:src="@android:drawable/ic_menu_send" />
    </RelativeLayout>

</RelativeLayout>

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

try this using linear layout

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    card_view:cardCornerRadius="25dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Your hint" />

            <EditText
                android:id="@+id/etDestination"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Your hint" />
        </LinearLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/btnFindPath"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="fabClick"
            android:src="@drawable/ic_down"
            app:fabSize="normal"
            android:layout_gravity="center"
            app:layout_anchor="@id/fab_coordinator_layout" />

    </LinearLayout>
</android.support.v7.widget.CardView>
AskNilesh
  • 67,701
  • 16
  • 123
  • 163