start android
start android

Reputation: 323

How to set these two different images using position in android?

I have two different images and would like to set them using different views but overlap slightly to match the below images. I also want to be able to set a different OnClickListener for each of them. I know with iOS I can set the view positions using x and y values but I'm not sure how to do this in Android.

How can I go about doing this?

Desired result:

enter image description here

Image One

enter image description here

Image Two

enter image description here

Upvotes: 3

Views: 489

Answers (2)

Emil Adz
Emil Adz

Reputation: 41099

Try set those two images as ImageView's and put them inside a FrameLayout. That way you can set them as you want, So they can overlap one another. That way you can set an clickable and onClick properties for both of them separately:

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

<ImageView
    android:id="@+id/iFirstImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@drawable/sort_image_1"
    android:clickable="true"
    android:onClick="setFirstImageClick"
    android:src="@drawable/sort_image_1" />

<ImageView
    android:id="@+id/iSecondImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@drawable/sort_image_2"
    android:clickable="true"
    android:onClick="setSecondImageClick"
    android:src="@drawable/sort_image_2" />

And create methods setFirstImageClick and setSecondImageClick in your activity to decide what each image click should do.

Upvotes: 2

adonal3
adonal3

Reputation: 268

You can place the first image however you want, and then in the xml for the second image use

android:layout_toRightOf="firstImageId"

You can set each onClick separately after that.

Upvotes: 0

Related Questions