Question

How to show a customized Toast like this :

enter image description here

Était-ce utile?

La 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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top