Search code examples
androidandroid-layouttextview

How to append the textview to the end of another textview?


I am creating a Gallery application. Each image will show the title along with the name of the person who uploaded the image. Example of what I want is below:

enter image description here

Here the Night is dark, but this darkness has the silence is the title for the image, and the shaggy boy is the user who uploaded the image for example.

So, now I want Night is dark, but this darkness has the silence to be in the separate textview and the shaggy boy at the end of the Night is dark, but this darkness has the silence's textview even if Night is dark, but this darkness has the silence is a single line or multi-line.

I have used the Flexbox Layout but I am failed to achieve what I want.

How to append the textview to the end of another textview?


Solution

  • You can use a single TextView and set its text String with a Spannable and match each portion of text with indices here I am separating both portions by a hyphen; but you can change that as you would like.

    TextView textView = findViewById(...);
    
    String tag = "Night is dark, but this darkness has the silence - shaggy boy";
    SpannableString spannableString = new SpannableString(tag);
    
    spannableString.setSpan(new ForegroundColorSpan(Color.BLACK), 0, tag.indexOf("-"), 0);
    spannableString.setSpan(new ForegroundColorSpan(Color.GRAY), tag.indexOf("-"), tag.length(), 0);
    textView.setText(spannableString);