Home>

toast displays a message to the user on the phone screen,The message will disappear automatically after some time.

1. Default usage

toast.maketext (getapplicationcontext (), "default toast style", toast.length_short) .show ();

2. Fragment usage

toast.maketext (getactivity (), "Network connection error,Please check the network settings ", toast.length_long) .show ();

3.Custom display position effect

toast=toast.maketext (getapplicationcontext (), "custom position toast", toast.length_long);
toast.setgravity (gravity.center, 0, 0);
toast.show ();

4. With picture effect

toast=toast.maketext (getapplicationcontext (), "toast with image", toast.length_long);
toast.setgravity (gravity.center, 0, 0);
linearlayout toastview=(linearlayout) toast.getview ();
imageview imagecodeproject=new imageview (getapplicationcontext ());
imagecodeproject.setimageresource (r.drawable.icon);
toastview.addview (imagecodeproject, 0);
toast.show ();

5. Fully custom effects

layoutinflater inflater=getlayoutinflater ();
view layout=inflater.inflate (r.layout.custom, (viewgroup) findviewbyid (r.id.lltoast));
imageview image=(imageview) layout.findviewbyid (r.id.tvimagetoast);
image.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 ("Fully custom toast");
toast=new toast (getapplicationcontext ());
toast.setgravity (gravity.right | gravity.top, 12, 40);
toast.setduration (toast.length_long);
toast.setview (layout);
toast.show ();

6. Other threads

main.java code:

package com.wjq.toast;
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.viewgroup;
import android.view.view.onclicklistener;
import android.widget.imageview;
import android.widget.linearlayout;
import android.widget.textview;
import android.widget.toast;
public class main extends activity implements onclicklistener {
  handler handler=new handler ();
  @override
  public void oncreate (bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    setcontentview (r.layout.main);
    findviewbyid (r.id.btnsimpletoast) .setonclicklistener (this);
    findviewbyid (r.id.btnsimpletoastwithcustomposition) .setonclicklistener (this);
    findviewbyid (r.id.btnsimpletoastwithimage) .setonclicklistener (this);
    findviewbyid (r.id.btncustomtoast) .setonclicklistener (this);
    findviewbyid (r.id.btnruntoastfromotherthread) .setonclicklistener (this);
  }
  public void showtoast () {
    handler.post (new runnable () {
      @override
      public void run () {
        toast.maketext (getapplicationcontext (), "I'm from another thread!", toast.length_short) .show ();
      }
    });
  }
  @override
  public void onclick (view v) {
    toast toast=null;
    switch (v.getid ()) {
    case r.id.btnsimpletoast:
      toast.maketext (getapplicationcontext (), "default toast style", toast.length_short) .show ();
      break;
    case r.id.btnsimpletoastwithcustomposition:
      toast=toast.maketext (getapplicationcontext (), "custom position toast", toast.length_long);
      toast.setgravity (gravity.center, 0, 0);
      toast.show ();
      break;
    case r.id.btnsimpletoastwithimage:
      toast=toast.maketext (getapplicationcontext (), "toast with image", toast.length_long);
      toast.setgravity (gravity.center, 0, 0);
      linearlayout toastview=(linearlayout) toast.getview ();
      imageview imagecodeproject=new imageview (getapplicationcontext ());
      imagecodeproject.setimageresource (r.drawable.icon);
      toastview.addview (imagecodeproject, 0);
      toast.show ();
      break;
    case r.id.btncustomtoast:
      layoutinflater inflater=getlayoutinflater ();
      view layout=inflater.inflate (r.layout.custom, (viewgroup) findviewbyid (r.id.lltoast));
      imageview image=(imageview) layout.findviewbyid (r.id.tvimagetoast);
      image.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 ("Fully custom toast");
      toast=new toast (getapplicationcontext ());
      toast.setgravity (gravity.right | gravity.top, 12, 40);
      toast.setduration (toast.length_long);
      toast.setview (layout);
      toast.show ();
      break;
    case r.id.btnruntoastfromotherthread:
      new thread (new runnable () {
        public void run () {
          showtoast ();
        }
      }). start ();
      break;
    }
  }
}

main.xml code:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center"
  android:orientation="vertical"
  android:padding="5dip">
  <button
    android:id="@ + id/btnsimpletoast"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="default">
  </button>
  <button
    android:id="@ + id/btnsimpletoastwithcustomposition"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="custom display position">
  </button>
  <button
    android:id="@ + id/btnsimpletoastwithimage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="with picture">
  </button>
  <button
    android:id="@ + id/btncustomtoast"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="fully customized">
  </button>
  <button
    android:id="@ + id/btnruntoastfromotherthread"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Other threads">
  </button>
</linearlayout>

custom.xml:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@ + id/lltoast"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="#ffffffff"
  android:orientation="vertical">
  <textview
    android:id="@ + id/tvtitletoast"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="1dip"
    android:background="#bb000000"
    android:gravity="center"
    android:textcolor="#ffffffff" />
  <linearlayout
    android:id="@ + id/lltoastcontent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginbottom="1dip"
    android:layout_marginleft="1dip"
    android:layout_marginright="1dip"
    android:background="#44000000"
    android:orientation="vertical"
    android:padding="15dip">
    <imageview
      android:id="@ + id/tvimagetoast"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center" />
    <textview
      android:id="@ + id/tvtexttoast"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:paddingleft="10dip"
      android:paddingright="10dip"
      android:textcolor="#ff000000" />
  </linearlayout>
</linearlayout>
  • Previous Yii's multi-level linkage drop-down menu
  • Next Nodejs project deployment process using forever and pm2
  • Trends