0

I'm having some problems with ScrollView and an ImageView.

What I want is:

I have an ImageView set always at the top of the screen. Then, I have an ScrollView where go all elements of the View, and inside it I have another ImageView which it is set at the bottom of the screen when previous elements don't fill screen (usually Big Screens), and it is set at the bottom of the ScrollView when exists Scroll.

It is ok for some other Activities I have, but I have some problems in one Activity that has a FrameLayout.

What I get is:

(See code below) When I set in the RelativeLayout of the ImageView (the one to stay at the bottom) height = "wrap_content":

  • In portrait orientation: The Image don't have its real size because it doesn't fit on the screen. To fit in it, Scroll should appear. -> I don't know why it doesn't appear like in other Activities.
  • In landscape orientation: ImageView appears at the bottom of the screen. As other elements don't fit on the screen, Scroll appears inside FrameLayout.

When I set in the RelativeLayout of the ImageView (the one to stay at the bottom) height = "200dp" (real height of image is smaller):

  • In portrait orientation: The Image doesn't appear. There isn't any Scroll (it is not necessary because other elements are seen).
  • In landscape orientation: Appear two Scrolls. One inside FrameLayout for elements, and another for seeing the ImageView.

Here is my code up to date:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    tools:context=".FacturasActivity"
    android:orientation="vertical" >

    <LinearLayout 
        android:id="@+id/linearLayoutLogoFactor_factura"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

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

    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollViewMain"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true" >

        <LinearLayout
            android:id="@+id/linearLayoutMain2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_gravity="center_horizontal"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:orientation="vertical" >

            <LinearLayout 
                android:id="@+id/linearLayoutDireccionFactura"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dp"
                android:layout_marginRight="25dp"
                android:layout_marginTop="10dp"
                android:weightSum="1.0">

                <TextView
                    android:id="@+id/textViewlabelDireccionFactura"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.3"
                    android:text="@string/etiqueta_direccion"
                    android:textSize="@dimen/textSize"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/textViewDireccion"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.7"
                    android:textSize="@dimen/textSize" />

            </LinearLayout>

            <FrameLayout
                android:id="@+id/frameLayoutFacturas"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dp"
                android:layout_marginRight="25dp"
                android:layout_marginTop="10dp" >

                <ListView
                    android:id="@android:id/list"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:id="@android:id/empty"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/facturas_vacia"
                    android:textSize="@dimen/textSize" />

            </FrameLayout>

            <RelativeLayout
                android:id="@+id/linearLayoutImagenInferior_main"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp" >

                <ImageView
                    android:id="@+id/imageViewImagenInferior_main"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:adjustViewBounds="true"
                    android:src="@drawable/la_electrica_de_las_empresas" />
            </RelativeLayout>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

Can you help me? Thanks in advance.

Lyd
  • 2,106
  • 3
  • 26
  • 33
  • I don't understand exactly what you say? but, i think you have problem with ListView inside ScrollView because both contain scrolling functionality default. so, you get both scrolling in Landscape mode and problem in smooth scroll for both. am i right? – Mr.Sandy Jul 12 '13 at 07:27
  • It is, and in some cases `ImageView` is not seen. Maybe it is due to that problem with `ListView` inside `ScrollView`? – Lyd Jul 12 '13 at 15:35
  • 1
    Your touch consider which scroll perform but, when you have different space for that. In your situation ListView and ScrollView take same space in small screen device. so, it will not detect that scrollView scrolling so,it performs ListView Scrolling.and your bottom Image hide in bottom area. – Mr.Sandy Jul 13 '13 at 05:54
  • For both scrolling manage check [this](http://stackoverflow.com/questions/15062365/how-to-work-when-listview-inside-the-scrollview) question useful for you. – Mr.Sandy Jul 13 '13 at 06:02
  • I didn't want both scrolling, but I've found my solution in a suggested question in a comment. Now there's only one scroll, the ones of `ScrollView`, like I wanted. Thank you very much again! – Lyd Jul 15 '13 at 07:52

1 Answers1

0

The solution I've found, looking at that question, is:

Create that function:

public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); 
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

and call it with my ListView. Only one Scroll (the one of ScrollView) is seen and used.

Community
  • 1
  • 1
Lyd
  • 2,106
  • 3
  • 26
  • 33