Android Toast usage Summary

Source: Internet
Author: User

Content

  • Environment
  • Demonstrate Toast usage
Environment
  • Windows 2008 R2 64-bit
  • Eclipse ADT V22.6.2, Android 4.4.3
  • Samsung SM-G3508

 

Demonstrate Toast usage

A progressive demonstration of how to use Toast.

Figure 1 left: Main Program; middle: click "Default Toast"; Right: click "Custom location Toast"

Figure 2 left: click "Toast with image"; medium: click "Custom Toast"; right: "Toast from other threads"

The main. xml file contains only six buttons. Toast can also use the custom. xml file as an information prompt to download the Demo, Which is omitted here, as shown in Ruian, the core code:

package com.example.toastdemo.activity;
 
import com.example.toastdemo.R;
 
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends Activity {
    Handler handler = new Handler();
    Toast toast = null;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        findViewById(R.id.btnSimpleToast).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
Toast. makeText (getApplicationContext (), "Toast by default ",
                                Toast.LENGTH_SHORT).show();
                    }
                });
 
        findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        toast = Toast.makeText(getApplicationContext(),
"Custom location Toast", Toast. LENGTH_LONG );
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    }
                });
 
        findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        toast = Toast.makeText(getApplicationContext(),
"Toast with image", Toast. LENGTH_LONG );
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        LinearLayout toastView = (LinearLayout) toast.getView();
                        ImageView image1 = new ImageView(
                                getApplicationContext());
                        image1.setImageResource(R.drawable.icon);
                        toastView.addView(image1, 0);
                        toast.show();
                    }
                });
 
        findViewById(R.id.btnCustomToast).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LayoutInflater inflater = getLayoutInflater();
                        View layout = inflater.inflate(R.layout.custom,
                                (ViewGroup) findViewById(R.id.llToast));
                        ImageView image2 = (ImageView) layout
                                .findViewById(R.id.tvImageToast);
                        image2.setImageResource(R.drawable.icon);
                        TextView title = (TextView) layout
                                .findViewById(R.id.tvTitleToast);
                        title.setText("Attention");
                        TextView text = (TextView) layout
                                .findViewById(R.id.tvTextToast);
Text. setText ("Custom Toast ");
                        toast = new Toast(getApplicationContext());
                        toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
                        toast.setDuration(Toast.LENGTH_LONG);
                        toast.setView(layout);
                        toast.show();
                    }
                });
 
        findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new Thread(new Runnable() {
                            public void run() {
                                showToast();
                            }
                        }).start();
                    }
                });
 
    }
 
    public void showToast() {
        handler.post(new Runnable() {
            @Override
            public void run() {
Toast. makeText (getApplicationContext (), "from other threads Toast ",
                        Toast.LENGTH_SHORT).show();
 
            }
        });
    }
}

Download Demo

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.