Home > Net >  How to add one image over other in Android
How to add one image over other in Android

Time:11-12

I want to make a view in which one image view, say iv_up should be placed above the another image view, say iv_down.

The iv_up should be slighty transparent. One more thing, the iv_down happens to be an QR Code.

Is there any way to implement this? Below is the code that I have written so far:

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

    <ImageView
        android:id="@ id/iv_down"
        android:layout_width="419dp"
        android:layout_height="419dp"
        android:layout_centerInParent="true"
        android:contentDescription="QR CODE"
        tools:srcCompat="@tools:sample/avatars"/>

   <ImageView
        android:id="@ id/iv_up"
        android:layout_width="419dp"
        android:layout_height="419dp"
        android:layout_centerInParent="true"
        android:contentDescription="QR CODE"
        tools:srcCompat="@tools:sample/avatars"/>

 

</RelativeLayout>

CodePudding user response:

So take a look at this sample XML I have created:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/iv_down"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_centerInParent="true"
            android:contentDescription="QR CODE"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:srcCompat="@tools:sample/avatars" />

    <ImageView
            android:id="@ id/iv_up"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_centerInParent="true"
            android:layout_marginStart="50dp"
            android:layout_marginTop="50dp"
            android:contentDescription="QR CODE"
            app:layout_constraintStart_toStartOf="@id/iv_down"
            app:layout_constraintTop_toTopOf="@id/iv_down"
            tools:srcCompat="@tools:sample/avatars" />

</androidx.constraintlayout.widget.ConstraintLayout>

This will create a UI where the iv_up appears on top of the iv_down. Using the ConstraintLayout you can position views relative to one another with ease - it is the "better version" of the RelativeLayout. So here we set the iv_down to be in the top left corner of the view, and iv_up to be 50dp from the top and start of the iv_down view. Since the views are ordered as such in the XML, the iv_up will always sit on top of the iv_down due to its Z positioning. You can change this by setting specific values for the android:translationZ attribute.

Simply setting the android:alpha="0.5" attribute on the iv_up view, will cause the view to be 50% transparent. You can play with this float value from 0-1 to get the desired look for the transparency. Changing the margins on iv_up will also change how far the view moves from the top left corner i.e. how much of the views overlap!

  • Related