Search code examples
javaandroidclickablespannablestring

Add tag to ClickableSpan


I'm trying to store data in a ClickableSpan, like it can be done with setTag() with other type of elements.

I've read this answer in stackoverflow: SpannableString - setTag

And it clearly says: - Create your own subclass of ClickableSpan that holds the data you want, and apply it to your SpannableString. - When you create the ClickableSpan and attach a word to it via a data member, you will have access to that data in onClick()

But I'm somewhat of a newbie and can't figure out how to code this.

I am using a class that extends ClickableSpan to create my clickable spans:

// CLASS BEING IMPORTED
public abstract class TouchableSpan extends ClickableSpan {

    // IMPLEMENT SET TAG FUNCTION HERE??

}

// CODE ON ACTIVITY
TouchableSpan touchableSpan = new TouchableSpan() {

    @Override
    public void onClick(View widget) {
        this.setPressed(true);

        // GET TAG INFO HERE. BUT HOW?
    }

    // SET TAG HERE? BUT HOW?

    private boolean mIsPressed;

    public void setPressed(boolean isSelected) {
        mIsPressed = isSelected;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setColor(Color.BLACK);
        ds.bgColor = mIsPressed ? selectedHlColor : 0xffeeeeee;
        ds.setUnderlineText(false);
    }
}

spannablesstringbuilder.setSpan(touchableSpan, index+2, index2, 0);

comment: I know I should ideally ask clarification on the comments sections of the question I linked, but I don't have enough reputation to do so.


Solution

  • I think that the solution is use custom functions inside your custom class somthing like this could work

    // CLASS BEING IMPORTED
    public abstract class TouchableSpan extends ClickableSpan {
    
      // IMPLEMENT SET TAG FUNCTION HERE??
    
      //COMMENT: YES, LIKE THIS: CAN BE ANY FUNCTION U LIKE
    
      private String myCustomVar;
    
      public String getMyVar(){
      return this.myCustomVar;
      }
    
      public void setMyVar(String myVari){
      this.myCustomVar = myVari;
      return;
      }
    
    }
    
    // CODE ON ACTIVITY
    TouchableSpan touchableSpan = new TouchableSpan() {
    
      @Override
      public void onClick(View widget) {
          this.setPressed(true);
    
          // GET TAG INFO HERE. BUT HOW?
    
          //COMMENT: LIKE THIS: 
    
          String extravar = touchableSpan.getMyVar();
          Log.d("TEST", extravar);
      }
    
      // SET TAG HERE? BUT HOW?
    
      COMMENT: NOT HERE. SEE BELOW
    
      private boolean mIsPressed;
    
      public void setPressed(boolean isSelected) {
          mIsPressed = isSelected;
      }
    
      @Override
      public void updateDrawState(TextPaint ds) {
          super.updateDrawState(ds);
          ds.setColor(Color.BLACK);
          ds.bgColor = mIsPressed ? selectedHlColor : 0xffeeeeee;
          ds.setUnderlineText(false);
      }
    }
    
    //COMMENT: SET YOUR VARIABLE/TAG HERE:
    
    touchableSpan.setMyVar("HOLALA");
    
    spannablesstringbuilder.setSpan(touchableSpan, index+2, index2, 0);