0

I have the following problem. I am developing android app. My problem is the transition from "Portrait" to "Landscape" (transition from vertical to horizontal).

The "Portrait" display works correctly. Image appears distorted when "Landscape" is displayed. How can I solve this?

This is good...

orientationScreen 'Portrait'

and this is bad...

orientationScreen 'Landscape'

Here is my layout xml file:

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="center">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:src="@drawable/rsz_pprlogo"
        android:textSize="36sp" />
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">

        <ImageButton
            android:id="@+id/legislativa"
            android:onClick="OnButtonClick"
            android:src="@drawable/rsz_legislativa"
            android:
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:background="@android:color/transparent"
            android:padding="0dp"
            android:scaleType="fitXY"
            android:text="Button 1" />

        <ImageButton
            android:src="@mipmap/ic_launcher"
            android:text="Button 2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">

        <ImageButton
            android:src="@mipmap/ic_launcher"
            android:text="Button 3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:padding="0dp"
            android:gravity="center"/>

        <ImageButton
            android:src="@mipmap/ic_launcher"
            android:text="Button 4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">

        <ImageButton
            android:src="@mipmap/ic_launcher"
            android:text="Button 5"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"/>

        <ImageButton
            android:src="@mipmap/ic_launcher"
            android:text="Button 6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">

        <ImageButton
            android:src="@mipmap/ic_launcher"
            android:text="Button 7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"/>

        <ImageButton
            android:src="@mipmap/ic_launcher"
            android:text="Button 8"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"/>

    </LinearLayout>
</LinearLayout>

Is there any way to fix the 'Landscape' view? Thank You.

  • Does this answer your question? [Android: alternate layout xml for landscape mode](https://stackoverflow.com/questions/4858026/android-alternate-layout-xml-for-landscape-mode) – Ryan M Feb 06 '20 at 23:46

1 Answers1

0

You have an ImageButton with scaleType set on fitXY, and that's what is causing the strange effect. Just try to not use the scaleType, or try out different values, until you find the one that fits your needs the better. You can read more on scale types here: https://developer.android.com/reference/android/widget/ImageView.ScaleType

This is the ImageButton with the scaleType:

<ImageButton
        android:id="@+id/legislativa"
        android:onClick="OnButtonClick"
        android:src="@drawable/rsz_legislativa"
        android:
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@android:color/transparent"
        android:padding="0dp"
        android:scaleType="fitXY"
        android:text="Button 1" />

Also, there is a tag without any specification, it would be better to remove it. Lastly, I would suggest you to use a ConstraintLayout instead of nested LinearLayouts

Gregorio Palamà
  • 1,965
  • 2
  • 17
  • 22
  • Thank you for the tip. I will test your recommendation. @Gregorio Palama –  Feb 06 '20 at 23:50