4

Guys this is the sample text: "I want this [ option1 / option2 ] clickable."

What i what to happen is when user click an option( or a word) i will save the clicked word to a variable. How to make that options clickable?

Thanks for any help..

jayellos
  • 691
  • 1
  • 14
  • 32
  • 1
    Look there: http://stackoverflow.com/questions/1697084/handle-textview-link-click-in-my-android-app/3452944#3452944 – pawelzieba Nov 10 '11 at 11:39

2 Answers2

3

This is a blog having example same as you need It helped me a lot. Link To Blog

Code from Blog how can you do this.

In Your Main Activity set Links This way.

mTextSample = (TextView) findViewById(R.id.text1);
        text = "<html>Visit my blog <a href=\"http://sherifandroid.blogspot.com\">mysite</a> or run the <a href=\"sherif-activity://myactivity?author=sherif&nick=king\">myactivity</a> callback</html>";  
        mTextSample.setText(Html.fromHtml(text));
        mTextSample.setMovementMethod(LinkMovementMethod.getInstance());

To get the Parameters :

TextView mTextSample = (TextView) findViewById(R.id.text);
        mTextSample.setText(getIntent().getData().getQueryParameter("author"));
        mTextSample = (TextView) findViewById(R.id.text1);
        mTextSample.setText(getIntent().getData().getQueryParameter("nick"));

In Manifest Declare your second Activity as :

<activity android:label="@string/app_name" android:name="sherif.android.linkify.example.TestActivity"> 
   <intent-filter> <data android:scheme="sherif-activity" android:host="myactivity"/> 
   <action android:name="android.intent.action.VIEW"/> 
   <category android:name="android.intent.category.DEFAULT"/> 
   <category android:name="android.intent.category.BROWSABLE"/> 
   </intent-filter> 
</activity>
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • Thanks for expanding your answer. The problem with just posting a link is if the link breaks, the answer loses value. On another note, I've removed comments under this answer, as they degenerated into noise. In the future, please keep comments constructive, professional and most importantly, on topic. – Tim Post Nov 11 '11 at 07:47
-1

can't do that easily without checking inside the text view in an onTouch event which part of the textview is touched (involves checking x,y position of touch inside textview )

to do what you want to do easily use 2 textboxes and set 2 different onClickListener

check this out for more detail http://developer.android.com/guide/topics/ui/ui-events.html on how to set clicklisteners

Optimus
  • 2,716
  • 4
  • 29
  • 49
  • that's what I use before, but the problem is, the text is from database and there is a possibility that the text will be like this "I want this [ option1 / option2 ] clickable. And also I want this [ option1 / option2 ] clickable. ..." – jayellos Nov 10 '11 at 11:39
  • @marvz finally i guess you need a custom view, a horizontal LinearLayout with a List of Textboxes, – Optimus Nov 10 '11 at 11:42
  • @dziobas if its not, please post an answer to clarify what can be done, thanks :) – Optimus Nov 10 '11 at 11:45
  • @dziobas what if there are 100 options in one sentence? – jayellos Nov 10 '11 at 11:52
  • 3
    @Optimus the way you mentioned is doable only upto some extent but android provide other facility like linkify,that can be used,if there are 100 options that also can be done this way...if you think like Twitter app having many clickable links in text view showing text with user names,links,keywords..then how to do..that time linkify is used. hope u understood. – MKJParekh Nov 10 '11 at 12:42