Home » Android » android – Align ImageView to top right corner-Exceptionshub

android – Align ImageView to top right corner-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I have a RelativeLayout with images next to each other. The number of images will be variable.

    <RelativeLayout
        android:id="@id/multipleImages"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/border">

        <ImageView
            android:id="@+id/multi_image1"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/imag1"/>
        <ImageView
            android:id="@+id/multi_image2"
            android:layout_toRightOf="@+id/imag1"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/imag2"/>
        <ImageView
            android:id="@+id/multi_image3"
            android:layout_toRightOf="@+id/multi_image2"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/imag3"/>

        <ImageView
            android:id="@+id/multi_image4"
            android:layout_toRightOf="@+id/multi_image3"
            android:layout_marginTop="2dp"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/imag4"/>

    </RelativeLayout>

I want to add another image (the red rectangle) to the top-right corner slightly above the last image .
I tried layout_alignParentEnd but the layout goes full width.
Setting negative layout_marginTop the image goes up but is disappears outside the RelativeLayout.

enter image description here

How to&Answers:

Try this :

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

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:background="#ff0000" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginTop="25dp"
        android:layout_marginRight="25dp">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginRight="10dp"
            android:background="#000000" />

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginRight="10dp"
            android:background="#000000" />

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginRight="10dp"
            android:background="#000000" />

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginRight="10dp"
            android:background="#000000" />

    </LinearLayout>

</RelativeLayout>

You can change margin as per your requirement and have desired result

Answer:

make a new relative layout as a root layout and put your image view after your original relative layout

Answer:

<RelativeLayout android:id="@id/multipleImages"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/border"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

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

        <ImageView
            android:id="@+id/multi_image1"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/imag1"/>
        <ImageView
            android:id="@+id/multi_image2"
            android:layout_toRightOf="@+id/imag1"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/imag2"/>
        <ImageView
            android:id="@+id/multi_image3"
            android:layout_toRightOf="@+id/multi_image2"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/imag3"/>

        <ImageView
            android:id="@+id/multi_image4"
            android:layout_toRightOf="@+id/multi_image3"
            android:layout_marginTop="2dp"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/imag4"/>


    </RelativeLayout>

    <ImageView
        android:id="@+id/multi_image_last"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="2dp"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:scaleType="fitXY"
        app:srcCompat="@drawable/imag4"/>

</RelativeLayout>

Answer:

Try adding marginTop to your RelativeLayout, so when you add negative margin to the last image, it won’t dissappear

Answer:

I have used ConstraintLayout instead of RelativeLayout. Try using my answer:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/multipleImages"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:background="@drawable/border">

    <ImageView
        android:id="@+id/multi_image1"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/imag" />

    <ImageView
        android:id="@+id/multi_image2"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/multi_image4"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/imag2" />

    <ImageView
        android:id="@+id/multi_image3"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/multi_image2"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/imag3" />

    <ImageView
        android:id="@+id/multi_image4"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/multi_image1"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/imag4" />

</android.support.constraint.ConstraintLayout>

Answer:

Use this xml :

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="@dimen/dimension_40">

    <LinearLayout
        android:id="@+id/multipleImages"
        android:layout_width="wrap_content"
        android:gravity="center"
        android:layout_gravity="bottom"
        android:layout_height="wrap_content"
        android:padding="@dimen/dimension_10"
        android:background="@drawable/grey_border"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/multi_image1"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/ic_check_icon_blue" />

        <ImageView
            android:id="@+id/multi_image2"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_toRightOf="@+id/multi_image1"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/ic_check_icon_blue" />

        <ImageView
            android:id="@+id/multi_image3"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_toRightOf="@+id/multi_image2"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/ic_check_icon_blue" />

        <ImageView
            android:id="@+id/multi_image4"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginTop="2dp"
            android:layout_toRightOf="@+id/multi_image3"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/ic_check_icon_blue" />

    </LinearLayout>


    <ImageView
        android:id="@+id/multi_image5"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="end|center_vertical"
        android:layout_marginStart="@dimen/dimension_15"
        android:layout_marginBottom="@dimen/dimension_10"
        android:background="@drawable/notification_background"
        />
</FrameLayout>