Monday, June 15, 2015

// //

Create a custom toast in android

What is Toast?
A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. For example, navigating away from an email before you send it triggers a "Draft saved" toast to let you know that you can continue editing later. Toasts automatically disappear after a timeout.

What are we going to do?
Today we are going to make a custom toast. Really?
Yes!

1.
custom toast in android


2.
custom toast in android
main.xml



toast_layout.xml

    
   


CustomToast.java
import android.app.Activity;
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 Activity {
    private Button AnotherToast;
 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        AnotherToast = (Button)findViewById(R.id.AnotherToast);
        
        LayoutInflater inflater = getLayoutInflater();
        View layoutseeme = inflater.inflate(R.layout.toast_layout,
                                       (ViewGroup) findViewById(R.id.customtoast));

        ImageView image = (ImageView) layoutseeme.findViewById(R.id.image);
        image.setImageResource(R.drawable.icon);
        TextView text = (TextView) layoutseeme.findViewById(R.id.text);
        text.setText("Hellow! This custom toast will help you!");
       
  final Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layoutseeme);
        AnotherToast.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View arg0) {
          toast.show();
      }
  });
        
    }
}


Point-to-Note: this code is responsible of custom toast
View layoutseeme = inflater.inflate(R.layout.toast_layout,
                                       (ViewGroup) findViewById(R.id.customtoast));

//and we will call it to the toast:

 toast.setView(layoutseeme);