Search code examples
androidandroid-layoutandroid-activityandroid-imageviewtextview

How to create feed post view like Twitter with dynamic links?


I'm trying to achieve something like this image image twitter tweet. The user creates tweets with hash tags and links in it and Twitter app converts into click able links. Normal TextView can't achieve that. How do I create something like that? Please provide technical details.


Solution

  • Try this You can use ClickableSpan to achieve this like this

            ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                Toast.makeText(context,"clicked", Toast.LENGTH_SHORT).show();
    
            }
        };
    
            SpannableStringBuilder builder = new SpannableStringBuilder();
    
            SpannableString str1 = new SpannableString("click me");
            str1.setSpan(clickableSpan, 0, str1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            str1.setSpan(new ForegroundColorSpan(Color.BLUE), 0, str1.length(), 0);
            str1.setSpan(new UnderlineSpan(), 0, str1.length(), 0);
    
            SpannableString str2 = new SpannableString("demo String 2  ");
            builder.append(str2);
            builder.append(str1);
    
            Textview.setText(builder, TextView.BufferType.SPANNABLE);
            Textview.setMovementMethod(LinkMovementMethod.getInstance());