Learn How to Position the Toast Message

Today in this tutorial, we will discuss about how to position the toast message in Android applications.

Although I assume you must know what is Toast in android, but for beginners, I will define it in brief. A toast is the simple popup message which provides simple feedback about an operation. By default it is displayed at the bottom of the Application, but sometimes we don’t want it to be at the bottom. You should read my previous posts on toast messages if not yet read the list below:

The code which we are going to develop will show how to create toast messages in different positions by altering Gravity. You will learn how to alter the standard position of toast 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.

For example the following code will show the Toast at top-left corner with X-Offset and Y-Offset as 50 and 100 respectively.

 

<code class="java plain">Toast toast = Toast.makeText(getApplicationContext(), </code><code class="java string">"Hello toast!"</code><code class="java plain">, Toast.LENGTH_SHORT);</code>
<div class="container">
<div class="line number2 index1 alt1"><code class="java comments">// Set the Gravity to Top and Left</code></div>
<div class="line number3 index2 alt2"><code class="java plain">toast.setGravity(Gravity.TOP | Gravity.LEFT, 5</code><code class="java value">0</code><code class="java plain">, 1</code><code class="java value">00</code><code class="java plain">);</code></div>
<div class="line number4 index3 alt1"><code class="java plain">toast.show();</code></div>
<div class="line number4 index3 alt1">

 

Follow the steps below to create the code example discussed above:
Step 1: Create a new android application in your IDE.  

Step 2: Write following into your activity_main.xml:

 

<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="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="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="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="Bottom/Right Gravity" >
</Button>

</RelativeLayout>

 

 

Step 3: Write following into your MainActivity.java:

 

package com.example.toastpositioning;

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();
}

}
}

 

 

Step 4: Run for the output as shown in the video below:

 

That’s it!!!

Leave a Comment: