How to Set Position of Toast in Android Application

Toast 

As we know that A toast is a view containing a quick little message for the user in android. When the view is shown to the user, appears as a floating view over the application.

Toast notification in android always appears near the bottom of the screen, centered horizontally.

We can also change its position with the setGravity(int, int, int) method.

This method has three parameters:

  1. A Gravity constant,
  2. An x-position offset,
  3. A y-position offset.
For instance:

TOP_RIGHT:

  1. Toast toast = Toast.makeText(getApplicationContext(), "TOP RIGHT!", Toast.LENGTH_LONG);  
  2. // Here we can set the Gravity to Top and Right  
  3. toast.setGravity(Gravity.TOP | Gravity.RIGHT, 100, 200);  
  4. toast.show();  
TOP_LEFT:
  1. Toast toast = Toast.makeText(getApplicationContext(), "TOP LEFT!", Toast.LENGTH_LONG);  
  2. // Set the Gravity to Top and Left  
  3. toast.setGravity(Gravity.TOP | Gravity.LEFT, 100, 200);  
  4. toast.show();  
BOTTOM_LEFT:
  1. Toast toast= Toast.makeText(getApplicationContext(), "BOTTOM LEFT!", Toast.LENGTH_LONG);  
  2. // Set the Gravity to Bottom and Left  
  3. toast.setGravity(Gravity.BOTTOM | Gravity.LEFT, 100, 200);  
  4. toast.show();  
BOTTOM_RIGHT:
  1. Toast toast = Toast.makeText(getApplicationContext(), "BOTTOM RIGHT!", Toast.LENGTH_LONG);  
  2. // Set the Gravity to Bottom and Right  
  3. toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 100, 200);  
  4. toast.show();  
Source Code:

Main_Activity.java
  1. package com.example.abhijeet.toastpositiondemo;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.design.widget.FloatingActionButton;  
  5. import android.support.design.widget.Snackbar;  
  6. import android.support.v7.app.AppCompatActivity;  
  7. import android.support.v7.widget.Toolbar;  
  8. import android.view.Gravity;  
  9. import android.view.View;  
  10. import android.view.Menu;  
  11. import android.view.MenuItem;  
  12. import android.widget.Button;  
  13. import android.widget.Toast;  
  14.   
  15. public class MainActivity extends AppCompatActivity {  
  16.   
  17.     public Button btn;  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
  24.         setSupportActionBar(toolbar);  
  25.   
  26.         btn = (Button)findViewById(R.id.button);  
  27.   
  28.         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);  
  29.         fab.setOnClickListener(new View.OnClickListener() {  
  30.             @Override  
  31.             public void onClick(View view) {  
  32.                 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)  
  33.                         .setAction("Action"null).show();  
  34.             }  
  35.         });  
  36.   
  37.         btn.setOnClickListener(new View.OnClickListener() {  
  38.             @Override  
  39.             public void onClick(View v) {  
  40.   
  41.                 Toast toast = Toast.makeText(getApplicationContext(), "TOP RIGHT!", Toast.LENGTH_LONG);  
  42.                 // Here we can set the Gravity to Top and Right  
  43.                 toast.setGravity(Gravity.TOP | Gravity.RIGHT, 100200);  
  44.                 toast.show();  
  45.   
  46.                 Toast toast1 = Toast.makeText(getApplicationContext(), "TOP LEFT!", Toast.LENGTH_LONG);  
  47.                 // Set the Gravity to Top and Left  
  48.                 toast1.setGravity(Gravity.TOP | Gravity.LEFT, 100200);  
  49.                 toast1.show();  
  50.   
  51.                 Toast toast2 = Toast.makeText(getApplicationContext(), "BOTTOM LEFT!", Toast.LENGTH_LONG);  
  52.                 // Set the Gravity to Bottom and Left  
  53.                 toast2.setGravity(Gravity.BOTTOM | Gravity.LEFT, 100200);  
  54.                 toast2.show();  
  55.   
  56.                 Toast toast3 = Toast.makeText(getApplicationContext(), "BOTTOM RIGHT!", Toast.LENGTH_LONG);  
  57.                 // Set the Gravity to Bottom and Right  
  58.                 toast3.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 100200);  
  59.                 toast3.show();  
  60.   
  61.   
  62.             }  
  63.         });  
  64.   
  65.     }  
  66.   
  67.     @Override  
  68.     public boolean onCreateOptionsMenu(Menu menu) {  
  69.         // Inflate the menu; this adds items to the action bar if it is present.  
  70.         getMenuInflater().inflate(R.menu.menu_main, menu);  
  71.         return true;  
  72.     }  
  73.   
  74.     @Override  
  75.     public boolean onOptionsItemSelected(MenuItem item) {  
  76.         // Handle action bar item clicks here. The action bar will  
  77.         // automatically handle clicks on the Home/Up button, so long  
  78.         // as you specify a parent activity in AndroidManifest.xml.  
  79.         int id = item.getItemId();  
  80.   
  81.         //noinspection SimplifiableIfStatement  
  82.         if (id == R.id.action_settings) {  
  83.             return true;  
  84.         }  
  85.   
  86.         return super.onOptionsItemSelected(item);  
  87.     }  
  88. }  
content_main.xml:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"  
  5.     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     app:layout_behavior="@string/appbar_scrolling_view_behavior"  
  10.     tools:showIn="@layout/activity_main" tools:context=".MainActivity">  
  11.   
  12.     <TextView android:text="Hello World!" android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:id="@+id/textView" />  
  15.   
  16.     <Button  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:text="Click Here for Toast"  
  20.         android:id="@+id/button"  
  21.         android:layout_below="@+id/textView"  
  22.         android:layout_toRightOf="@+id/textView"  
  23.         android:layout_toEndOf="@+id/textView"  
  24.         android:layout_marginTop="167dp" />  
  25.   
  26.   
  27. </RelativeLayout>  
Output:

output
Ebook Download
View all
Learn
View all