Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing an image on the top left of another image using Constraint Layout

I'm trying to place an icon top left of an image using Constraint Layout but it needs to be offset from the center of its sides. Like in the pictures:

This is what I've got so far:

This is what I've got so far

This is the end goal:

This is the end goal

and this is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@color/grey_500">

    <ImageView
        android:id="@+id/iv_landscape_art"
        android:layout_width="240dp"
        android:layout_height="150dp"
        android:layout_marginTop="48dp"
        android:elevation="2dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:background="@android:color/white"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <ImageView
        android:id="@+id/iv_sold_icon"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:adjustViewBounds="true"
        android:background="@color/colorPrimary"
        android:elevation="4dp"
        android:scaleType="fitXY"
        app:layout_constraintTop_toTopOf="@+id/iv_landscape_art"
        app:layout_constraintStart_toStartOf="@id/iv_landscape_art"
        app:layout_constraintEnd_toStartOf="@+id/iv_landscape_art"
        app:layout_constraintBottom_toTopOf="@+id/iv_landscape_art"/>

How do I accomplish that?

like image 593
devB78 Avatar asked Jan 12 '19 15:01

devB78


People also ask

How to place an image on top of another image?

Placing one image over another image is very easy with CSS. You can easily position image on top of another image using CSS. In this tutorial, we will show you how to position an image over image using HTML and CSS. The example HTML & CSS code snippets place one image over another image.

How to align an image in HTML?

Image alignment is used to move the image at different locations (top, bottom, right, left, middle) in our web pages. We use <img> align attribute to align the image. It is an inline element. left: It is used for the alignment of image to the left.

How do I overlay an image on another image in HTML?

Add two <img> elements with the following class names: “image1” and “image2”. Add a relative div placed in the flow of the page. Set the background image as relative so as the div knows how big it must be. Set the overlay as absolute, which will be relative to the upper-left edge of the first image.

How to move the image to the top left of screen?

The image is staying in the center top of the screen however. How can I move it to the top left relative to the screen? Thanks. Show activity on this post. You have centered the column, that's why everything in it is centered. Simply remove the top level Center () widget and your image will be aligned top left.


1 Answers

The simplest way to offset the smaller square from the top left corner of the larger square is to apply an X and Y translation. Add the following to the XML of the smaller square to see what I mean.

android:translationX="10dp"
android:translationY="10dp"

Another way to offset the smaller square is to place a Space widget in the top left corner of the larger square and make the sides of the Space 48dp in length. Now constrain the right and bottom sides of the smaller square to the right and bottom sides of the Space widget.

like image 53
Cheticamp Avatar answered Sep 19 '22 09:09

Cheticamp