Reputation: 3443
I have a layout with a ImageView
that I want to keep similar in landscape and portrait orientations, such as this figure:
I'm using two similar layouts (layout and layout-land). In the second layout, I add the android:rotation
parameter such in code below, but it resulted in this figure:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rotation="90"
android:scaleType="fitXY"
android:src="@drawable/background_agogo" />
Is it possible to rotate the image view using only xml layout file? How could I do it?
Upvotes: 3
Views: 5418
Reputation: 8372
Try adding android:configChanges="orientation|screenSize"
to your activities xml in your AndroidManifest.xml
Then in your Fragment/Activity, whatever contains the ImageView, override public void onConfigurationChanged(Configuration newConfig)
and in there you can check if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
and then you could rotate your ImageView programmatically image.setRotation(90)
Note that setRotation requires API 11 or higher. Otherwise refer to this answer for how to rotate programmatically: https://stackoverflow.com/a/10104318/4898635
For more information on handling runtime changes like orientation, check this out: http://developer.android.com/guide/topics/resources/runtime-changes.html
Upvotes: 2
Reputation: 751
Instead of using
android:src = "@drawable/background_agogo"
use
android:background="@drawable/background_agogo"
The image should stretch to fit the imageview when using the background attribute.
Upvotes: 0