Pregunta

I have a scrollview and a text view in it. I want to display the text in the textview scrollable.

XML FILE

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="500dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:src="@drawable/apple" />

    <ScrollView
        android:id="@+id/SonucScrollView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center" >
    </ScrollView>

    <TextView
        android:id="@+id/SonucTextView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/imageView1"
        android:editable="false"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:text="The apple is the pomaceous fruit of the apple tree, species Malus domestica in the rose family (Rosaceae). It is one of the most widely cultivated tree fruits, and the most widely known of the many members of genus Malus that are used by humans. Apples grow on small, deciduous trees. The tree originated in Central Asia, where its wild ancestor, Malus sieversii, is still found today. Apples have been grown for thousands of years in Asia and Europe, and were brought to North America by European colonists. Apples have been present in the mythology and religions of many cultures, including Norse, Greek and Christian traditions. In 2010, the fruit&apos;s genome was decoded as part of research on disease control and selective breeding in apple production.

There are more than 7,500 known cultivars of apples, resulting in a range of desired characteristics. Different cultivars are bred for various tastes and uses, including cooking, fresh eating and cider production. Domestic apples are generally propagated by grafting, although wild apples grow readily from seed. Trees are prone to a number of fungal, bacterial and pest problems, which can be controlled by a number of organic and non-organic means.

About 69 million tons of apples were grown worldwide in 2010, and China produced almost half of this total. The United States is the second-leading producer, with more than 6% of world production. Turkey is third, followed by Italy, India and Poland. Apples are often eaten raw, but can also be found in many prepared foods (especially desserts) and drinks. Many beneficial health effects are thought to result from eating apples; however, two forms of allergies are seen to various proteins found in the fruit.
Pests and diseases
Leaves with significant insect damage
Main article: List of apple diseases
See also: List of Lepidoptera that feed on Malus

Apple trees are susceptible to a number of fungal and bacterial diseases and insect pests. Many commercial orchards pursue an aggressive program of chemical sprays to maintain high fruit quality, tree health, and high yields. A trend in orchard management is the use of organic methods. These ban the use of some pesticides, though some older pesticides are allowed. Organic methods include, for instance, introducing its natural predator to reduce the population of a particular pest.

A wide range of pests and diseases can affect the plant; three of the more common diseases/pests are mildew, aphids and apple scab.

    Mildew: which is characterized by light grey powdery patches appearing on the leaves, shoots and flowers, normally in spring. The flowers will turn a creamy yellow color and will not develop correctly. This can be treated in a manner not dissimilar from treating Botrytis; eliminating the conditions which caused the disease in the first place and burning the infected plants are among the recommended actions to take.[54]
    Aphids: There are five species of aphids commonly found on apples: apple grain aphid, rosy apple aphid, apple aphid, spirea aphid and the woolly apple aphid. The aphid species can be identified by their color, the time of year when they are present and by differences in the cornicles, which are small paired projections from the rear of aphids.[54] Aphids feed on foliage using needle-like mouth parts to suck out plant juices. When present in high numbers, certain species reduce tree growth and vigor.[55]
    Apple scab: Apple scab causes leaves to develop olive-brown spots with a velvety texture that later turn brown and become cork-like in texture. The disease also affects the fruit, which also develops similar brown spots with velvety or cork-like textures. Apple scab is spread through fungus growing in old apple leaves on the ground and spreads during warm spring weather to infect the new year's growth.[56]

Among the most serious disease problems are fireblight, a bacterial disease; and Gymnosporangium rust, and black spot, two fungal diseases.[55] Codling moths and apple maggots are two other pests which affect apple trees. Young apple trees are also prone to mammal pests like mice and deer, which feed on the soft bark of the trees, especially in winter.[56]"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

APPLE.java CLASS

package com.example.fruitsinfo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

public class apple  extends Activity{
    ImageView apple;
    ScrollView sv;
    TextView tv;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.apple);
        apple=(ImageView) findViewById(R.id.imageView1);
        Log.d("asfdasf","apple class");
        sv=(ScrollView) findViewById(R.id.SonucScrollView);
        tv=(TextView) findViewById(R.id.SonucTextView);

    }

}

