Question

OK. Well, although I use this site a lot, this is actually my first question.

Although I have many years experience in programming using Visual Studio, I have recently started to program for Android (using Android Studio) and I'm stuck on image positioning.

What I have is an image in an ImageView which automatically scales (as you would expect) to the orientation of the screen and super imposed on this are ImageButtons.

Currently, I am using automatic positioning to the ImageView for the ImageButtons but what I'm after is to position the ImageButtons exactly to the upper right corner of the (scaled) image, no matter it's size or position.

I can get the (default) size of the image but what I need to retrieve is the scaled size and x:y co-ordinates and then adjust setTop() and setRight() of the ImageButton accordingly.

I have read loads of examples on here but I don't seem to make any of them do what I'm looking for. Is there a simple method for getting the size / position of the displayed / scaled image (where actual image pixels are displayed), or is this something I would have to manually do by creating my own function?

Thanks in advance.

Correct Landscape:

enter image description here

Incorrect Landscape:

enter image description here

Correct Portrait:

enter image description here

Incorrect Portrait:

enter image description here

Was it helpful?

Solution

I don't understand the problem... but i'll try to make and answer for that.

when you use imageView you can set this attributes

scaleType="fitXY"
adjustBounds="true"

That will center your image view and resize to XY if is necessary. But please remember that you need to use src="" and not setbackground in the image view.

Well with this clear we'll proceed with RelativeLayout

Make a parent for example a FrameLayout, inside make a relative layout this (nesting is not necessary but for now we'll use it).

<RelativeLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <ImageButton 
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_add"/>
    <ImageView 
        android:layout_width="match_parent"
        android:layout_width="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@drawable/ic_add"
        />
</RelativeLayout>

i don't test it but thats the main idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top