Is it possible to change the text color in a string to multiple colors in Java?

后端 未结 9 1776
暗喜
暗喜 2020-11-27 04:37

What I mean is, is it possible to change the text \"This text is blue\" to the color blue in a single string? There must be a way...



        
相关标签:
9条回答
  • 2020-11-27 04:50

    Try this..

    TextView update= (TextView) dialog.findViewById(R.id.address);
    String colorText= "Driver is nearby "
                    + "<font color=\"#E72A02\"><bold>"
                    + "43, KR Rd, Tata Silk Farm, Jayanagar"
                    + "</bold></font>"
                    + " and he is "
                    + "<font color=\"#B92000\"><bold>"
                    + "11 km"
                    + "</bold></font>"
                    + " & "
                    + "<font color=\"#B92000\"><bold>"
                    + "20 mins"
                    + "</bold></font>"
                    + " away from your current location.";
    
            update.setText(Html.fromHtml(colorText));
    

    and the result will be like this..

    result

    0 讨论(0)
  • 2020-11-27 04:52

    You can try this: In a fragment :

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
    View rootView = inflater.inflate(
                    R.layout.shipment_accepted_fragment,
                    container, false);
    
            String email = "(abc@gmail.com)";
    
            TextView shipment_email = (TextView) rootView
                    .findViewById(R.id.textview);
            String text_shipment_email = "Hello"
                    + " <font color='"
                    + getResources().getColor(R.color.green_color) + "'>" + email
                    + "</font>"
                    + "Bye";
            shipment_email.setText(Html.fromHtml(text_shipment_email),
                    TextView.BufferType.SPANNABLE);
    }
    
    0 讨论(0)
  • 2020-11-27 04:53

    Html.fromHtml(String) is deprecated in Android N

    To support latest version of Android so something like this

     val colorText = ("Some Normal Text\n" 
                        + "<font color=\"#FFA500\"><bold> Orange Text </bold></font>"
                        + "More Normal text")
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                venueAddressValue.setText(Html.fromHtml(colorText, Html.FROM_HTML_MODE_LEGACY));
            } else {
                venueAddressValue.setText(Html.fromHtml(colorText));
            }
    
    0 讨论(0)
  • 2020-11-27 04:56
    String text = "<font color=#000>this color is black </font><font color=#6ab04c>this color is green</font>";
    
    textview.setText(Html.fromHtml(text));
    
    0 讨论(0)
  • 2020-11-27 05:03

    A simple way to do it is to use HTML and set the text to the TextView programmatically.

    String text = "This text is white. <font color=\"blue\">This text is blue.</font>";
    textView.setText(Html.fromHtml(text), BufferType.SPANNABLE);
    
    0 讨论(0)
  • 2020-11-27 05:09

    I created a class like this:

    import android.text.SpannableStringBuilder;
    import android.text.style.CharacterStyle;
    
    public class StyleableSpannableStringBuilder extends SpannableStringBuilder {
        public StyleableSpannableStringBuilder appendWithStyle(CharacterStyle c, CharSequence text) {
            super.append(text);
            int startPos = length() - text.length();
            setSpan(c, startPos, length(), 0);
            return this;
        }
    
        public StyleableSpannableStringBuilder appendWithStyle(CharacterStyle [] c, CharSequence text) {
            super.append(text);
            int startPos = length() - text.length();
            for (CharacterStyle c1 : c)
                setSpan(c1, startPos, length(), 0);         
            return this;
        }       
    }
    

    This allows me to do things like this:

    private void buildTickerItem(DelayedQuoteServiceObject o)
    {   
        Double lastPrice = Double.parseDouble(o.getValue("LastPrice"));
        Double dayChange = Double.parseDouble(o.getValue("DayChange"));
        Double percentChange = Double.parseDouble(o.getValue("PercentDayChange")) / 100;
    
        if (o.isIndex() == true)
        {
    
            tickerTapeData.appendWithStyle(new StyleSpan(Typeface.BOLD),o.getDisplayName());            
            tickerTapeData.append(" "+ indexFormat.format(lastPrice) + " (");
    
            if (dayChange >= 0)
                tickerTapeData.appendWithStyle(new ForegroundColorSpan(Colours.getTickerPositive()), indexFormat.format(dayChange));        
            else
                tickerTapeData.appendWithStyle(new ForegroundColorSpan(Color.RED), indexFormat.format(dayChange));
        }
        else
        {
            tickerTapeData.appendWithStyle(new StyleSpan(Typeface.BOLD), o.ticker);
    
            tickerTapeData.append("@"+ dollarFormat.format(lastPrice) + " (");              
    
            if (dayChange >= 0)
                tickerTapeData.appendWithStyle(new ForegroundColorSpan(Colours.getTickerPositive()), dollarFormat.format(dayChange));       
            else
                tickerTapeData.appendWithStyle(new ForegroundColorSpan(Color.RED), dollarFormat.format(dayChange));
    
    
        }
    
        tickerTapeData.append("/");
    
    
        if (dayChange >= 0)
            tickerTapeData.appendWithStyle(new ForegroundColorSpan(Colours.getTickerPositive()), percentFormat.format(percentChange));      
        else
            tickerTapeData.appendWithStyle(new ForegroundColorSpan(Color.RED), percentFormat.format(percentChange));
    
        tickerTapeData.append(")  ");       
    }
    

    To create a ticker tape. Works pretty nicely and keeps the code clean.

    0 讨论(0)
提交回复
热议问题