I have a scrollview and a text view in it. I want to display the text in the textview scrollable.

¿Fue útil?

Solución 2

The Scrollview will always need to have sub child as any Layout.thats why you need...

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="500dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
android:src="@drawable/apple"
            />

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/imageView1"

             >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
                <TextView
            android:id="@+id/SonucTextView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/imageView1"
            android:editable="false"
            android:scrollbarAlwaysDrawVerticalTrack="true"
            android:text="The apple is the pomaceous fruit of the apple tree, species Malus domestica in the rose family (Rosaceae). It is one of the most widely cultivated tree fruits, and the most widely known of the many members of genus Malus that are used by humans. Apples grow on small, deciduous trees. The tree originated in Central Asia, where its wild ancestor, Malus sieversii, is still found today. Apples have been grown for thousands of years in Asia and Europe, and were brought to North America by European colonists. Apples have been present in the mythology and religions of many cultures, including Norse, Greek and Christian traditions. In 2010, the fruit&apos;s genome was decoded as part of research on disease control and selective breeding in apple production.

    There are more than 7,500 known cultivars of apples, resulting in a range of desired characteristics. Different cultivars are bred for various tastes and uses, including cooking, fresh eating and cider production. Domestic apples are generally propagated by grafting, although wild apples grow readily from seed. Trees are prone to a number of fungal, bacterial and pest problems, which can be controlled by a number of organic and non-organic means.

    About 69 million tons of apples were grown worldwide in 2010, and China produced almost half of this total. The United States is the second-leading producer, with more than 6% of world production. Turkey is third, followed by Italy, India and Poland. Apples are often eaten raw, but can also be found in many prepared foods (especially desserts) and drinks. Many beneficial health effects are thought to result from eating apples; however, two forms of allergies are seen to various proteins found in the fruit.
    Pests and diseases
    Leaves with significant insect damage
    Main article: List of apple diseases
    See also: List of Lepidoptera that feed on Malus

    Apple trees are susceptible to a number of fungal and bacterial diseases and insect pests. Many commercial orchards pursue an aggressive program of chemical sprays to maintain high fruit quality, tree health, and high yields. A trend in orchard management is the use of organic methods. These ban the use of some pesticides, though some older pesticides are allowed. Organic methods include, for instance, introducing its natural predator to reduce the population of a particular pest.

    A wide range of pests and diseases can affect the plant; three of the more common diseases/pests are mildew, aphids and apple scab.

        Mildew: which is characterized by light grey powdery patches appearing on the leaves, shoots and flowers, normally in spring. The flowers will turn a creamy yellow color and will not develop correctly. This can be treated in a manner not dissimilar from treating Botrytis; eliminating the conditions which caused the disease in the first place and burning the infected plants are among the recommended actions to take.[54]
        Aphids: There are five species of aphids commonly found on apples: apple grain aphid, rosy apple aphid, apple aphid, spirea aphid and the woolly apple aphid. The aphid species can be identified by their color, the time of year when they are present and by differences in the cornicles, which are small paired projections from the rear of aphids.[54] Aphids feed on foliage using needle-like mouth parts to suck out plant juices. When present in high numbers, certain species reduce tree growth and vigor.[55]
        Apple scab: Apple scab causes leaves to develop olive-brown spots with a velvety texture that later turn brown and become cork-like in texture. The disease also affects the fruit, which also develops similar brown spots with velvety or cork-like textures. Apple scab is spread through fungus growing in old apple leaves on the ground and spreads during warm spring weather to infect the new year's growth.[56]

    Among the most serious disease problems are fireblight, a bacterial disease; and Gymnosporangium rust, and black spot, two fungal diseases.[55] Codling moths and apple maggots are two other pests which affect apple trees. Young apple trees are also prone to mammal pests like mice and deer, which feed on the soft bark of the trees, especially in winter.[56]"
            android:textAppearance="?android:attr/textAppearanceMedium" />

            </LinearLayout>


        </ScrollView>

    </RelativeLayout>

Add this 2 line to textview

