How to use Scroll View In Android

3.3k Views Asked by At

I have use two scroll views for two different layout. And those two layouts are inside a Linear Layout.

Here is my XML file. I don't why ScrollView is not working for me

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="horizontal" >

    <LinearLayout
            android:id="@+id/flightResultData"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="horizontal" >

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

                        <ImageView
                            android:id="@+id/onewayflightLogo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="3dp"
                            android:src="@drawable/spicejet" />

                        <TextView
                            android:id="@+id/onewayflightName"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="SpiceJet" />

                        <TextView
                            android:id="@+id/onewayflightNumber"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="9W - 496" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_marginTop="8dp"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/onewayflightTime"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:minLines="1"
                            android:text="06:00 - 7:05"
                            android:textSize="12dp" />

                        <TextView
                            android:id="@+id/onewayflightDuration"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:minLines="1"
                            android:text="1h 35m | Non Stop"
                            android:textSize="10dp" />
                        <TextView
                            android:id="@+id/onewayflightAmount"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:minLines="1"
                            android:text="Rs 200000"
                            android:textSize="12dp" />
                    </LinearLayout>
                </LinearLayout>
                <View
                    android:layout_width="1dp"
                    android:layout_height="60dp"
                    android:background="@android:color/darker_gray" />
               <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="horizontal" >

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

                        <ImageView
                            android:id="@+id/retrunflightLogo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="3dp"
                            android:src="@drawable/spicejet" />

                        <TextView
                            android:id="@+id/retrunflightName"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="SpiceJet" />

                        <TextView
                            android:id="@+id/retrunflightNumber"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="9W - 496" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_marginTop="8dp"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/retrunflightTime"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:minLines="1"
                            android:text="06:00 - 7:05"
                            android:textSize="12dp" />

                        <TextView
                            android:id="@+id/retrunflightDuration"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:minLines="1"
                            android:text="1h 35m |Non Stop"
                            android:textSize="10dp" />

                        <TextView
                            android:id="@+id/retrunflightAmount"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:minLines="1"
                            android:text="Rs 200000"
                            android:textSize="12dp" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
</LinearLayout>

This is my main layout in which I am inflating my above xml dynamically through a loop

<LinearLayout
        android:id="@+id/flightResultData"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sortFlightLayouts"
        android:layout_marginLeft="8dp"
        android:orientation="vertical" >
</LinearLayout>

And in my activity code I am doing this:

void setTestResultData(){

    flightResult=(LinearLayout)findViewById(R.id.flightResultData);
    LinearLayout.LayoutParams flightDetailsLayout = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout.LayoutParams forUnderLine = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    forUnderLine.setMargins(0,0, 0, 0);
    for(int i=0;i < 13;i++){
        LinearLayout flightInformations=(LinearLayout)inflater.inflate(R.layout.flight_details_layout, null);
        flightResult.addView(flightInformations);
    }
}
2

There are 2 best solutions below

4
On

if you are display your scrollview in Horizontal Linear Layout and given a weight to scrollview then set width of Scrollview to "0dp"

android:layout_width="0dp"

and if it is vertical then

android:layout_height="0dp"

also set your main LinearLayout height to fill parent

2
On

I think you need a simple, two-column organization:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <!-- stuff goes here that should appear above the scrolling areas -->

    <ScrollView
        android:id="@+id/left_side_scroller"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <!-- contents of left side go here -->
    </ScrollView>

    <ScrollView
        android:id="@+id/right_side_scroller"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <!-- contents of right side go here -->
    </ScrollView>

    <!-- stuff goes here that should appear below  the scrolling areas -->
</LinearLayout>

Alternatively (and perhaps better) it looks like you should be using two ListView elements instead of two ScrollView elements. Each ListView would have the same layout parameters as shown above for the ScrollView. Since a ListView manages scrolling internally, you then don't need ScrollView at all.

Also, you probably want the entire layout to fill the screen, with the "filter" and "sort" elements always at the bottom. To achieve this effect,the top-level layout should have android:layout_height="fill_parent" instead of "wrap_content". Also, the scrollable areas should have android:layout_height="0dp" and a non-zero weight (which they already do).