Show Toast in four different corners

Description

The following code shows how to Show Toast in four different corners.

Example


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:onClick="upperLeft"
        android:text="show toast" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:onClick="upperRight"
        android:text="show toast" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:onClick="bottomLeft"
        android:text="show toast" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:onClick="bottomRight"
        android:text="show toast" />

</RelativeLayout>

Java code


import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;
/*from   w  w w .j a v a2s.  com*/
public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }

  public void upperLeft(View v) {
    Toast toast = Toast.makeText(this, "Upper Left!",
        Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);
    toast.show();
  }

  public void upperRight(View v) {
    Toast toast = Toast.makeText(this, "Upper Right!",
        Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.TOP | Gravity.RIGHT, 0, 0);
    toast.show();
  }

  public void bottomLeft(View v) {
    Toast toast = Toast.makeText(this, "Bottom Left!",
        Toast.LENGTH_SHORT);

    toast.setGravity(Gravity.BOTTOM | Gravity.LEFT, 0, 0);
    toast.show();
  }

  public void bottomRight(View v) {
    Toast toast = Toast.makeText(this, "Bottom Right!",
        Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 0, 0);
    toast.show();
  }
}
Show Toast in four different corners




















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView