Android Toast With Example


Android Toast

Android Toast :-

Android, Toast is a notification message that pop up, display a certain amount of time, and automatically fades in and out, most people just use it for debugging purpose.

Android Toast Example:-



Toast.makeText(getApplicationContext(),"codewithandroid",Toast.LENGTH_SHORT)
.show();  
In this tutorial, we will show you two Toast examples :
  1. Normal Toast view.
  2. Custom Toast view.

1. Normal Toast View

File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical"

    android:gravity="center">



    <Button

        android:layout_gravity="center"

        android:id="@+id/buttonToast"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Show Toast" />



</LinearLayout>


File : MainActivity.java
package com.example.codewithandroid;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

    private Button button;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.buttonToast);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
                  public void onClick(View arg0) {

 Toast.makeText(getApplicationContext(),"Button is clicked", Toast.LENGTH_LONG).show();
           
 }
        });


    }
}


2. Custom Toast View

custom_toast.xml –.
<?xml version="1.0" encoding="utf-8"?><LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"    
android:id="@+id/custom_toast_layout_id"    
android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFF"
    android:orientation="horizontal" 
   android:padding="5dp" >
    <ImageView 
       android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="5dp" />
   
    <TextView        
        android:id="@+id/text"
        android:layout_width="wrap_content"   
        android:layout_height="fill_parent"
        android:textColor="#000" />
</LinearLayout>

: CustomToast.java 
package com.example.codewithandroid;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class CustomToast extends AppCompatActivity {
    private Button button;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_toast);
        button = (Button) findViewById(R.id.buttonToast);
        button.setOnClickListener(new View.OnClickListener() {

            @Override            
             public void onClick(View arg0) {

                // get your custom_toast.xml layout
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.activity_custom_toast,                        
               (ViewGroup) findViewById(R.id.custom_toast_layout_id));
                // set a dummy image
                ImageView image = (ImageView) layout.findViewById(R.id.image);                
                image.setImageResource(R.mipmap.ic_launcher);
                // set a message
                TextView text = (TextView) layout.findViewById(R.id.text);                
                text.setText("Button is clicked!");
                // Toast...
                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();            }
        });    }
}




Post a Comment

0 Comments