2

I need a ScrollView which displays content in it's center if the screen is Big Enough, else it should display scroll.

Here is the example code that i used for testing

<ScrollView
    android:id="@+id/maincontent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/admob"
    android:layout_below="@id/app_bar">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="abc1\nabc2\nabc3\nabc4\nabc5\nabc6\nabc7\nabc8\nabc9\nabc10\nabc11\nabc12\nabc13\nabc14\nabc15\nabc16\nabc17\nabc18\nabc19\nabc20\nabc21"
            android:textSize="35sp"
            android:textStyle="bold"/>
    </LinearLayout>
</ScrollView>

My app needs to display in scrollview from abc1 to abc21. But instead it displays from abc 5 to abc21 with extra bottom space. check the screen shots attacked below:

here is the picture showing textview starting from abc5 instead of abc1: here is the picture showing textview starting from abc5 instead of abc1

here is the picture showing extra space below text view: here is the picture showing extra space below text view

The extra space measures correct to the missing parts in textview and also the problem only erases when scroll needs to happen (i.e if content fits in screen and when scroll is not needed-their was on problem, when content is more and scroll is needed then the problem comes).

Update 1 :- added some landscape images

Top Top Extra Space Extra Space End End

Update 2: Once I removed the layout_gravity="center" the problem is solved, But I need that attribute. Because my content is dynamic and when small content need to be displayed it should be displayed in the center instead of top(which happens when I remove that attribute) and when content is large it needs to display the scroll.

CodingSource
  • 203
  • 2
  • 15
Ashok Varma
  • 3,489
  • 3
  • 28
  • 43

2 Answers2

3

This solved my problem:-

just add android:fillViewport="true" to the scrollview, you can also remove layout_gravity attribute from it's child and yet you can get this case done.

for more details check :- http://pivotallabs.com/centering-a-view-within-a-scrollview/

Ashok Varma
  • 3,489
  • 3
  • 28
  • 43
0

This 2 atrrs solved problem :

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" //this attr
    android:layout_gravity="center"> //and this
</ScrollView>
Alireza Ghasemi
  • 560
  • 1
  • 4
  • 15