0

I have one TextView with two different colors. Here if I click mini it should redirect to one activity, if I click metro redirect to another activity. How can I achieve this?

TextView t = (TextView)v.findViewById(R.id.text);
String text = "<font color=#000000><b>"+"mini"+"</b></font><font color=#000000> added </font><font color=#1569C7>"+"Metro"+"</font><font color=#000000> as a favourite.</font>";
t.setText(Html.fromHtml(text));
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • There's not simple solution to this. If you're set on using TextView instead of WebView, then you need to start by determining the coordinates of the click point (onClickListener may not help here - you'd need to use Touch events) and the determine the position of text within the text view. – Aleks G Jul 10 '12 at 08:07

5 Answers5

2

What you can do is create an custom class that extends ClickableSpan and manage your clickable text. I had answered the same here.

Community
  • 1
  • 1
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
0

On your click listener of textview you have to find its text and you can use getText property just compare the text and opens activity as per the text.

user
  • 323
  • 6
  • 11
0

In an onclick listener I'd try this:

if (text.equalsIgnoreCase("mini")) {
    // call your next activity here
} else if(text.equalsIgnoreCase("metro")) {
    // call your next activity here
}

Don't forget to set android:clickable="true" in your XML file of the TextView.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Siva K
  • 4,968
  • 14
  • 82
  • 161
  • 2
    That won't help. Please read the question carefully. The OP wants to differentiate clicks on different parts of the textview. – Aleks G Jul 10 '12 at 08:09
0

I think you can use two textview in linear layout with two different colours and text. Then you can implement onClicklistener() for two text view. You will achieve what you want and it looks same like what you expecting.

<LinearLayout
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/settings" 
    android:layout_marginTop = "10dip"
    android:padding="3dip"
    android:text="@string/txt1"
    android:textColor="#000000"
    android:textSize="25sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/settings" 
    android:layout_marginTop = "10dip"
    android:padding="3dip"
    android:text="@string/txt2"
    android:textColor="#1569C7"
    android:textSize="25sp"
    android:textStyle="bold" />

</LinearLayout>

Hope it helps

vinothp
  • 9,939
  • 19
  • 61
  • 103
0

This answers what you want, though keep in mind that it's very hard to get the usability of such a component right. It's very likely that someone somewhere will have trouble clicking the links - especially if the text is on multiple lines and the linked words are close to each other vertically.

I would advice against such a solution.

Community
  • 1
  • 1
Zharf
  • 2,638
  • 25
  • 26