android:maxLines="10"
            android:scrollbars="vertical"

And into java file

tv_desc = (TextView) findViewById(R.id.YOURTEXTID);
    scrollView1.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
                    // TODO Auto-generated method stub Log.v("PARENT",
                    // "PARENT TOUCH");

                    findViewById(R.id.tv_desc).getParent()
                            .requestDisallowInterceptTouchEvent(false);
        return false;
                }
            });

    tv_desc.setMovementMethod(new ScrollingMovementMethod());       
    tv_desc.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {

                    arg0.getParent().requestDisallowInterceptTouchEvent(true);
                    return false;
                }
            });

Otros consejos

Try to put your TextView inside ScrollView. You have not kept it inside Scrollview it seems in your code.

The Scrollview will always need to have sub child as any Layout. So you have to put some layout as a subchild of ScrollView and in that layout you can keep your TextView.

Try out as below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="500dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:src="@drawable/apple" />

    <ScrollView
        android:id="@+id/SonucScrollView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@+id/imageView1" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/SonucTextView"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:editable="false"
                android:scrollbarAlwaysDrawVerticalTrack="true"
                android:text="The apple is the pomaceous fruit of the apple tree, species Malus domestica in the rose family (Rosaceae). It is one of the most widely cultivated tree fruits, and the most widely known of the many members of genus Malus that are used by humans. Apples grow on small, deciduous trees. The tree originated in Central Asia, where its wild ancestor, Malus sieversii, is still found today. Apples have been grown for thousands of years in Asia and Europe, and were brought to North America by European colonists. Apples have been present in the mythology and religions of many cultures, including Norse, Greek and Christian traditions. In 2010, the fruit&apos;s genome was decoded as part of research on disease control and selective breeding in apple production.There are more than 7,500 known cultivars of apples, resulting in a range of desired characteristics. Different cultivars are bred for various tastes and uses, including cooking, fresh eating and cider production. Domestic apples are generally propagated by grafting, although wild apples grow readily from seed. Trees are prone to a number of fungal, bacterial and pest problems, which can be controlled by a number of organic and non-organic means.

About 69 million tons of apples were grown worldwide in 2010, and China produced almost half of this total. The United States is the second-leading producer, with more than 6% of world production. Turkey is third, followed by Italy, India and Poland. Apples are often eaten raw, but can also be found in many prepared foods (especially desserts) and drinks. Many beneficial health effects are thought to result from eating apples; however, two forms of allergies are seen to various proteins found in the fruit.
Pests and diseases
Leaves with significant insect damage
Main article: List of apple diseases
See also: List of Lepidoptera that feed on Malus

Apple trees are susceptible to a number of fungal and bacterial diseases and insect pests. Many commercial orchards pursue an aggressive program of chemical sprays to maintain high fruit quality, tree health, and high yields. A trend in orchard management is the use of organic methods. These ban the use of some pesticides, though some older pesticides are allowed. Organic methods include, for instance, introducing its natural predator to reduce the population of a particular pest.

A wide range of pests and diseases can affect the plant; three of the more common diseases/pests are mildew, aphids and apple scab.

    Mildew: which is characterized by light grey powdery patches appearing on the leaves, shoots and flowers, normally in spring. The flowers will turn a creamy yellow color and will not develop correctly. This can be treated in a manner not dissimilar from treating Botrytis; eliminating the conditions which caused the disease in the first place and burning the infected plants are among the recommended actions to take.[54]
    Aphids: There are five species of aphids commonly found on apples: apple grain aphid, rosy apple aphid, apple aphid, spirea aphid and the woolly apple aphid. The aphid species can be identified by their color, the time of year when they are present and by differences in the cornicles, which are small paired projections from the rear of aphids.[54] Aphids feed on foliage using needle-like mouth parts to suck out plant juices. When present in high numbers, certain species reduce tree growth and vigor.[55]
    Apple scab: Apple scab causes leaves to develop olive-brown spots with a velvety texture that later turn brown and become cork-like in texture. The disease also affects the fruit, which also develops similar brown spots with velvety or cork-like textures. Apple scab is spread through fungus growing in old apple leaves on the ground and spreads during warm spring weather to infect the new year&apos;s growth.[56]

