Change toast message position in android

Set toast alignment location center, left, right, top, bottom,vertical, horizontal in activity screen using gravity.

Toast message position can be easily set after applying gravity tag on toast message object. With the setGravity() function developer can set toast message at any place on mobile screen. So here is the complete step by step tutorial for Change toast message position in android .

android-project-download-code-button

Below functions using with toast variable you can set it following places.

  • ToastMessage.setGravity(Gravity.BOTTOM, 0, 0) : Set toast message position at the bottom of the screen.
  • ToastMessage.setGravity(Gravity.TOP, 0, 0) : Display toast on top of the screen.
  • ToastMessage.setGravity(Gravity.CENTER, 0, 0) : Set toast message position at center of activity screen.
  • ToastMessage.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0) Set toast message at horizontally center of screen.
  • ToastMessage.setGravity(Gravity.CENTER_VERTICAL, 0, 0) : Set toast at vertically center of screen.
  • ToastMessage.setGravity(Gravity.LEFT, 0, 0) : Display toast message on left side of screen.
  • ToastMessage.setGravity(Gravity.RIGHT, 0, 0) : Display toast on right side of screen.

How to Change toast message position in android complete program.

Code for MainActivity.java file.

 package com.android_examples.com.toastposition;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

 Button BGravity;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 BGravity = (Button)findViewById(R.id.button1);
 BGravity.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 Toast ToastMessage = Toast.makeText(getApplicationContext(),"Change Toast Position",Toast.LENGTH_SHORT);
 
 //Set listed gravity here. 
 ToastMessage.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
 ToastMessage.show();
 
 }
 });
 }
}

Code for activity_main.xml layout file.

 <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.android_examples.com.toastposition.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="149dp"
 android:text="Show toast message at center of activity screen" />

</RelativeLayout>

Screenshot:

Change toast message position in android

Click Here To Download Change toast message position in android project.

Leave a Reply

Your email address will not be published. Required fields are marked *