Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Multiple clickable strings in textview

I am creating a small Android app. I would like to display a text in a textview with multiple parts to click on. (Each should show some different message)

Finally I managed to find out how to display mutiple spans in one textview, however unfortunately the onClick methods don't work. Simply nothing happens, not even a logcat line.

I have something like this:

SpannableStringBuilder ssb=new SpannableStringBuilder();
ssb.append("first  second")

ssb.setSpan(new ClickableSpan() {
    @Override
    public void onClick(View v) {
    //Eredmeny2.this is just the context, name of the whole class
    Toast.makeText(Eredmeny2.this, "first", Toast.LENGTH_LONG).show();
    }
}, 1, 3, 0);

ssb.setSpan(new ClickableSpan() {
    @Override
    public void onClick(View v) {
    Toast.makeText(Eredmeny2.this, "second", Toast.LENGTH_LONG).show();
    }
}, 7, 10, 0);

TextView t1=new TextView(this);
t1.setText(ssb);
...

The text is underlined fine, but nothing happens when i click them. It is a part of a TableView, although I do not think this is relevant. Do you have any ideas why it does not do anything? What do I miss? Or should I do it on some totally different way?

Thanks in advance.


The layout file this part would use is the following:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
    android:id="@+id/ScrollView01"
    android:background="#FF0000">


    <TableLayout
        android:id="@+id/TableLayout01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:stretchColumns="0"
        android:showDividers="middle"
        android:padding="3dp">
    <TableRow
        android:id="@+id/TableRow01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="3dp"
            android:background="#000000"
            android:textColor="#FFFFFF"
            android:padding="6dp"
            android:text="Hour"
            android:textSize="20sp"
            android:textStyle="bold" >

        </TextView>
        <TextView android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textStyle="bold"
            android:text="Minute"
            android:padding="6dp"
            android:textColor="#FFFFFF"
            android:background="#000000">
        </TextView>

    </TableRow>

    </TableLayout>


</ScrollView>

And the TextView layout that the TextView uses directly is the following:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:background="#000000"
android:textIsSelectable="false"
android:textColor="#FFFFFF">
</TextView>
like image 244
skandigraun Avatar asked May 12 '13 18:05

skandigraun


People also ask

How do I make part of a TextView clickable?

In Android, the most common way to show a text is by TextView element. The whole text in the TextView is easy to make clickable implementing the onClick attribute or by setting an onClickListener to the TextView.

How do you make a clickable text Spannable Android?

SpannableString string = new SpannableString("Text with clickable text"); string. setSpan(new CustomClickableSpan(), 10, 19, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); Text with ClickableSpan .

How do you set the part of the text view is clickable in Android?

This example demonstrates how do I set the part of the Android textView as clickable. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I use Linkify?

To call linkify in android, we have to call linkify. addLinks(), in that method we have to pass textview and LinkifyMask. Linkify. WEB_URLS: It going make URL as web url, when user click on it, it going to send url to default web browsers.


1 Answers

as i understand you want to make multiple part of textview clickable.

this code worked for me!

SpannableString ss = new SpannableString("this is a text");
ss.setSpan(new myClickableSpan(1),0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new myClickableSpan(2),5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new myClickableSpan(3),8, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

mTextView.setText(ss);
mTextView.setMovementMethod(LinkMovementMethod.getInstance());

just make custom ClickableSpan to handle the click event

public class myClickableSpan extends ClickableSpan{

    int pos;
    public myClickableSpan(int position){
        this.pos=position;
    }

    @Override
    public void onClick(View widget) {
        Toast.makeText(getApplicationContext(), "Position "  + pos + " clicked!", Toast.LENGTH_LONG).show();
    }

}
like image 197
Manian Rezaee Avatar answered Oct 20 '22 22:10

Manian Rezaee