In android, Toast is a small popup notification which is used to display an information about the operation which we performed in our app. The Toast will show the message for a small period of time and it will disappear automatically after a timeout.
Generally, the size of Toast will be adjusted based on the space required for the message and it will be displayed on the top of main content of an activity for a short period of time.
For example, some of the apps will show a message like “Press again to exit” in toast, when we pressed a back button in the home page or showing a message like “saved successfully” toast, when we click on button to save the details.
Following is the pictorial representation of using Toast in android applications.
Create a Toast in Android
In android, we can create a Toast by instantiating an android.widget.Toast object using makeText() method. The makeText() method will take three parameters: application context, text message and the duration for the toast. We can display the Toast notification by using show() method.
Following is the syntax of creating a Toast in android applications.
Toast.makeText(context, "message", duration).show();
If you observe above syntax, we defined a Toast notification using makeText() method with three parameters, those are
Parameter | Description |
---|---|
context | It’s our application context. |
message | It’s our custom message which we want to show in Toast notification. |
duration | It is used to define the duration for notification to display on screen. |
We have a two ways to define the Toast duration, either in LENGTH_SHORT or LENGTH_LONG to display the toast notification for short or longer period of time.
Following is the example of defining a Toast in android applications.
Toast.makeText(MainActivity.this, "Details Saved Successfully.", Toast.LENGTH_SHORT).show();
Now we will see how to implement a Toast notification in android applications with examples.
Android Toast Notification Example
Create a new android application using android studio and give names as ToastExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.
Now open an activity_main.xml file from \res\layout path and write the code like as shown below
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast"
android:layout_marginTop="200dp" android:layout_marginLeft="140dp"/>
</LinearLayout>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast"
android:layout_marginTop="200dp" android:layout_marginLeft="140dp"/>
</LinearLayout>
If you observe above code we created a one Button control in XML Layout file to show the toast notification when we click on Button.
Once we are done with creation of layout with required controls, we need to load the XML layout resource from our activity onCreate() callback method, for that open main activity file MainActivity.java from \java\com.tutlane.toastexample path and write the code like as shown below.
MainActivity.java
package com.tutlane.toastexample;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.btnShow);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "You Clicked on Button..", Toast.LENGTH_SHORT).show();
}
});
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.btnShow);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "You Clicked on Button..", Toast.LENGTH_SHORT).show();
}
});
}
}
If you observe above code we are created a toast notification using makeText() method and showing a toast notification on Button click.
Generally, during the launch of our activity, onCreate() callback method will be called by android framework to get the required layout for an activity.
Output of Android Toast Notification Example
When we run above example using android virtual device (AVD) we will get a result like as shown below.
If you observe above result we created a toast notification and shown it on Button click based on our requirements.
Change the Position of Android Toast Notification
By default, the android Toast notification will always appear near the bottom of the screen, centred horizontally like as shown in above image.
In case if we want to change the position of Toast notification, we can do it by using setGravity(int, int, int) method. The setGravity() method will accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.
Following is the example of changing the position of android Toast notification to top-right based on offset positions by using setGravity() method.
Toast toast = Toast.makeText(MainActivity.this, "You Clicked on Button..", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.RIGHT, 100, 250);
toast.show();
toast.setGravity(Gravity.TOP|Gravity.RIGHT, 100, 250);
toast.show();
If you want to move the position of toast to right, increase the value of the second parameter. To move it down, increase the value of the last parameter.
Android Toast Positioning Example
Following is the example of changing the position of android toast notification to top-right side using setGravity() method.
We need to modify our main activity file MainActivity.java code like as shown below.
MainActivity.java
package com.tutlane.toastexample;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.widget.Button;import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.btnShow);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//display toast message in top right side
Toast toast = Toast.makeText(MainActivity.this, "You Clicked on Button..", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.RIGHT, 100, 250);
toast.show();
}
});
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.btnShow);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//display toast message in top right side
Toast toast = Toast.makeText(MainActivity.this, "You Clicked on Button..", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.RIGHT, 100, 250);
toast.show();
}
});
}
}
If you observe above code we are changing the position of android toast notification using setGravity() property.
Output of Android Toast Positioning Example
When we run above example using android virtual device (AVD) we will get a result like as shown below.
If you observe above result we changed the position of toast notification to top right side on Button click based on our requirements.
In case if we want to change the style of toast notification, then we can do it by creating a custom XML layout file. To know more about custom toast view, check this Android Custom Toast with Example.
No comments:
Post a Comment