0

I am using an NestedScrollView for my layout to display a TextView as a header with a HorizontalScrollView to display slidable pictures.
Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="none"
    android:layout_weight="1">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:textStyle="bold"
            android:text="Cable Crunches"
            android:id="@+id/cableCrunches"
            android:gravity="center"
            android:padding="3dp"
            android:textSize="18sp"/>

        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/cableCrunches"
            android:id="@+id/cableCrunchesPics">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/cableCrunchesPics1"
                    android:src="@drawable/cable_crunches_1"/>

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/cableCrunchesPics2"
                    android:src="@drawable/cable_crunches_2"/>

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/cableCrunchesPics3"
                    android:src="@drawable/cable_crunches_3"/>

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/cableCrunchesPics4"
                    android:src="@drawable/cable_crunches_4"/>

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/cableCrunchesPics5"
                    android:src="@drawable/cable_crunches_5"/>

            </LinearLayout>
        </HorizontalScrollView>

    </RelativeLayout>

</android.support.v4.widget.NestedScrollView>

The TextView and the HorizontalScrollView block are used around 15 times in my layout and with this design I've got a OutOfMemoryError.
When I use less of those blocks the error doesn´t appear.

I've tried to minimize the size of my PNG files, but this doesn't solve the problem.
Is there any other layout I could use were not all ImageView are loaded at the start of the app?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Lars H.
  • 107
  • 1
  • 5

3 Answers3

2

You need to use RecyclerView or ListView with ImageView as row item instead of HorizontalScrollView. RecyclerView will create enough ImageViews to fill the screen. When the user scrolls it will reuse the existing ImageView's.

Also consider using an image loading library to load the images asynchronously. Glide or Picasso are really good libraries.

LordRaydenMK
  • 13,074
  • 5
  • 50
  • 56
  • There are also nice tutorials for that: [picasso](http://www.101apps.co.za/index.php/articles/android-recyclerview-and-picasso-tutorial.html) [glide](http://blog.grafixartist.com/image-gallery-app-android-studio-1-4-glide/) – Cem Philipp Freimoser Dec 21 '15 at 13:32
  • Thanks I use now a RecyclerView! :D – Lars H. Dec 26 '15 at 20:19
  • @LarsH. you are welcome. If this answer solves your problem you can accept it using the check-mark below answer score. – LordRaydenMK Dec 26 '15 at 20:26
0

add this line in your manifest in application tag

 android:largeHeap="true"

Maybe this will help you

And yes like others said. You can use different libraries to load images. I would recommend Universal image Loader. Link

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
  • This doesn't solve the OutOfMemoryError bug, all it means it will take a little while longer to run into the same issue! – Zain Dec 21 '15 at 13:40
0

you could use an library for loading the images. My favourite one is Glide it is simple to use https://github.com/bumptech/glide.

So you would load your images programmatically. You also gain more control over the how, when and where to load images. Glide also solved some memory issues in my App.