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.
For instance:
TOP_RIGHT: - Toast toast = Toast.makeText(getApplicationContext(), "TOP RIGHT!", Toast.LENGTH_LONG);
-
- toast.setGravity(Gravity.TOP | Gravity.RIGHT, 100, 200);
- toast.show();
TOP_LEFT: - Toast toast = Toast.makeText(getApplicationContext(), "TOP LEFT!", Toast.LENGTH_LONG);
-
- toast.setGravity(Gravity.TOP | Gravity.LEFT, 100, 200);
- toast.show();
BOTTOM_LEFT: - Toast toast= Toast.makeText(getApplicationContext(), "BOTTOM LEFT!", Toast.LENGTH_LONG);
-
- toast.setGravity(Gravity.BOTTOM | Gravity.LEFT, 100, 200);
- toast.show();
BOTTOM_RIGHT: - Toast toast = Toast.makeText(getApplicationContext(), "BOTTOM RIGHT!", Toast.LENGTH_LONG);
-
- toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 100, 200);
- toast.show();
Source Code: 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);
-
- toast.setGravity(Gravity.TOP | Gravity.RIGHT, 100, 200);
- toast.show();
-
- Toast toast1 = Toast.makeText(getApplicationContext(), "TOP LEFT!", Toast.LENGTH_LONG);
-
- toast1.setGravity(Gravity.TOP | Gravity.LEFT, 100, 200);
- toast1.show();
-
- Toast toast2 = Toast.makeText(getApplicationContext(), "BOTTOM LEFT!", Toast.LENGTH_LONG);
-
- toast2.setGravity(Gravity.BOTTOM | Gravity.LEFT, 100, 200);
- toast2.show();
-
- Toast toast3 = Toast.makeText(getApplicationContext(), "BOTTOM RIGHT!", Toast.LENGTH_LONG);
-
- toast3.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 100, 200);
- toast3.show();
-
-
- }
- });
-
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
-
-
-
- int id = item.getItemId();
-
-
- if (id == R.id.action_settings) {
- return true;
- }
-
- return super.onOptionsItemSelected(item);
- }
- }
content_main.xml: - <?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>
Output: