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. import android.os.Bundle;
  3. import android.support.design.widget.FloatingActionButton;
  4. import android.support.design.widget.Snackbar;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.support.v7.widget.Toolbar;
  7. import android.view.Gravity;
  8. import android.view.View;
  9. import android.view.Menu;
  10. import android.view.MenuItem;
  11. import android.widget.Button;
  12. import android.widget.Toast;
  13. public class MainActivity extends AppCompatActivity {
  14. public Button btn;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  20. setSupportActionBar(toolbar);
  21. btn = (Button)findViewById(R.id.button);
  22. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  23. fab.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View view) {
  26. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  27. .setAction("Action", null).show();
  28. }
  29. });
  30. btn.setOnClickListener(new View.OnClickListener() {
  31. @Override
  32. public void onClick(View v) {
  33. Toast toast = Toast.makeText(getApplicationContext(), "TOP RIGHT!", Toast.LENGTH_LONG);
  34. // Here we can set the Gravity to Top and Right
  35. toast.setGravity(Gravity.TOP | Gravity.RIGHT, 100, 200);
  36. toast.show();
  37. Toast toast1 = Toast.makeText(getApplicationContext(), "TOP LEFT!", Toast.LENGTH_LONG);
  38. // Set the Gravity to Top and Left
  39. toast1.setGravity(Gravity.TOP | Gravity.LEFT, 100, 200);
  40. toast1.show();
  41. Toast toast2 = Toast.makeText(getApplicationContext(), "BOTTOM LEFT!", Toast.LENGTH_LONG);
  42. // Set the Gravity to Bottom and Left
  43. toast2.setGravity(Gravity.BOTTOM | Gravity.LEFT, 100, 200);
  44. toast2.show();
  45. Toast toast3 = Toast.makeText(getApplicationContext(), "BOTTOM RIGHT!", Toast.LENGTH_LONG);
  46. // Set the Gravity to Bottom and Right
  47. toast3.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 100, 200);
  48. toast3.show();
  49. }
  50. });
  51. }
  52. @Override
  53. public boolean onCreateOptionsMenu(Menu menu) {
  54. // Inflate the menu; this adds items to the action bar if it is present.
  55. getMenuInflater().inflate(R.menu.menu_main, menu);
  56. return true;
  57. }
  58. @Override
  59. public boolean onOptionsItemSelected(MenuItem item) {
  60. // Handle action bar item clicks here. The action bar will
  61. // automatically handle clicks on the Home/Up button, so long
  62. // as you specify a parent activity in AndroidManifest.xml.
  63. int id = item.getItemId();
  64. //noinspection SimplifiableIfStatement
  65. if (id == R.id.action_settings) {
  66. return true;
  67. }
  68. return super.onOptionsItemSelected(item);
  69. }
  70. }
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. <TextView android:text="Hello World!" android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:id="@+id/textView" />
  14. <Button
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="Click Here for Toast"
  18. android:id="@+id/button"
  19. android:layout_below="@+id/textView"
  20. android:layout_toRightOf="@+id/textView"
  21. android:layout_toEndOf="@+id/textView"
  22. android:layout_marginTop="167dp" />
  23. </RelativeLayout>
Output:

output