0

How do you create a layout with a screen-filling scrollview that has a small image in an imageview scaled to fit the whole scrollview and be scrollable? eg. A 720x1280 display has a full-screen scrollview (fill_parent). Inside is a linearlayout (fill_parent). In that is a 300x900 bitmap in an imageview that is upscaled to 720x2160, fills the screen width and exceeds the vertical bounds, and can be scrolled up/down in the scrollview.

The solution should work for all screen sizes to support multiple devices from mobiles to tablets.

Tickled Pink
  • 969
  • 2
  • 14
  • 26

2 Answers2

1

This works for me:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:layout_width="720dp"
            android:layout_height="2160dp"
            android:src="@drawable/image"
            android:scaleType="fitXY"/>

    </LinearLayout>

</ScrollView>

If you need to make this work for any screen size do it programmatically:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageView = (ImageView) findViewById(R.id.imageView);

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    imageView.getLayoutParams().width = width;
}
Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • That would, but that solution only works for a fixed screen size. If the same app is run on a 600 x 1024 display, or 320 x 480 display, the image won't scale. – Tickled Pink May 31 '13 at 13:38
  • 1
    You would need to scale the ImageView programmatically, or provide different layouts. The ImageView needs to have the correct width and height layout params given to it. These will be different for different screen sizes. – Ken Wolf May 31 '13 at 13:39
  • Okay. That sucks. Seems Android is built around doing everything programmatically. – Tickled Pink May 31 '13 at 13:42
  • Thanks. I'm targeting 2.2 so getSize() won't working, but I should be able to tweak that – Tickled Pink May 31 '13 at 13:49
  • Yes, use `getWindowManager().getDefaultDisplay().getWidth();` – Ken Wolf May 31 '13 at 13:51
0

if scroll view height and width is fill parent than if background is set with an small image it will automatically expand in your layout

anddevmanu
  • 1,459
  • 14
  • 19
  • That will scale the image to fit the screen, but not larger than the screen so it can be scrolled. I also want a buttonview after the imageview. – Tickled Pink May 31 '13 at 13:35
  • scrolling works but if you have no of elements thats total height greater than screen size and they are going out of screen – anddevmanu May 31 '13 at 13:42
  • My scrollview only contains an image. Having a background image won't scale it suitably. – Tickled Pink May 31 '13 at 13:44