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:
- A Gravity constant,
- An x-position offset,
- A y-position offset.
TOP_RIGHT:
- Toast toast = Toast.makeText(getApplicationContext(), "TOP RIGHT!", Toast.LENGTH_LONG);
- // Here we can set the Gravity to Top and Right
- toast.setGravity(Gravity.TOP | Gravity.RIGHT, 100, 200);
- toast.show();
- Toast toast = Toast.makeText(getApplicationContext(), "TOP LEFT!", Toast.LENGTH_LONG);
- // Set the Gravity to Top and Left
- toast.setGravity(Gravity.TOP | Gravity.LEFT, 100, 200);
- toast.show();
- Toast toast= Toast.makeText(getApplicationContext(), "BOTTOM LEFT!", Toast.LENGTH_LONG);
- // Set the Gravity to Bottom and Left
- toast.setGravity(Gravity.BOTTOM | Gravity.LEFT, 100, 200);
- toast.show();
- Toast toast = Toast.makeText(getApplicationContext(), "BOTTOM RIGHT!", Toast.LENGTH_LONG);
- // Set the Gravity to Bottom and Right
- toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 100, 200);
- toast.show();
Main_Activity.java
- package com.example.abhijeet.toastpositiondemo;
- import android.os.Bundle;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.view.Gravity;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.Button;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- public Button btn;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- btn = (Button)findViewById(R.id.button);
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
- .setAction("Action", null).show();
- }
- });
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast toast = Toast.makeText(getApplicationContext(), "TOP RIGHT!", Toast.LENGTH_LONG);
- // Here we can set the Gravity to Top and Right
- toast.setGravity(Gravity.TOP | Gravity.RIGHT, 100, 200);
- toast.show();
- Toast toast1 = Toast.makeText(getApplicationContext(), "TOP LEFT!", Toast.LENGTH_LONG);
- // Set the Gravity to Top and Left
- toast1.setGravity(Gravity.TOP | Gravity.LEFT, 100, 200);
- toast1.show();
- Toast toast2 = Toast.makeText(getApplicationContext(), "BOTTOM LEFT!", Toast.LENGTH_LONG);
- // Set the Gravity to Bottom and Left
- toast2.setGravity(Gravity.BOTTOM | Gravity.LEFT, 100, 200);
- toast2.show();
- Toast toast3 = Toast.makeText(getApplicationContext(), "BOTTOM RIGHT!", Toast.LENGTH_LONG);
- // Set the Gravity to Bottom and Right
- toast3.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 100, 200);
- toast3.show();
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
- android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- app:layout_behavior="@string/appbar_scrolling_view_behavior"
- tools:showIn="@layout/activity_main" tools:context=".MainActivity">
- <TextView android:text="Hello World!" android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/textView" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Click Here for Toast"
- android:id="@+id/button"
- android:layout_below="@+id/textView"
- android:layout_toRightOf="@+id/textView"
- android:layout_toEndOf="@+id/textView"
- android:layout_marginTop="167dp" />
- </RelativeLayout>


Join the conversation! Your thoughts help the community grow.