Tuesday, March 16, 2021

Custom Toast

Android Custom Toast Example

You are able to create custom toast in android. So, you can display some images like congratulations or loss on the toast. It means you are able to customize the toast now.


activity_main.xml

Drag the component that you want to display on the main activity.

File: activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.customtoast.MainActivity">  
  8.   
  9.     <TextView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="Hello World!"  
  13.         app:layout_constraintBottom_toBottomOf="parent"  
  14.         app:layout_constraintLeft_toLeftOf="parent"  
  15.         app:layout_constraintRight_toRightOf="parent"  
  16.         app:layout_constraintTop_toTopOf="parent" />  
  17.   
  18. </android.support.constraint.ConstraintLayout>  


customtoast.xml

Create another xml file inside the layout directory. Here we are having ImageView and TextView in this xml file.

File: customtoast.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:id="@+id/custom_toast_layout"  
  6.     android:orientation="vertical"  
  7.     android:background="#F14E23"  
  8.     >  
  9.   
  10.     <ImageView  
  11.         android:id="@+id/custom_toast_image"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:contentDescription="Hello world"  
  15.         android:src="@drawable/jtp_logo"/>  
  16.   
  17.     <TextView  
  18.         android:id="@+id/custom_toast_message"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:contentDescription="To"  
  22.         android:text="JavaTpoint custom Toast" />  
  23. </LinearLayout>  

Activity class

Now write the code to display the custom toast.

File: MainActivity.java
  1. package example.javatpoint.com.customtoast;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.Gravity;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends AppCompatActivity {  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.   
  18.         //Creating the LayoutInflater instance  
  19.         LayoutInflater li = getLayoutInflater();  
  20.         //Getting the View object as defined in the customtoast.xml file  
  21.         View layout = li.inflate(R.layout.customtoast,(ViewGroup) findViewById(R.id.custom_toast_layout));  
  22.   
  23.         //Creating the Toast object  
  24.         Toast toast = new Toast(getApplicationContext());  
  25.         toast.setDuration(Toast.LENGTH_SHORT);  
  26.         toast.setGravity(Gravity.CENTER_VERTICAL, 00);  
  27.         toast.setView(layout);//setting the view of custom toast layout  
  28.         toast.show();  
  29.     }  
  30. }  

Output:

android custom toast example output 1 android custom toast example output 2

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile