Elad Benda2
Elad Benda2

Reputation: 15482

how to put an image in center above another image in android?

I want to create a layout like this:

enter image description here

I have 3 different images: the background, the white square and the V sign.

How can I position them as in the photo, if I want the box to move from side to side

and the V sign to fade in\out when onClick is triggered.

I have tried:

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:background="@drawable/off_background">


             <ImageView
            android:id="@+id/handle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
             android:src="@drawable/handle"
            android:scaleType="centerInside" />

    <ImageView android:id="@+id/switch_v"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/switch_v"
        android:layout_gravity="center"
        android:scaleType="centerInside"/>



</RelativeLayout>

which gives me the order correctly, but not posed correctly:

enter image description here

Upvotes: 1

Views: 1728

Answers (2)

ramaral
ramaral

Reputation: 6179

Create a drawable for the the white square and the V sign:

white_square_and_V_sign.xml

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
        <bitmap
            android:src="@drawable/handle"/>
    </item>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/switch_v"/>
    </item>  
</layer-list>  

Create a drawable for the the white square:

white_square.xml

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
        <bitmap
            android:src="@drawable/handle"/>
    </item>
</layer-list>  

Create a Selector:

mySelector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/white_square"
          android:state_pressed="true" />
    <item android:drawable="@drawable/white_square_and_V_sign" />
</selector>  

Use it in your layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:background="@drawable/off_background" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/mySelector"/>

</RelativeLayout>

Upvotes: 0

Majid Daeinejad
Majid Daeinejad

Reputation: 1037

You have to use layout_align for this issue. I have tested it for your example and it worked correctly.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/off_background" >

<ImageView
    android:id="@+id/handle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:scaleType="centerInside"
    android:src="@drawable/handle" />

<ImageView
    android:id="@+id/switch_v"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/handle"
    android:layout_alignTop="@id/handle"
    android:layout_alignBottom="@id/handle"
    android:layout_alignRight="@id/handle"
    android:layout_gravity="center"
    android:scaleType="centerInside"
    android:src="@drawable/switch_v" />
</RelativeLayout>

Upvotes: 2

Related Questions