39

Im having a problem where the android scrollview starts hiding a pair of textviews I have at the top of my layout, I have found another person on this very site who had that problem and was able to get help unfortunately the person who helped them didnt actually say what fixes it, can anyone tell me what was the fix here?

android scrollview hiding top content

any help would be huge here is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/title_color_dark_transparent" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical|center_horizontal|center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="5dp"
        android:orientation="vertical"
        android:paddingTop="5dp" >

        <TextView
            android:id="@+id/saysomething"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/loading"
            android:textColor="#FFFFFF"
            android:textSize="30dp" />

        <TextView
            android:id="@+id/saysomethinginfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:text="@string/loading"
            android:textColor="#FFFFFF"
            android:textSize="17dp" />

        <EditText
            android:id="@+id/tweetedittext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/edit_text"
            android:gravity="top"
            android:hint="@string/edittext_hint"
            android:inputType="textCapSentences|textMultiLine|textAutoCorrect|textAutoComplete"
            android:lines="3"
            android:textAppearance="@style/TextAppearance.EditText"
            android:textSize="17dp" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/photobutton"
                style="@style/TextAppearance.Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/button_background"
                android:onClick="photobuttonClicked"
                android:text="@string/photobuttontext"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/charactersremaining"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_centerInParent="true"
                android:layout_toLeftOf="@+id/posttweetbutton"
                android:text="@string/characters"
                android:textColor="#FFFFFF"
                android:textSize="20dp" />

            <Button
                android:id="@id/posttweetbutton"
                style="@style/TextAppearance.Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginLeft="2dp"
                android:background="@drawable/button_background"
                android:onClick="posttweetbuttonClicked"
                android:text="@string/postbuttonstext"
                android:textSize="15dp" />
        </RelativeLayout>
    </LinearLayout>

    <TextView
        android:id="@+id/photodetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#FFFFFF"
        android:textSize="17dp"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp" />

    <ImageView
        android:id="@+id/iv_pic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp" />
</LinearLayout>

</ScrollView>
Community
  • 1
  • 1
Edmund Rojas
  • 6,376
  • 16
  • 61
  • 92
  • 1
    I fail to see any TextViews above your ScrollView. You're also missing your closing tag on your ScrollView. – kabuko Mar 28 '12 at 22:14
  • seems like I mistakenly didnt comment in the closing tag for the scrollview but its there now, the textviews are the first ones that you see inside the linear layout, placing the textviews outside the linear layout gives me an error because apparently you cant have more than one child in the scrollview – Edmund Rojas Mar 28 '12 at 22:19
  • I thought you don't want your ScrollView to hide those TextViews? Maybe you should explain what you actually want the layout to look like, because it's not very clear to me. – kabuko Mar 28 '12 at 22:20
  • the layout is too big for the screen, so I put it in a scrollview, but when I set the image in the imageview the 2 textviews "@+id/saysomething" and "@+id/saysomethinginfo" are hidden and cant be scrolled to see what it says, what I need is for the user to be able to scroll up and be able to see whats hidden under there – Edmund Rojas Mar 28 '12 at 22:28
  • Might this be of help http://stackoverflow.com/questions/8280051/black-screen-using-scrollview-relativelayout-and-a-costum-view-acting-like-a/9095958#9095958 – Nikola Despotoski Mar 28 '12 at 22:36
  • unfortunately this also did not help :/ – Edmund Rojas Mar 28 '12 at 23:01
  • You code works fine its showing text. Can u please take screen shot of your layout and posted it?since it help us to understand what you needed.. – deepa Mar 29 '12 at 04:33
  • I figured out the problem, turns out nothing was actually wrong but when the emulator launches the screen with the scrollview they actually simulate the keyboard being activated which causes the top elements to be hidden the way that it was, I was able to figure this out by testing the app on an actual device, so thankfully impact seems to be averted hopefully this helps any one else who has this perceived problem lol – Edmund Rojas Mar 30 '12 at 22:43

5 Answers5

116

Found it ! It's the android:layout_gravity="center" in the LinearLayout that is the culprit. Just delete this and everything should be fine.

Sylphe
  • 1,553
  • 1
  • 12
  • 13
  • Thanks for this - worked for me in an instance of this problem. – Rob Pridham Jan 31 '13 at 16:20
  • 6
    Works, and if you still need to center the View inside use this workaround: http://pivotallabs.com/centering-a-view-within-a-scrollview/ – Takhion Apr 25 '13 at 17:09
  • Big thank you @Sylphe, strange that your answer not accepted :( – Henadzi Rabkin Jul 07 '13 at 01:28
  • Many thanks for this. I had a similar problem but in a `HorizontalScrollView`. Shouldn't this be filed as a bug on Google Code? At the very least, lint should be added to warn of this perhaps. – darrenp Dec 10 '13 at 12:31
  • removing 'android:layout_gravity="center"' is helped for me! Thanks guy! :)))))))))))) – nAkhmedov Nov 08 '14 at 08:01
  • I had this issue, but I needed to move many of the layout parameters that I used to have on the top level linearlayout to the now top level scrollview. – dazza5000 Apr 01 '16 at 14:45
9

Have you tried setting

android:fillViewport="true"

in the ScrollView?

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:fillViewport="true"
  android:background="@color/title_color_dark_transparent" >
dwbrito
  • 5,194
  • 5
  • 32
  • 48
2

You can scroll programmatically.

  1. Remove android:layout_gravity property from xml
  2. Add scrolling to onResume function

Some code from my project:

protected void onResume() {
    super.onResume();
    final HorizontalScrollView svInMenu = (HorizontalScrollView) findViewById(R.id.svInMenu);
    ViewTreeObserver vto = svInMenu.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            svInMenu.scrollTo(svInMenu.getRight() / 4, 0);
        }
    });
}
0

Add android:layout_weight="1" to your scrollview. It will resolve the problem. Something like this

 <ScrollView
        android:id="@+id/scroll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_weight="1" >
Joseph Selvaraj
  • 2,225
  • 1
  • 21
  • 21
0

Your outer LinearLayout has its height set to match_parent. It should be wrap_content instead. The inner LinearLayout should also have height wrap_content.

kabuko
  • 36,028
  • 10
  • 80
  • 93