'Clickable span in TextView problems with ScrollView

I have fragment with a textview with a list of clickable words. The textview it's contained into a relativelayout thant is scrollable with a scrollview. If i use a scrollview it's difficult to handle the click event on the clickable span, but if i remove the scrollview the click event works correctly.

this is the layout code

<ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:id="@+id/scroll1"
            android:scrollbars="vertical"
            android:fadingEdge="vertical">
    <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:padding="40dp">
    <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:id="@+id/sinonimiContainer"
                android:background="@drawable/layerlist"
                android:layout_centerHorizontal="true">
        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
                  android:id="@+id/titolo"
                  android:text="@string/titoloSinonimi"
                  android:textSize="60sp"
                  android:paddingBottom="25dp"
                  android:paddingTop="30dp"
                  android:paddingLeft="50dp"
                  android:textColor="@color/nero"/>
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                    android:id="@+id/txtSinonimi"
                    android:textColor="@color/nero"
                    android:textSize="27sp"
                    android:paddingBottom="30dp"
                    android:paddingLeft="30dp"
                    android:paddingRight="30dp"
                    android:clickable="true"
                    android:layout_below="@id/titolo" />

    </RelativeLayout>
    </RelativeLayout>
</ScrollView>

and this one is the clickable words inserted into the textview (txtsinonimi)

private void setParole(String parole)
{
    parole = parole.trim();
    Spannable spans;
    txtSinonimi.setText(parole, TextView.BufferType.SPANNABLE);
    spans = (Spannable) txtSinonimi.getText();

    String[] fields = parole.split(", ");

    for (String word: fields)
    {
        word = word.trim();
        if ( Character.isLetterOrDigit(word.charAt(0))) {
            ClickableSpan clickSpan = getClickableSpan(word);
            int index = parole.indexOf(word);
            spans.setSpan(clickSpan, index, index + word.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }

    txtSinonimi.setMovementMethod(LinkMovementMethod.getInstance());
}

/* evento click sulla parole/link */
private ClickableSpan getClickableSpan(final String word) {
    return new ClickableSpan() {
        final String mWord;
        {
            mWord = word;
        }

        @Override
        public void onClick(View widget)
        {
            Intent intent = new Intent(getActivity(), SearchResult.class);
            intent.putExtra(SearchManager.QUERY, mWord);
            startActivity(intent);
        }
    };
}

how i can use my textview with the clickable span inside the scrollview??



Solution 1:[1]

I had a similar issue. I had a TextView inside of ScrollView and ClickableSpan "attached" on that TextView. The ClickableSpan was responding on the click, but wouldn't highlight the part of the TextView on the click. I have resolved it by using:

android:focusableInTouchMode="true"

in the TextView.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marijana Gligoric