How to Position Toast in Android Application?

In this post, I am going to discuss about how to position toast message in Android applications.

What is Toast?

A toast provides simple feedback about an operation as a simple popup message. It is usually be displayed at the bottom of the Application.

What are we going to develop?

The application which we are going to develop will help you to create toast messages in different positions by altering Gravity and also by taking custom X-offset and Y-offset values.

Toast

Positioning the Toast message

A standard toast notification appears at the bottom of the screen, centered horizontally. You can alter this position with the help of setGravity(int, int, int) method. This method accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.

Below piece of code will display the Toast at top-left corner with X-Offset and Y-Offset as 100 and 200 respectively.

Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
// Set the Gravity to Top and Left
toast.setGravity(Gravity.TOP | Gravity.LEFT, 100, 200);
toast.show();

To show the Toast in Top Right corner with X-Offset and Y-Offset as 100 and 200 respectively:

Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
// Set the Gravity to Top and Right
toast.setGravity(Gravity.TOP | Gravity.RIGHT, 100, 200);
toast.show();

To show the Toast in Bottom Left corner with X-Offset and Y-Offset as 100 and 200 respectively:

Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
// Set the Gravity to Bottom and Left
toast.setGravity(Gravity.BOTTOM | Gravity.LEFT, 100, 200);
toast.show();

To show the Toast in Bottom Right corner with X-Offset and Y-Offset as 100 and 200 respectively:

Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
// Set the Gravity to Bottom and Right
toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 100, 200);
toast.show();

You can download source code from here if you don’t want to create Application from scratch, otherwise please proceed with below listings.

Step 1: Create Android Application Project

  • Create new android project [File >> New >> Android Application Project] with project name AndroidToastPosition
  • Enter package name as ‘com.prgguru.example’ and activity name as ‘MainActivity’
  • Choose Minimum Required SDK, Target SDK and Compile with (Choose latest SDKs to make sure Facebook SDK will work without any issues). Confused on choosing these options? Take a look at Minimum Required SDK – Target SDK – Compile With post.
  • Click Next button and finally click ‘Finish’ to create project

Step 2: Design screen

Open activity_main.xml and replace it with below code.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.prgguru.example.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="X-Offset" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:gravity="center"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:text="Y-Offset" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:gravity="center"
        android:inputType="number" >

    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="500dp"
        android:layout_height="60dp"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:onClick="showToastTopLeft"
        android:text="Show Toast (With Top/Left Gravity)" >
    </Button>

    <Button
        android:id="@+id/button2"
        android:layout_width="500dp"
        android:layout_height="60dp"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:onClick="showToastTopRight"
        android:text="Show Toast (With Top/Right Gravity)" >
    </Button>

    <Button
        android:id="@+id/button3"
        android:layout_width="500dp"
        android:layout_height="60dp"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true"
        android:onClick="showToastBottomLeft"
        android:text="Show Toast (With Bottom/Left Gravity)" >
    </Button>

    <Button
        android:id="@+id/button4"
        android:layout_width="500dp"
        android:layout_height="60dp"
        android:layout_below="@+id/button3"
        android:layout_centerHorizontal="true"
        android:onClick="showToastBottomRight"
        android:text="Show Toast (With Bottom/Right Gravity)" >
    </Button>

</RelativeLayout>

Step 3: MainActivity.java – Main Class

Open MainActivity.java and replace it with below code.

Each line of code is commented well, if you still have any doubt or question discuss it.

package com.prgguru.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	/**
	 * When Button "@+id/button1" is clicked
	 * @param v
	 */
	public void showToastTopLeft(View v) {
		EditText e1 = (EditText) findViewById(R.id.editText1);
		EditText e2 = (EditText) findViewById(R.id.editText2);
		if (e1.getText().toString().trim().length() > 0
				&& e2.getText().toString().trim().length() > 0) {
			String editText1 = e1.getText().toString();
			String editText2 = e2.getText().toString();
			int xOffset = Integer.parseInt(editText1);
			int yOffset = Integer.parseInt(editText2);
			Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
			// Set the Gravity to Top and Left
			toast.setGravity(Gravity.TOP | Gravity.LEFT, xOffset, yOffset);
			toast.show();
		} else {
			Toast.makeText(getApplicationContext(),
					"Please enter value for X-Offset and/or Y-Offset", Toast.LENGTH_SHORT)
					.show();
		}

	}
	
	/**
	 * When Button "@+id/button2" is clicked
	 * @param v
	 */
	public void showToastTopRight(View v) {
		EditText e1 = (EditText) findViewById(R.id.editText1);
		EditText e2 = (EditText) findViewById(R.id.editText2);
		if (e1.getText().toString().trim().length() > 0
				&& e2.getText().toString().trim().length() > 0) {
			String editText1 = e1.getText().toString();
			String editText2 = e2.getText().toString();
			int xOffset = Integer.parseInt(editText1);
			int yOffset = Integer.parseInt(editText2);
			Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
			// Set the Gravity to Top and Right
			toast.setGravity(Gravity.TOP | Gravity.RIGHT, xOffset, yOffset);
			toast.show();
		} else {
			Toast.makeText(getApplicationContext(),
					"Please enter value for X-Offset and/or Y-Offset", Toast.LENGTH_SHORT)
					.show();
		}

	}
	
	/**
	 * When Button "@+id/button3" is clicked
	 * @param v
	 */
	public void showToastBottomLeft(View v) {
		EditText e1 = (EditText) findViewById(R.id.editText1);
		EditText e2 = (EditText) findViewById(R.id.editText2);
		if (e1.getText().toString().trim().length() > 0
				&& e2.getText().toString().trim().length() > 0) {
			String editText1 = e1.getText().toString();
			String editText2 = e2.getText().toString();
			int xOffset = Integer.parseInt(editText1);
			int yOffset = Integer.parseInt(editText2);
			Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
			// Set the Gravity to Bottom and Left
			toast.setGravity(Gravity.BOTTOM | Gravity.LEFT, xOffset, yOffset);
			toast.show();
		} else {
			Toast.makeText(getApplicationContext(),
					"Please enter value for X-Offset and/or Y-Offset", Toast.LENGTH_SHORT)
					.show();
		}

	}
	
	/**
	 * When Button "@+id/button4" is clicked
	 * @param v
	 */
	public void showToastBottomRight(View v) {
		EditText e1 = (EditText) findViewById(R.id.editText1);
		EditText e2 = (EditText) findViewById(R.id.editText2);
		if (e1.getText().toString().trim().length() > 0
				&& e2.getText().toString().trim().length() > 0) {
			String editText1 = e1.getText().toString();
			String editText2 = e2.getText().toString();
			int xOffset = Integer.parseInt(editText1);
			int yOffset = Integer.parseInt(editText2);
			Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
			// Set the Gravity to Bottom and Right
			toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, xOffset, yOffset);
			toast.show();
		} else {
			Toast.makeText(getApplicationContext(),	"Please enter value for X-Offset and/or Y-Offset", Toast.LENGTH_SHORT).show();
		}

	}
}

Demo

That’s all. It’s time to test our code.

Run the application using emulator or device by right clicking on the project >> Run as >> Android applicaiton >> Choose emulator or device.



Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project (How to import android project in eclipse).

Download Source Code

[pglinkadssmall]

If you feel this article is helpful and interesting please spread a word about it to your friends and colleagues by sharing the article in Facebook or Twitter.

Share

You are always welcome to provide your comments and feedback from comment box.

[pgwriteforus]

[pgfeedback]

Related Articles

Author: Udhay

Share This Post On
468 ad
Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest

Shares