user3133534
user3133534

Reputation: 59

Android ScrollView Not Scrolling Correctly

I've been making my way through thenewboston's android tutorials and I'm stuck on number 35. There is supposed to be a scroll bar that takes up a weight of 30 at the top of the app. There are 6 textview/edittext field groups in the code, but I deleted 5 for simplicity. My problem is that all of the text and text boxes appear on the page and are not confined to the given weight (they all just show up on the page). Has anyone else had this problem or know how to fix it?

Please, I'm new to stackoverflow. If you think this is a stupid question, please explain to me why and how I can ask it better.

Here is what it looks like: https://i.sstatic.net/cdQCE.jpg

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="30" >

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

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10" >
            </EditText>

        </LinearLayout>
    </ScrollView>

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

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>

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


    <AnalogClock
        android:id="@+id/analogClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

Upvotes: 1

Views: 4521

Answers (2)

Parth Mehrotra
Parth Mehrotra

Reputation: 3030

Welcome to StackOverflow!

Couple of things,

  1. TheNewBoston made a mistake. Whenever you use weights you need to set the height (or width) of that element to 0dp. I've changed that in your code.
  2. You shouldn't rely on the xml tool that eclipse has, you should always try to launch your app to some type of a device (emulator or preferably hardware)
  3. I think by ScrollBar, you mean ScrollView? Because you're not doing anything to control the appearance of the scroll bar right now.
  4. The parent of your layout (in this case) should match_parent, not wrap. (this is your main issue)
  5. Also, you don't really need weightSum, android will just sum all the weights anyway.

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="30" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />
    
            <EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10" >
            </EditText>
    
        </LinearLayout>
    </ScrollView>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="30"
        android:orientation="vertical" >
    
    
        <AnalogClock
            android:id="@+id/analogClock1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    

Upvotes: 3

Steve M
Steve M

Reputation: 9784

You need to set the height of the views to 0dp if you are using weight. match_parent is causing problems. Also set the height of root view to match_parent. Try this, if I add many EditText's to the LinearLayout in the ScrollView it seems to give the effect you are after:

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40">

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

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"/>


            <EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10">
            </EditText>

        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="30"
        android:orientation="vertical">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="30"
        android:orientation="vertical">


        <AnalogClock
            android:id="@+id/analogClock1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

Upvotes: 0

Related Questions