Android Toast Customization

Source: Internet
Author: User

The Toast control is often used in Android project development, but the default style of the system is too ugly. Sometimes you need to change it, such as the background image and the above prompt text, sometimes you need to dynamically change the text of the prompt, such as the color.

Another question is how to make the content displayed in a TextView have different colors, for example, "Is the weather good today? It's good. "If you want to make" Today good ?" This sentence is displayed in red, and the words "pretty good" are displayed in green?

The following two questions will be answered together.

Next, let's look at the Code:

First, the main layout file main. xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: text = "@ string/hello"/>
<Button android: text = "Start Toast" android: id = "@ + id/button1"
Android: layout_width = "fill_parent" android: layout_height = "wrap_content"> </Button>
</LinearLayout>

Corresponding Activity

Package com. toast;

Import android. app. Activity;
Import android. graphics. Color;
Import android. OS. Bundle;
Import android. text. SpannableString;
Import android. text. Spanned;
Import android. text. style. ForegroundColorSpan;
Import android. view. Gravity;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. TextView;
Import android. widget. Toast;

Public class MyToastActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

Button button = (Button) findViewById (R. id. button1 );
Button. setOnClickListener (new MyOnclickListener ());
}
Private class MyOnclickListener implements OnClickListener {

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater. from (MyToastActivity. this );
View view = inflater. inflate (R. layout. my_toast, (ViewGroup) findViewById (R. id. toast_layout_root ));

TextView textView = (TextView) view. findViewById (R. id. text );

SpannableString ss = new SpannableString ("is the weather good today? Pretty good ");
Ss. setSpan (new ForegroundColorSpan (Color. RED), 0, 7, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
Ss. setSpan (new ForegroundColorSpan (Color. GREEN), 7, 10, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
TextView. setText (ss );

Toast toast = new Toast (MyToastActivity. this );
Toast. setDuration (Toast. LENGTH_LONG );
Toast. setView (view );
Toast. setGravity (Gravity. CENTER, 0, 0 );
Toast. show ();
}

}
}

Here, pay attention to the bold red part, that is, to process the text displayed in a TextView with different colors. Of course, you can also delete the lines and underline them...

Next, the layout file my_toast.xml used by Toast

<?xml version="1.0" encoding="utf-8"?>
<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:padding="10dp"
android:background="@drawable/pop_select_tost"
>
<TextView android:id="@+id/text"
android:layout_width="200dip"
android:layout_height="fill_parent"
android:layout_marginTop="10dip"
android:textColor="@color/black"
android:layout_marginLeft="10dip"/>
</LinearLayout>

To facilitate testing, we need to attach the images we use.

OK.

 

In addition, you can use Html or

TextView. setText (Html. fromHtml ("<font size = \" 3 \ "color = \" red \ "> is the weather good today? </Font> <font size = \ "3 \" color = \ "green \"> pretty good </font> "));

Which one do you like?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.