0

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);
Strawberry
  • 667
  • 2
  • 10
  • 24
  • Have you tried fixed size height? I think that layout height has to be fixed to make scrollbar actually appear. – Elvedin Hamzagic May 07 '15 at 11:32
  • @ElvedinHamzagic I cant be able to fix particular height . my app able to fit in all screen sizes – Strawberry May 07 '15 at 11:34
  • You try to given weight to ScrollView child but weight is not working under ScrollView so try to wrap ScrollView each child item and you already provider weight in xml so there is no need for again weight assignment at run time. – Haresh Chhelana May 07 '15 at 11:46
  • strawberry you can also set scrollview from xml so why you are doing it programatically... – Amitsharma May 07 '15 at 11:49
  • @amitsharma yes i have tried it once , experiencing same problem – Strawberry May 07 '15 at 13:42
  • strawberry do u know schroll work outside from LinearLayout or ScrollView always container for LinearLayout – Amitsharma May 08 '15 at 08:31

4 Answers4

2

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.

Bharathi
  • 21
  • 3
1

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?

Community
  • 1
  • 1
Elvedin Hamzagic
  • 825
  • 1
  • 9
  • 22
0

Set ScrollView's heigth to MATCH_PARENT.

Ircover
  • 2,406
  • 2
  • 22
  • 42
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – kapantzak May 07 '15 at 12:43
  • "Make scrollview in xml" - is critique. I sad how to make code work. @kapantzak – Ircover May 07 '15 at 13:31
0

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> 
Amitsharma
  • 1,577
  • 1
  • 17
  • 29
  • there is no need to do it programmatically simple lines are orintation vertical or add scrollview outside of LinearLayout – Amitsharma May 08 '15 at 08:39