2

I'm simply trying to do this alignment with constraintlayout but I'm not sure It's possible or not.

How can I do it ?

Sample

Sample xml

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="320dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/com_facebook_profile_picture_blank_portrait" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/floatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    app:srcCompat="@android:color/holo_green_light" />

My workaround.

My possible workaround.

Emre Akcan
  • 982
  • 10
  • 27
  • what do you mean by is it allowed or not ? – Umair Jun 05 '18 at 07:52
  • It's generally done with coordinator layout. I meant is it possible and if it's possible is it the right way. – Emre Akcan Jun 05 '18 at 07:54
  • Yes everything is possible, it's not wrong until or unless it is completing your requirements. It's possible and I believe you already have achieved it. :) – Umair Jun 05 '18 at 07:56
  • https://stackoverflow.com/a/39267214/4407266 – Aditya Vyas-Lakhan Jun 05 '18 at 07:56
  • @Umair if you meant my workaround it will probably shown different at different screens. – Emre Akcan Jun 05 '18 at 07:58
  • @EmreAkcan yes that's because it will adjust itself according to different screen sizes but the layout will remain the same. – Umair Jun 05 '18 at 08:00
  • @Umair So floating button's position on imageview will be same at every screen with my second adjustment? – Emre Akcan Jun 05 '18 at 08:02
  • @EmreAkcan ah nope it won't why because every screen has a different resolution so on bigger screens it will go down. Your best bet is to use framelayout of use relative layout inside constraintlayout. – Umair Jun 05 '18 at 08:06
  • @EmreAkcan Using *Frame Layout* will be effective for all resolutions. – Jay Rathod Jun 05 '18 at 08:28

2 Answers2

5

Constraining both the top and the bottom of the FloatingActionButton to the bottom of the ImageView will center it on the edge. Also android:layout_width="match_parent" of the ImageView should be changed to 0dp to enforce horizontal constraints. Example XML looks like this:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="320dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/com_facebook_profile_picture_blank_portrait" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@id/imageView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageView"
        app:srcCompat="@android:color/holo_green_light" />

</android.support.constraint.ConstraintLayout>
Pawel Laskowski
  • 6,078
  • 1
  • 21
  • 35
0

You can do it with constraint layout with the following code.

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="320dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/carrick" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="176dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/bcslogo" />
</android.support.constraint.ConstraintLayout>

otherwise you can use relative layout instead of constraint you can place your all content in relative layout like following

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="20dp"
        android:id="@+id/notesbg"
        android:layout_gravity="bottom|end">
    <android.support.v7.widget.RecyclerView
        android:layout_marginTop="2dp"
        android:id="@+id/myRecyclernote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


        <android.support.design.widget.FloatingActionButton
            android:id="@+id/myFAB"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="end|bottom"
            android:layout_marginEnd="20dp"
            android:layout_marginBottom="50dp"
            app:backgroundTint="#00b4ed"
            android:src="@drawable/floatingbtn"
            app:borderWidth="0dp"
            app:elevation="8dp" />
    </RelativeLayout>  
Adriaan
  • 17,741
  • 7
  • 42
  • 75
Vishal Sharma
  • 1,051
  • 2
  • 8
  • 15