3

I want a toast with multi-colors. Like this:

Multi Color Toast

I looked at the different tutorials on changing the layout in the xml to create a custom Toast, but none of them explain adding different colors like this.

How can I do this?

================================

ANSWER

================================

Using all your help, I have designed a simple Method() to make color toasts easier to call.

res/layout/toast_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    android:paddingBottom="6dp"
    android:paddingTop="6dp"
    android:background="#DAAA"
    >
    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

src/PROJECTNAME/FILENAME.java

// Color Toast(String1,String2,Color)
// Toastbackground = White
// String1 = Dark Gray
// String2 - **CASE SENSITIVE**
//   = "same" = Dark Gray, or
//   = "purple" = Purple, or
//   = "orange" = Orange

    public void CToast(String t1, String t2, String c) {
        if (c == "same") {
            c = "444444";
        } else if (c == "purple") {
            c = "6600FF";
        } else if (c == "orange") {
            c = "ffcc00";
        }

        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_layout,
                (ViewGroup) findViewById(R.id.toast_layout_root));
        TextView textCToast = (TextView) layout.findViewById(R.id.text);

        String text2 = "<font color=#444444>" + t1 + "</font> <font color=#" + c + ">" + t2 + "</font";
        textCToast.setText(Html.fromHtml(text2));

        Toast toast = new Toast(this);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();
    }

Hope this help! Thanks everyone

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
KickAss
  • 4,210
  • 8
  • 33
  • 41

3 Answers3

7

In yo0ur custom layout place a textView. There

String text = "This is <font color='red'>red</font>. This is <font color='blue'>blue</font>.";
textView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);

N.B.. example taken from here

Community
  • 1
  • 1
stinepike
  • 54,068
  • 14
  • 92
  • 112
4

Try doing this i hope this is what you realyy want

richTextView = (TextView)findViewById(R.id.rich_text);  

    // this is the text we'll be operating on  
    SpannableString text = new SpannableString("hello how are you");  

    // make "hello" to (characters 0 to 5) red color 
    text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);  

richTextView.setText(text, BufferType.SPANNABLE);  

And if you want it to show it as toast try this instead of setText use it like this

Toast.makeText(context, text, Toast.LENGTH_LONG).show();

enter image description here

Sreedev
  • 6,563
  • 5
  • 43
  • 66
3

I think you should toast with SpannableText - using that allows you to apply colors, styles and insert smileys and such into strings.

So, that would be my first idea to try and work out somehow.

I know for sure that this works for painting text and multicolor notifications. Or maybe you just have to apply the spannable filter. See here

Community
  • 1
  • 1
Shark
  • 6,513
  • 3
  • 28
  • 50