Home>

everyone is familiar toastNot much to say. Directly above the code.

The specific code is as follows:

main.xml:

<?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>
package com.example.test;
import android.app.activity;
import android.app.actionbar;
import android.app.fragment;
import android.content.intent;
import android.os.bundle;
import android.os.handler;
import android.view.gravity;
import android.view.layoutinflater;
import android.view.menu;
import android.view.menuitem;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.viewgroup;
import android.widget.button;
import android.widget.edittext;
import android.widget.imageview;
import android.widget.linearlayout;
import android.widget.textview;
import android.widget.toast;
import android.os.build;
public class mainactivity 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 location 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.ic_launcher);
      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.ic_launcher);
      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;
    }
  }
}

Just run.

  • Previous Application of lambda expressions in Java 8 and some general knowledge about generics
  • Next Summary of basic usage of Java multithreading
  • Trends