matghazaryan
matghazaryan

Reputation: 5896

how to make LinearLayout scrollable without using Scrollview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/bag" 
    android:layout_height="fill_parent" android:layout_width="fill_parent"
    android:orientation="vertical" >

<RelativeLayout
        android:id="@+id/relativeLayout2"
            style="@style/relbag"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView style="@style/CodeFont" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="18dp"
        android:textSize="15dp"
        android:text="General Preference"
        android:id="@+id/genpref">
        </TextView>


    <ListView
        android:id="@+id/settingsListView1"
        style="@style/listbag"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="15dip"

        android:background="@drawable/radius"
        android:listSelector="@drawable/list_selector"
        android:paddingTop="8dip"
        android:paddingBottom="8dip" 
        android:scrollbars="none"
        android:layout_below="@id/genpref"/>

        <TextView style="@style/CodeFont" 
        android:id="@+id/notpref"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="25dp"
        android:textSize="15dp"
        android:text="Notification Preference"
        android:layout_below="@id/settingsListView1">
        </TextView>


        <ListView style="@style/listbag" android:id="@+id/settingsListView2" android:layout_width="fill_parent"
         android:layout_height="fill_parent"
            android:layout_marginLeft="15dip" android:layout_marginRight="15dip" 
              android:layout_marginBottom="15dip" 
        android:background="@drawable/radius" 
                android:paddingLeft="5dip" android:paddingRight="5dip" 
            android:paddingTop="15dip" android:paddingBottom="15dip" 
            android:listSelector="@drawable/list_selector" 
            android:layout_below="@id/notpref" 
            android:scrollbars="none"
            />
            </RelativeLayout>
</LinearLayout>

enter image description here

In my app I want create two listview into linearlayout and make linearlayout scrollable not using ScrollView. As far as I read scrollview has a issues working with listview. This picture isn't exactly what I want but let's pretend there is another listview below of listview. So I want display listview's all available items (in my app 2 listview's items) and make LinearLayout scrollable. ScrollView doesn't go fine because it has to have only one direct child. I can't find solution. So please help me make solution, Thanks

Upvotes: 3

Views: 9715

Answers (4)

Pirisok
Pirisok

Reputation: 401

i used this and helped me

ListAdapter listAdapter = listView.getAdapter();
int rows = listAdapter.getCount();
int height = 60 * rows; // Insert the general cell height plus the dividers.

ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = height;
listView.setLayoutParams(params);
listView.requestLayout();

hope help u too

Upvotes: 0

Nirav Dangi
Nirav Dangi

Reputation: 3647

I thnk here is your solution,,

you do not need to use ScrollView in LinearLayout.

Use layout_weight param in childviews (In your case both listview) of your LinearLayout

As layout_weight param is not available in RelativeLayout you need to change it with LinearLayout.

you can make like following,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/bag" 
    android:layout_height="fill_parent" android:layout_width="fill_parent"
    android:orientation="vertical" >

<LinearLayout
        android:id="@+id/relativeLayout2"
            style="@style/relbag"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    android:orientation="vertical">

        <TextView style="@style/CodeFont" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="18dp"
        android:textSize="15dp"
        android:text="General Preference"
        android:id="@+id/genpref">
        </TextView>


    <ListView
        android:id="@+id/settingsListView1"
        style="@style/listbag"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="15dip"
        android:background="@drawable/radius"
        android:listSelector="@drawable/list_selector"
        android:paddingTop="8dip"
        android:paddingBottom="8dip"/>

        <TextView style="@style/CodeFont" 
        android:id="@+id/notpref"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="25dp"
        android:textSize="15dp"
        android:text="Notification Preference">
        </TextView>


        <ListView style="@style/listbag" 
        android:id="@+id/settingsListView2" 
        android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
                android:layout_marginLeft="15dip" 
        android:layout_marginRight="15dip" 
                android:layout_marginBottom="15dip" 
            android:background="@drawable/radius" 
                android:paddingLeft="5dip" android:paddingRight="5dip" 
                android:paddingTop="15dip" android:paddingBottom="15dip" 
                android:listSelector="@drawable/list_selector"/>

     </LinearLayout>
</LinearLayout>

Change your xml with above.. i had not tested so their may be some silly error of params that you can solve easily..

Upvotes: 0

himanshu
himanshu

Reputation: 1980

I dont think its a good idea to put multiple lists in a single screen becoz touch will become weird and using screen will become complex.You should think something else to display multiple lists together in a single screen.You can horizontally arrange multiple lists in a single screen.Romain Guy(Google developer) is also accepting this fact in the following link....

http://groups.google.com/group/android-developers/browse_thread/thread/77acd4e54120b777

I hope this answer will help u to solve ur problem.

Upvotes: 7

SamSPICA
SamSPICA

Reputation: 1366

Use a LinearLayout as a child to the ScrollView, and place the ListView as a child to the LinearLayout.

Upvotes: 0

Related Questions