Toasts for Android Studio
A toast provides a simple popup message that is displayed on the current activity UI screen (e.g. Main Activity).
Example:
Syntax:
// to get Context
Context context = getApplicationContext();
// message to display
String text = "Toast message";
// toast time duration, can also set manual value
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
// to show the toast
toast.show();
We can also create toast with single line by passing variables directly to makeText() function. This method takes three parameters context, popup text message, the toast duration. After creating Toast object you can display the toast by using show() method.
Example :
Toast.makeText(MainActivity.this, "Error"+ msg, Toast.LENGTH_SHORT).show();
Creating a Custom Toast
If you are not satisfied with simple Toast view in Android, then you can go ahead to make a custom Toast. Actually, custom Toast is a modified simple Toast that makes your UI more attractive. So that when you create a custom Toast then two things are required, one is XML (custom_toast.xml) required for layout view of custom Toast and another is activity class (custom_activity.class) file where you can write Java code. Java class file passes the root View object to the setView(View) method.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00AABB"
android:orientation="horizontal"
android:padding="8dp">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:src="@drawable/ic_warning" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="8pt"
android:textStyle="italic" />
</LinearLayout>
This is a XML layout for example purposes, so if you want your own design then you can create your own XML.
Custom_Activity.class
package com.example.hp.splashscreen;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
// get layout inflator object to inflate custom_toast layout
LayoutInflater inflater = getLayoutInflater();
// inflating custom_toast layout
View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup)findViewById(R.id.custom_toast_container));
// Find TextView elements with help of layout object.
TextView text = (TextView)layout.findViewById(R.id.text);
// set custom Toast message.
text.setText("Custom Toast message");
// Create the Toast object
Toast toast = new Toast(getApplicationContext());
// to show toast at center of screen
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
// set display duration, then show() method displays Toast.
toast.setDuration(Toast.LENGTH_LONG);
// set custom layout
toast.setView(layout);
// show toast
toast.show();
Contact Us