Asked  6 Months ago    Answers:  5   Viewed   27 times

Is it possible to set the color of just span of text in a TextView?

I would like to do something similar to the Twitter app, in which a part of the text is blue. See image below:

alt text
(source: twimg.com)

 Answers

73

Another answer would be very similar, but wouldn't need to set the text of the TextView twice

TextView TV = (TextView)findViewById(R.id.mytextview01);

Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");        

wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

TV.setText(wordtoSpan);
Tuesday, June 1, 2021
 
Packy
answered 6 Months ago
97

Try this way

create this function

public  void makeTextViewResizable(final TextView tv, final int maxLine, final String expandText) {

        if (tv.getTag() == null) {
            tv.setTag(tv.getText());
        }
        ViewTreeObserver vto = tv.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {

                ViewTreeObserver obs = tv.getViewTreeObserver();
                obs.removeGlobalOnLayoutListener(this);
                if (maxLine <= 0) {
                    int lineEndIndex = tv.getLayout().getLineEnd(0);
                    String text = tv.getText().subSequence(0, lineEndIndex - expandText.length() + 1) + " " + expandText;
                    tv.setText(text);
                } else if (tv.getLineCount() >= maxLine) {
                    int lineEndIndex = tv.getLayout().getLineEnd(maxLine - 1);
                    String text = tv.getText().subSequence(0, lineEndIndex - expandText.length() + 1) + " " + expandText;
                    tv.setText(text);
                }
            }
        });

    }

how to use

This will write "SeeMore" instead "..." at the end of 4th line

makeTextViewResizable(tv, 4, "SeeMore");

Now not need to write these lines in xml

android:ellipsize="end"
android:maxLines="3"
Thursday, August 5, 2021
 
Guesser
answered 4 Months ago
76

You should create a XML drawable for this, which can then be set as your single background. Here is what you are wanting (a rectangle with a different color border - replace gradient with if you don't want that).

This will go in your 'drawable' folder:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke android:width="3dp" android:color="@color/blue_button_border" />
    <gradient
      android:startColor="@color/gradient_end"
      android:endColor="@color/gradient_start"
      android:angle="-90" /> 
</shape>
Tuesday, August 10, 2021
 
cyber_truite
answered 4 Months ago
83

Performance wise good approach.

    int[] textViews = {R.id.textView1, R.id.textView2, R.id.textView3};
    String[] messages = {"one", "two", "three"};

     for (int i = 0; i < 3; i++) {
            TextView tv = (TextView) findViewById(textViews[i]);
            tv.setText(messages[i]);
        }
Friday, August 20, 2021
 
sverdianto
answered 4 Months ago
70

?attr/colorControlHighlight is a reference to a colorControlHighlight value defined in attr xml.

attrs.xml is a file located in:

android_sdkplatformsandroid-22dataresvaluesattrs.xml

Here, all of the attributes, that you may use in your app, are located.

If we check that file, we will find

<attr name="colorControlHighlight" format="color" /> string,

which means that colorControlHighlight itself is a reference to a color.

And all attributes declared here are just references to another values. The actual values are assigned in themes.xml file, which, in it's turn, is located in:

android-sdkplatformsandroid-22dataresvaluesthemes.xml

If we check that file, we'll find, that there are a lot of themes, which are using our colorControlHighlight reference. So whether you are using one theme or another in your application, the colorControlHighlight values would be different for each of them.

In our case there are 2 themes:

<item name="colorControlHighlight">@color/legacy_button_pressed</item> for Theme

and

<item name="colorControlHighlight">@color/legacy_light_button_pressed</item> for Theme.Light

Here we see another references instead of values. But now they refer to the color attribute: @color/. Thus we need to move to the color.xml files.

These are

android-sdkplatformsandroid-22dataresvaluescolors.xml

android-sdkplatformsandroid-22dataresvaluescolors_holo.xml

android-sdkplatformsandroid-22dataresvaluescolors_leanback.xml

android-sdkplatformsandroid-22dataresvaluescolors_legacy.xml

android-sdkplatformsandroid-22dataresvaluescolors_material.xml

Simple file check brings out the actual values we were looking for:

in the colors-legacy.xml file:

<color name="legacy_button_pressed">#fffea50b</color>

and

<color name="legacy_light_button_pressed">@color/legacy_button_pressed</color> which also refers to the first color.

So, the color we searched for, was #fffea50b

Sunday, October 10, 2021
 
octern
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share