Tuesday, 16 October 2012

Custom Toast in Android Example

Make Custom Toast in android using another layout xml

android has "Toast" for display message to user like popup window its display some of time and automatically disappear, that popup display only text on that but user want to display image text and any other thing follow this step:-

  1. make layout of that which you want to display
  2. inflate that layout using "LayoutInflater"
  3. set that layout as a ToastView using setView method 


Button btn = (Button) findViewById(R.id.button1);
              btn.setOnClickListener(new OnClickListener() {
                     @Override
                     public void onClick(View v) {
                           LayoutInflater inflater = getLayoutInflater();
                           View layout = inflater.inflate(R.layout.toast,
                                         (ViewGroup) findViewById(R.id.toast_layout_root));
                           ImageView image = (ImageView) layout.findViewById(R.id.image);
                           image.setImageResource(R.drawable.ic_launcher);
                           TextView text = (TextView) layout.findViewById(R.id.text);
                           text.setText("custom toast Demo!");
                           Toast toast = new Toast(getApplicationContext());

                           // set postion of toast where you want to display
                           toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

                           // Visible time
                           toast.setDuration(Toast.LENGTH_LONG);
                           toast.setView(layout);
                           toast.show();
                     }
              });





Download Source Code


1 comment:

  1. I think this is an informative post and it is very useful and knowledgeable. Hire front end developer suited to your business needs. it adds great value to your business and online presence.

    hire front end developer

    ReplyDelete

Call web services easily

get source code from here, Download