Kunal Shah
Kunal Shah

Reputation: 499

Position of imageview

I want to place an image at the right of the screen width minus 20 dp on a android device.

The pseudo code for this could be:

  1. Get Image from res/drawable folder.
  2. Set position of image - this has to be the screen width minus 20 dp,it should calculate the screen width and then set the position ( Is this possible in xml? )
  3. Render the image.

Parts 1 & 3 are clear, but the second part I guess is tough & is where I need some help.

Upvotes: 0

Views: 154

Answers (3)

Che Jami
Che Jami

Reputation: 5151

You can use a RelativeLayout as a container. Set android:layout_alignParentRight="true" and android:layout_marginRight="20dp" on an ImageView within this layout.

You can get more information here.

Upvotes: 2

waqaslam
waqaslam

Reputation: 68167

Yes, you can get it with xml too:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"    
    android:gravity="right"
    android:layout_marginRight="20dp"
    android:orientation="vertical" >

   <ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/icon" />

</LinearLayout>

Note that android:layout_marginRight="20dp" sets padding from right

Upvotes: 0

ASceresini
ASceresini

Reputation: 419

My understanding is that you will need to do this in a Java class where you get the dimensions of the window, do your calculations and then set the position of the image within the onCreate() Method.

Upvotes: 0

Related Questions