Search code examples
androidtoastspannablestring

Customize Toast message text color


How to show a customized Toast like this :

enter image description here


Solution

  • We can use SpannableString for this message as a text.

    like this block:

    public void showToast(){
       SpannableString span=new SpannableString("This is a Colorful Spannable text!");
        span.setSpan(new ForegroundColorSpan(Color.CYAN), 10, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(new ForegroundColorSpan(Color.GRAY), 11, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(new ForegroundColorSpan(Color.GREEN), 12, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(new ForegroundColorSpan(Color.MAGENTA), 13, 14, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(new ForegroundColorSpan(Color.RED), 14, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(new ForegroundColorSpan(Color.LTGRAY), 15, 16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(new ForegroundColorSpan(Color.BLUE), 16, 17, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(new ForegroundColorSpan(Color.YELLOW), 17, 18, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
        Toast.makeText(this,span, Toast.LENGTH_LONG).show();
    }
    
    showToast();
    

    and this is the result:

    enter image description here