April 14, 2011

How To display a Custom Toast Notification in Android

     If you need to display a simple Toast use this tutorial but if you want to customize your Toast Notification read the post below.

  1. Create your own layout for the Toast. Add it to the res/layout directory ( file - customizedtoast.xml).
  2. 
        
        
    
    
  3. Create a new LayourInflater, then call the inflate() method passing the Layout of the custom Toast and the root id (id of the linearLayour in our case) .
  4. LayoutInflater li = getLayoutInflater();
    View layout = li.inflate(R.layout.customizedtoast, 
      (ViewGroup) findViewById(R.id.customToastLayout));
    
  5. Create new Toast and pass the root of the layout to the setView() method of the Toast. Call the show() method.
  6. Toast toast = new Toast(getApplicationContext());
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setView(layout);
    toast.show();
    

Example:


Complete code :

main.java
package com.blog.customizedtoastnotification;

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.Toast;

public class main extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  Button btn = (Button) findViewById(R.id.button1);
  btn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {

    LayoutInflater li = getLayoutInflater();
    View layout = li.inflate(R.layout.customizedtoast,
      (ViewGroup) findViewById(R.id.customToastLayout));

    Toast toast = new Toast(getApplicationContext());
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setView(layout);
    toast.show();
   }
  });
 }
}
main.xml

 
 



3 comentarii:

Unknown said...

android application development association is vested with a monstrous commitment to make the best applications for its clients that can offer them some help with extending their bit of the pie and access customers. Along these lines, we at Webnet, guarantee that we make for you the applications that are incredibly powerful in the business division as well as rather furthermore offer you some help with emerging among your adversaries

Unknown said...

We value that affiliations are on the viewpoint for custom android app development that can satisfy their complete custom android application needs. This is the reason imagiacian offers a wide degree of custom android associations to affiliations.

chhassnain said...

We are the best SEO services company, we find and fix the errors on your website .As a SEO services company, we tend to facilitate brands reach new customers
through search optimization and social media organic visibility.

Post a Comment