Among the most serious disease problems are fireblight, a bacterial disease; and Gymnosporangium rust, and black spot, two fungal diseases.[55] Codling moths and apple maggots are two other pests which affect apple trees. Young apple trees are also prone to mammal pests like mice and deer, which feed on the soft bark of the trees, especially in winter.[56]"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>
    try this one..
    In your Layout of XML file try to put scrollview inside of the Textview..
    <ScrollView
            android:id="@+id/SonucScrollView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center" >

use Linear Layout in this place.

        <TextView
            android:id="@+id/SonucTextView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/imageView1"
     **android:singleLine="false"**//use this one your xml file
            android:editable="false"
            android:scrollbarAlwaysDrawVerticalTrack="true"
            android:text="The apple is the pomaceous fruit of the apple tree, species Malus domestica in the rose family (Rosaceae). It is one of the most widely cultivated tree fruits, and the most widely known of the many members of genus Malus that are used by humans. Apples grow on small, deciduous trees. The tree originated in Central Asia, where its wild ancestor, Malus sieversii, is still found today. Apples have been grown for thousands of years in Asia and Europe, and were brought to North America by European colonists. Apples have been present in the mythology and religions of many cultures, including Norse, Greek and Christian traditions. In 2010, the fruit&apos;s genome was decoded as part of research on disease control and selective breeding in apple production.

    There are more than 7,500 known cultivars of apples, resulting in a range of desired characteristics. Different cultivars are bred for various tastes and uses, including cooking, fresh eating and cider production. Domestic apples are generally propagated by grafting, although wild apples grow readily from seed. Trees are prone to a number of fungal, bacterial and pest problems, which can be controlled by a number of organic and non-organic means.

    About 69 million tons of apples were grown worldwide in 2010, and China produced almost half of this total. The United States is the second-leading producer, with more than 6% of world production. Turkey is third, followed by Italy, India and Poland. Apples are often eaten raw, but can also be found in many prepared foods (especially desserts) and drinks. Many beneficial health effects are thought to result from eating apples; however, two forms of allergies are seen to various proteins found in the fruit.
    Pests and diseases
    Leaves with significant insect damage
    Main article: List of apple diseases
    See also: List of Lepidoptera that feed on Malus

    Apple trees are susceptible to a number of fungal and bacterial diseases and insect pests. Many commercial orchards pursue an aggressive program of chemical sprays to maintain high fruit quality, tree health, and high yields. A trend in orchard management is the use of organic methods. These ban the use of some pesticides, though some older pesticides are allowed. Organic methods include, for instance, introducing its natural predator to reduce the population of a particular pest.

    A wide range of pests and diseases can affect the plant; three of the more common diseases/pests are mildew, aphids and apple scab.

        Mildew: which is characterized by light grey powdery patches appearing on the leaves, shoots and flowers, normally in spring. The flowers will turn a creamy yellow color and will not develop correctly. This can be treated in a manner not dissimilar from treating Botrytis; eliminating the conditions which caused the disease in the first place and burning the infected plants are among the recommended actions to take.[54]
        Aphids: There are five species of aphids commonly found on apples: apple grain aphid, rosy apple aphid, apple aphid, spirea aphid and the woolly apple aphid. The aphid species can be identified by their color, the time of year when they are present and by differences in the cornicles, which are small paired projections from the rear of aphids.[54] Aphids feed on foliage using needle-like mouth parts to suck out plant juices. When present in high numbers, certain species reduce tree growth and vigor.[55]
        Apple scab: Apple scab causes leaves to develop olive-brown spots with a velvety texture that later turn brown and become cork-like in texture. The disease also affects the fruit, which also develops similar brown spots with velvety or cork-like textures. Apple scab is spread through fungus growing in old apple leaves on the ground and spreads during warm spring weather to infect the new year's growth.[56]

    Among the most serious disease problems are fireblight, a bacterial disease; and Gymnosporangium rust, and black spot, two fungal diseases.[55] Codling moths and apple maggots are two other pests which affect apple trees. Young apple trees are also prone to mammal pests like mice and deer, which feed on the soft bark of the trees, especially in winter.[56]"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </ScrollView>

