Strawberry
Strawberry

Reputation: 651

ScrollView goes out of view

I'm trying scrollview inside linearlayout , this linearlayout weight is 40% , but while adding more elements in scrollview , linearlayout expands it height not adding scrollbar inside the scrollview , Heres is my code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100"  
 >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="20" 
    android:orientation="vertical"/> 

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

   android:id="@+id/new_list1"
   android:orientation="vertical"
   android:weightSum="20"
   android:layout_weight="40" 
   ></LinearLayout>

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:id="@+id/list"
   android:orientation="vertical"
   android:layout_weight="40" 
   ></LinearLayout>
</LinearLayout>

MyActivityCode

       LinearLayout OuterList  = (LinearLayout)                   
      findViewById(R.id.list);

    LinearLayout OuterScroll = new LinearLayout(context);
    OuterScroll.setOrientation(LinearLayout.VERTICAL);

   for(int i = 0; i<= 10 ; i++)
   {
      LinearLayout childLayout = new LinearLayout(context);
      LinearLayout.LayoutParams trlpChildOuter = new 
      LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 
       LayoutParams.MATCH_PARENT,30f);
      OuterScroll.addView(childLayout,trlpChildOuter)

    }


    ScrollView scroll = new ScrollView(context);        
    LinearLayout.LayoutParams Params = new       
    LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,     
     LayoutParams.MATCH_PARENT,100f);
    scroll.addView(OuterScroll, Params);


    LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,10
0f);

    OuterList.addView(scroll, rps);

Upvotes: 0

Views: 107

Answers (4)

Amitsharma
Amitsharma

Reputation: 1578

Try by this .xml layout code to view as same or working code try by this code there is no need to do programmatically that.

  <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="match_parent"
    android:orientation="vertical" >

   <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    ></LinearLayout>

<ScrollView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:layout_weight="1"
    >

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

Upvotes: 0

Elvedin Hamzagic
Elvedin Hamzagic

Reputation: 855

As I understand, you want to fit your outer view somewhat inside phone height, and then you want some 10 views to fit in there with scrollbar if they overflow that outer view, right?

To do that your ScrollView has to be fixed height, e.g MATCH_PARENT. Because the page is divided before, all other outer view-s have fixed height already, and because the height is fixed the ScrollView will show scrollbar inside to make room for its content.

In your code that is rps param, which has to be:

LinearLayout.LayoutParams rps = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 100f);

Layout weight is not percentage value, but a float number, which express relative size of sibling elements. For example, put two views inside container, where first have weight 1.0 and second have weight 2.0, and the second will be twice as large as the first. Look What does android:layout_weight mean?

Upvotes: 1

Ircover
Ircover

Reputation: 2446

Set ScrollView's heigth to MATCH_PARENT.

Upvotes: 0

Bharathi
Bharathi

Reputation: 21

you can add that scrollview in xml file. why are you adding in Activity.java file.If you add in xml file it is working fine.I tried this one it is working for me.

Upvotes: 2

Related Questions