Toasts for Android Studio

22 May, 2018
A toast provides a simple popup message that is displayed on the current activity UI screen (e.g. Main Activity).
Example :
Syntax :
// to get ContextContext context = getApplicationContext(); // message to displayString 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 toasttoast.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.
custom_toast.xml
<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
// get layout inflator object to inflate custom_toast layoutLayoutInflater inflater = getLayoutInflater();  // inflating custom_toast layoutView 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 objectToast toast = new Toast(getApplicationContext());  // to show toast at centre of screentoast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);  // display duration then show() method displays Toast.toast.setDuration(Toast.LENGTH_LONG);  // set custom layout toast.setView(layout);  // show toasttoast.show();
android
Technical Scripter
TechTips
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Find the Wi-Fi Password Using CMD in Windows?
Docker - COPY Instruction
Setting up the environment in Java
How to Run a Python Script using Docker?
Running Python script on GPU.
How to setup cron jobs in Ubuntu
How to Add External JAR File to an IntelliJ IDEA Project?
'dd' command in Linux
How to Delete Temporary Files in Windows 10?
How to set up Command Prompt for Python in Windows10 ?