2

I have a little question about coding in Java on Android.

So, I have one TextView with a long text from a string. I want to make only one word of this long text clickable, that I can link it to an other Activity.

By the way, I'm new to the Android development, so please don't explain your solution too complicated. Thanks! :)

I have no idea, how I could do this. I also googled after a solution before but didn't find a clear way that worked well. Anyway, I would appreciate your help.

Piezord
  • 159
  • 1
  • 2
  • 9

4 Answers4

2

What is the outcome you want for the click?

The simplest way to do this would be to apply a URLSpan onto the TextView's contents, but if you want to do something other than view a webpage you can implement your own version of ClickableSpan and make the click do whatever you want.

Edit per your comment:

Making a ClickableSpan go to another activity is really easy, here's the start of the code you'd need for it:

public class MyURLSpan extends ClickableSpan {
        Class<?> mClassToLaunch;

        public MyURLSpan(Class<?> classToLaunch) {
                mClassToLaunch = classToLaunch;
        }

        @Override
        public void onClick(View widget) {
                Intent intent = new Intent(widget.getContext(), mClassToLaunch);
                widget.getContext().startActivity(intent);
        }

        @Override
        public void updateDrawState(TextPaint ds) {
                // If you don't want the default drawstate (blue-underline), comment this super call out.
                super.updateDrawState(ds);
        }
}

Then to use it:

String longText = "your text goes here...";

SpannableString textViewContents = new SpannableString(longText);
textViewContents.setSpan(new MyURLSpan(NextActivity.class), indexToStart, lengthOfClickArea, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

myTextView.setText(textViewContents);
Kasra Rahjerdi
  • 2,483
  • 26
  • 42
  • Sorry I forgot to tell this... I will add it. So, I want to link that one word to an other activity. – Piezord Dec 23 '13 at 18:38
  • @Piezord Added some example code :) (Wrote it by hand so I don't think it'll compile without actually checking some of it) – Kasra Rahjerdi Dec 23 '13 at 18:44
  • Thank you but I have an error in the following line: `textViewContents.setSpan(new MyURLSpan(extras.class), indexToStart, lengthOfClickArea,` . I get the error: Syntax error on token(s), misplaced construct(s) So, what does this mean, what shall I do here? – Piezord Dec 23 '13 at 19:06
  • Those are words I put in there to tell you that actual values need to go there instead :) Look at the description of [setSpan](http://developer.android.com/reference/android/text/Spannable.html) to see what to use. – Kasra Rahjerdi Dec 23 '13 at 19:07
  • I'm sorry but I don't get it. :/ So, you see I'm really new to the Android Development. Perhaps you can explain it yet a bit easier? :) Maybe which words I need to change. Maybe wrote this words is " " or something like that? That would be so nice. :) (Btw I'm not English, that makes it a bit more difficult for me to understand...) – Piezord Dec 23 '13 at 19:42
  • Ok. I got an other solution but anyway, thank you very much! :) – Piezord Dec 23 '13 at 20:04
1

Well, you're lucky.
It seems to be possible: see this link.
Another method could be to put an hyperlink in your string.

Community
  • 1
  • 1
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
0

You can create a TextView per-word, and add them to a FlowLayout. Then you can assign OnClick events to any view you want.

The sentance "This is a test" would actually be made up of 4 TextViews, all inside some Layout (maybe a FlowLayout).

There's some details about a FlowLayout in this SO post:

How can I do something like a FlowLayout in Android?

Community
  • 1
  • 1
Matt
  • 3,837
  • 26
  • 29
0

i did not try this but i think if you use below hint then may be You will success. txt.setText(Html.fromHtml("...link...")); txt.setMovementMethod(LinkMovementMethod.getInstance());

select a word on a tap in TextView/EditText

i think it will help.

Community
  • 1
  • 1
Yogendra
  • 4,817
  • 1
  • 28
  • 21