• 11
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

Suppose I have a string

String str =" Hello, @John how's going on"

Now I want to know., how to highlight the word @John and wanna make it clickable from Textview

You can use clickable span in a spannable string like the following :-

SpannableString ss = new SpannableString(" Hello, @John how's going on");
ClickableSpan clickableSpan = new ClickableSpan() {
   @Override
   public void onClick(View textView) {
     //perform click operation
   }
   @Override
   public void updateDrawState(TextPaint ds) {

   }
};

ss.setSpan(clickableSpan, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

yourTv.setText(ss);
yourTv.setMovementMethod(LinkMovementMethod.getInstance());
yourTv.setHighlightColor(Color.TRANSPARENT);

startIndex - is the index of J of word John in the full string

endIndex - is the index of n of the word John in the full string.

&& the onclick method will be triggered once john is clicked..

Edit

To extract the username John u can split the string and get it like this snippet :-

 String username = yourString.split("@")[1].split(" ")[0]; // this will extract john 
  • 2
Reply Report

Here You can Set Your String In text view in your XML file it will not be an issue. then you can Add the onclick: "clickable" attribute in it and it will work fine

  • 0
Reply Report
      • 2
    • that will just make whole Textview clickable . What I want is that to extract the username @John and only make that clickable

This will also work with multible mentions.

var str = "Hello, @John how's going on. Greedings to @Bob.";
    var regex = /[@]\S[^. ]*/g;

    var matches = str.match(regex)
    var split = str.split(regex);

    for(var i = 0; i<matches.length;i++){
        document.body.appendChild(document.createTextNode(split[i]));

        var a = document.createElement('a');
        a.appendChild(document.createTextNode(matches[i]));
        a.title = "my title text";//text you can see onHover
          a.href = "http://example.com/users/"+matches[i].split('@')[1];//link to user
        document.body.appendChild(a);
    }
    document.body.appendChild(document.createTextNode(split[split.length-1]));
  • -1
Reply Report