Search code examples
androidhorizontalscrollview

My horizontalscrollview is stretching my imagebuttons when I use scale:fitxy


My xml works just fine on the android studios nexus 7 and nexus 10 emulators and the xml looks fine running on my nexus 4. But when I run it on a 7 inch tablet it stretches my image in the y direction only (in landscape activity) and doesn't keep the aspect ratio. I've tried 2 different 7 inch tablets with the same result.I think I've tried changing layoutwidth and layoutheight to just about every possible combination. I also have the same scaling issue if I try it with a imageview instead of an imagebutton. I googled and googled and tried everything but haven't found a solution that works for me. Please help me...

Here is my xml:

<?xml version="1.0" encoding="utf-8"?>


<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/horizontalScrollView"
android:scrollbars="none"
android:fillViewport="true">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:measureWithLargestChild="false">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/imageButton2"
        android:src="@drawable/mainmenu1"
        android:background="@android:color/transparent"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:onClick="goToLab101" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/imageButton3"
        android:src="@drawable/mainmenu2"
        android:background="@android:color/transparent"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:onClick="goToPeriodicTable" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/imageButton4"
        android:src="@drawable/mainmenu3"
        android:adjustViewBounds="true"
        android:background="@android:color/transparent"
        android:scaleType="fitXY"
        android:onClick="goToLab102"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/imageButton5"
        android:src="@drawable/fountainselector"
        android:background="@android:color/transparent"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/imageButton6"
        android:src="@drawable/mainmenu5comingsoon"
        android:background="@android:color/transparent"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:onClick="goToLab103"/>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/imageButton7"
        android:src="@drawable/mainmenu6"
        android:background="@android:color/transparent"
        android:adjustViewBounds="true"
        android:scaleType="fitXY" />
</LinearLayout>
</HorizontalScrollView>

Solution

  • Add your same image drawables in drawable-large-xhdpi for 7 inch tablet support