-2

This is my activity_main.xml code.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignTop="@+id/textView1"
    android:contentDescription="@string/walter_white"
    android:src="@drawable/walter1" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_below="@+id/imageView1"
    android:layout_marginLeft="14dp"
    android:layout_marginTop="21dp" >


<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/scrollView1"
    android:layout_alignLeft="@+id/imageView1"
    android:text="@string/break_bad" />
 </ScrollView>

</RelativeLayout>

I want to know whether my code is right or wrong and Please correct me . I want to know that my ScrollView works fine or not because I am not able to scroll the text in my emulator

Steph Sharp
  • 11,462
  • 5
  • 44
  • 81
Ashoka
  • 452
  • 1
  • 5
  • 20

5 Answers5

0

Everything you want to scroll needs to be inside your ScrollView definition. For Example:

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_below="@+id/imageView1"
    android:layout_marginLeft="14dp"
    android:layout_marginTop="21dp" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/scrollView1"
        android:layout_alignLeft="@+id/imageView1"
        android:text="@string/break_bad" />
</ScrollView>
Simulant
  • 19,190
  • 8
  • 63
  • 98
0

If you want to scroll the text in TextView with id "textView2" you should put it between the and tags.

Also: I usually don't set the height of a scrollview to "wrap_content" but define it in some other way, like "match_parent" or by aligning it with other Views around it. This makes sense because normally you know exactly how big you want your ScrollView to be, but you don't know the size of what you put in it. This is the reason you are probably using a ScrollView in the first place.

baske
  • 1,316
  • 9
  • 16
0

As of now, your ScrollView is empty (doesn't contain any text). If you want to be able to scroll, try something like this :

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_below="@+id/imageView1"
    android:layout_marginLeft="14dp"
    android:layout_marginTop="21dp" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/scrollView1"
        android:layout_alignLeft="@+id/imageView1"
        android:text="@string/break_bad" />
</ScrollView>

As ScrollView can only have one child, if you want to be able to scroll more than one View, the first child of ScrollView must be a ViewGroup that can have children itself, more details in this question.

Community
  • 1
  • 1
2Dee
  • 8,609
  • 7
  • 42
  • 53
0

You can do this in two ways:

One Way :

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_below="@+id/imageView1"
    android:layout_marginLeft="14dp"
    android:layout_marginTop="21dp"
    android:scrollbars="vertical" >


<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/scrollView1"
    android:layout_alignLeft="@+id/imageView1"
    android:text="@string/break_bad" />
 </ScrollView>

Second Way :

Put below line in TextView and remove scrollview..

android:scrollbars = "vertical"
Sanket
  • 3,094
  • 1
  • 16
  • 19
0

You need to pass LinearLayout with vertical orientation inside your ScrollView and set fixed ScrollView height. For Exmaple:

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_below="@+id/imageView1"
    android:layout_marginLeft="14dp"
    android:layout_marginTop="21dp" >

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

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/scrollView1"
        android:layout_alignLeft="@+id/imageView1"
        android:text="@string/break_bad" />

    </LinearLayout>
</ScrollView>
Roman Black
  • 3,501
  • 1
  • 22
  • 31