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
Solution 1:
Welcome to StackOverflow!
Couple of things,
- 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.
- 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)
- I think by ScrollBar, you mean ScrollView? Because you're not doing anything to control the appearance of the scroll bar right now.
- The parent of your layout (in this case) should match_parent, not wrap. (this is your main issue)
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>
Solution 2:
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>
Post a Comment for "Android ScrollView Not Scrolling Correctly"