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 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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With