Friday, March 23, 2012

How to Hide showed Toast Messages in Android?

In my App, there is a list, when a user clicks on an button, a toast message is being displayed, 10 items - 10 toast messages, so if the user clicks 10 times, then presses the button... he/she has to wait for some seconds, until he's able to read the menu option text.

I have found the below solution to hide the shows toast message.

public class SampleToastActivity extends Activity {
Toast myToast;
static int i=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i++;
if(myToast!=null){
      myToast.cancel();
      myToast.setText("Count"+i);
      }else{
          myToast = Toast.makeText(SampleToastActivity.this, "Sample"+i, 10);
         }
      myToast.show();
   }
    });
  }
}

No comments:

Post a Comment