just write your

TextView

inside

ScrollView

tag

<ScrollView
    android:id="@+id/SonucScrollView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center" >

  <TextView android:id="@+id/SonucTextView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"/>
</ScrollView>

Put your TextView inside ScrollView in XML like:

 <ScrollView
            android:id="@+id/SonucScrollView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center">

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent" >

           <TextView
            android:id="@+id/SonucTextView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/imageView1"
            android:editable="false"
            android:scrollbarAlwaysDrawVerticalTrack="true"
            android:text="The apple is the pomaceous fruit of the apple tree, species Malus domestica in the rose family (Rosaceae). It is one of the most widely cultivated tree fruits, and the most widely known of the many members of genus Malus that are used by humans. Apples grow on small, deciduous trees. The tree originated in Central Asia, where its wild ancestor, Malus sieversii, is still found today. Apples have been grown for thousands of years in Asia and Europe, and were brought to North America by European colonists. Apples have been present in the mythology and religions of many cultures, including Norse, Greek and Christian traditions. In 2010, the fruit&apos;s genome was decoded as part of research on disease control and selective breeding in apple production.

    There are more than 7,500 known cultivars of apples, resulting in a range of desired characteristics. Different cultivars are bred for various tastes and uses, including cooking, fresh eating and cider production. Domestic apples are generally propagated by grafting, although wild apples grow readily from seed. Trees are prone to a number of fungal, bacterial and pest problems, which can be controlled by a number of organic and non-organic means.

    About 69 million tons of apples were grown worldwide in 2010, and China produced almost half of this total. The United States is the second-leading producer, with more than 6% of world production. Turkey is third, followed by Italy, India and Poland. Apples are often eaten raw, but can also be found in many prepared foods (especially desserts) and drinks. Many beneficial health effects are thought to result from eating apples; however, two forms of allergies are seen to various proteins found in the fruit.
    Pests and diseases
    Leaves with significant insect damage
    Main article: List of apple diseases
    See also: List of Lepidoptera that feed on Malus

    Apple trees are susceptible to a number of fungal and bacterial diseases and insect pests. Many commercial orchards pursue an aggressive program of chemical sprays to maintain high fruit quality, tree health, and high yields. A trend in orchard management is the use of organic methods. These ban the use of some pesticides, though some older pesticides are allowed. Organic methods include, for instance, introducing its natural predator to reduce the population of a particular pest.

    A wide range of pests and diseases can affect the plant; three of the more common diseases/pests are mildew, aphids and apple scab.

        Mildew: which is characterized by light grey powdery patches appearing on the leaves, shoots and flowers, normally in spring. The flowers will turn a creamy yellow color and will not develop correctly. This can be treated in a manner not dissimilar from treating Botrytis; eliminating the conditions which caused the disease in the first place and burning the infected plants are among the recommended actions to take.[54]
        Aphids: There are five species of aphids commonly found on apples: apple grain aphid, rosy apple aphid, apple aphid, spirea aphid and the woolly apple aphid. The aphid species can be identified by their color, the time of year when they are present and by differences in the cornicles, which are small paired projections from the rear of aphids.[54] Aphids feed on foliage using needle-like mouth parts to suck out plant juices. When present in high numbers, certain species reduce tree growth and vigor.[55]
        Apple scab: Apple scab causes leaves to develop olive-brown spots with a velvety texture that later turn brown and become cork-like in texture. The disease also affects the fruit, which also develops similar brown spots with velvety or cork-like textures. Apple scab is spread through fungus growing in old apple leaves on the ground and spreads during warm spring weather to infect the new year's growth.[56]

    Among the most serious disease problems are fireblight, a bacterial disease; and Gymnosporangium rust, and black spot, two fungal diseases.[55] Codling moths and apple maggots are two other pests which affect apple trees. Young apple trees are also prone to mammal pests like mice and deer, which feed on the soft bark of the trees, especially in winter.[56]"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>
        </ScrollView>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top