Marc
Marc

Reputation: 1184

Align ImageView to top right of screen

I've a imageView in my layout which serves as a background which doesn't scale. But I need to have my image in the topright corner of the screen. I now have my image in the top left corner of the screen. This is my xml for the imageview:

    <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:src="@drawable/achtergrond"
android:scaleType="fitCenter" />

I also tried to put it in a linearlayout which has gravity="top|right" but that doesn't remove the white space around the picture because it's with the match_parent sizes. I also need to keep the scale of the picture, so I can't do fill_parent.

I hope someone can help me with this!

Upvotes: 7

Views: 30401

Answers (4)

gmartinsnull
gmartinsnull

Reputation: 1153

I'd like to point out that hcpl's answer is right although I wasn't being able to achieve that because I had set gravity of the RelativeLayout to Center so it was centralizing every child view to center. So if you who came here having the same issue please, pay attention to the parent view.

Upvotes: 0

osolmaz
osolmaz

Reputation: 1923

Maybe add

android:adjustViewBounds="true" 

to your ImageView? I had a similar problem.

Upvotes: 25

Hans Cappelle
Hans Cappelle

Reputation: 17495

The following layout will position your image (or any other view) at the top right corner of the screen. Check comments below code block for more details.

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

The important parts are the wrapping RelativeLayout and the layout_alignParentRightand layout_alignParentTopset to true on the view itself.

This won't wrap your complete view yet since it's all wrap_content. You can use different size resources per screen type or work with different layout_width and layout_height and then use the scaleType property as you whish (probably fitXY to scale image to match the view).

Don't use a LinearLayout if not necessary. Documentation on RelativeLayoutcan be found here.

Upvotes: 15

stinepike
stinepike

Reputation: 54672

with linearlayout to fit the imageview with the image use

android:scaleType="fitXY"

Upvotes: 0

Related Questions