3

I have a LinearLayout used by my Main Activity that looks as follows:

<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="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    tools:context=".MainActivity" >

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

</LinearLayout>

I am dynamically adding fragments inside activity_main_container but the problem is, when my fragment height is larger than the screen height, the content at the bottom gets hidden. I am not able to scroll or do anything.

I have tried wrapping the fragment's layout inside ScrollView but it won't work for me because I have listview inside my fragments. I've also tried setting layout height to say 3000dp on every container but it didn't work either.

My fragment's sample layout that I am dynamically adding looks as follows. I am adding contents dynamically inside my fragment as well.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/fragment_listing_detail_linearLayout_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFF"
        android:orientation="vertical"
        android:paddingLeft="@dimen/fragment_padding_side"
        android:paddingRight="@dimen/fragment_padding_side" >

        <TextView
            android:id="@+id/fragment_listing_detail_textview_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Review Title"
            android:textSize="20sp"
            android:textStyle="bold" />

        <RatingBar
            android:id="@+id/fragment_listing_detail_ratingbar_rating"
            style="@style/customRatingBarSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="5"
            android:stepSize="0.5"
            android:isIndicator="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Photos" />

        <View style="@style/divider" />         

        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:paddingBottom="10dp" >

            <LinearLayout
                android:id="@+id/fragment_listing_detail_linearlayout_images"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

            </LinearLayout>
        </HorizontalScrollView>

        <TextView
            android:id="@+id/fragment_listing_detail_textview_review"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Reviews" />

        <View style="@style/divider" />         

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

            <ImageView
                android:layout_width="match_parent"
                android:paddingTop ="7dp"
                android:paddingBottom="7dp"
                android:layout_height="wrap_content"
                android:contentDescription="review divisor"
                android:src="@android:drawable/divider_horizontal_bright" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/fragment_listing_detail_reviews_fragment_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>

       <Button
            android:id="@+id/fragment_listing_detail_button_add_review"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Review" />

       <ImageView
           android:id="@+id/fragment_listing_detail_imageview_testimage"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />

    </LinearLayout> 

How do I make it so that when my fragment goes beyond screen height, I can still scroll to see the bottom views? Thanks.

c 2
  • 1,127
  • 3
  • 13
  • 21
  • Have you tried to set LinearLayout property android:layout_height="match_parent" ? – yushulx Sep 17 '13 at 01:46
  • Tried on both outermost one and my fragment's linearlayout. didn't work. – c 2 Sep 17 '13 at 01:48
  • "I have tried wrapping the fragment's layout inside ScrollView ", does this mean wrapping the root `LinearLayout` that you currently have inside of the `ScrollView`? – codeMagic Sep 17 '13 at 01:51
  • yup I've tried. But I have ListViews inside my layout so they simply collapse instead of expanding. Is scrollview the only option? – c 2 Sep 17 '13 at 01:59
  • The way I see it `ScrollView` is what you need. But without knowing what the layout actually looks like its hard to say if there might be another design option – codeMagic Sep 17 '13 at 02:01
  • ya I think ScrollView is the way to go. I've decided to replace listview inside my fragment with linearlayout instead. – c 2 Sep 17 '13 at 02:11
  • Maybe it is but if you have a sketch of how these elements should be then maybe we can help find a different way...if `ScrollView` is bad for what you want – codeMagic Sep 17 '13 at 02:33

1 Answers1

6

I believe you would want the ScrollView to be the root View. Try this out for size:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 tools:context=".MainActivity"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fillViewPort="true" >

<!-- ScrollViews can only have 1 child View, so the 1st LinearLayout below
     will be the "container" -->

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

<!-- This is where you want to put everything scrollable -->

</LinearLayout>
</ScrollView>

I hope this helps, Happy Coding!

MattMatt
  • 905
  • 6
  • 19
  • ya seems like ScrollView is the way to go. Probably not at the root, but just within the fragment's layout. There's a syntax error (extra slash) in your xml btw. – c 2 Sep 17 '13 at 02:12
  • Please note that placing a ListView inside a ScrollView is not recommended. – Vikram Sep 17 '13 at 02:21
  • Sorry, just typed it out. Thanks for pointing that out. I've personally never had a problem with `ScrollView` being the root View, but to each their own @user2558882 where are you getting a `ListView` from? – MattMatt Sep 17 '13 at 02:26
  • 1
    From the question: `I have tried wrapping the fragment's layout inside ScrollView but it won't work for me because I have listview inside my fragments`. – Vikram Sep 17 '13 at 02:29
  • My apologies, This is all I can think of – MattMatt Sep 17 '13 at 02:41
  • @c 2: If you want listview inside scrollview then you have to giveaway listview recycle feature,it is supposed to be very bad UI design but still :http://stackoverflow.com/questions/10709411/android-listview-rows-in-scrollview-not-fully-displayed-clipped. Why it is bad ? : http://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing – Pankaj Sep 17 '13 at 07:10
  • use android:layout_height="wrap_content" for ScrollView – Defuera Aug 22 '14 at 12:03