views:

116

answers:

4

Hi,

I need to create a similar function to Toast.makeText(...) but more fast to disappear. I have seen such messagebox in the software "Le monde fr" when you select a button from the toolbar down. Its appear and disappear very fast if you move to another icon. I m looking to do the same functionnality but cannot figure out how to do that. Messagebox should not be modal, what i want is kind of a fast tooltip. The tooltip should appear and disappear fast. any ideas ?

A: 

Tooltip can be done through PopupWindows. I am assuming you have a grid of icons and want to provide tool tips that either disappear based on time or by clicking another icon -- correct me if I am wrong:

Create a global mPopupWindow and dismiss it before inflating a new one every time. You can use threads that dismiss it for you based on time or set code in your scroll-listener.

PopupWindow mPopupWindow = null;

mIconButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (mPopupWindow != null) {
                        mPopupWindow.dismiss();
                        mPopupWindow = null;
                    }

                    TextView tv = new TextView(getApplicationContext());
                    tv.setText(tooltipText);
                    tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon,
                            0, 0, 0);
                    mPopupWindow = new PopupWindow(tv);

                    mPopupWindow.setWindowLayoutMode(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);

                    mPopupWindow.showAsDropDown(v);

// write threads to disable it based on time as well

                }
            });
Sameer Segal
Very intersting, i didnt know about that !
Fabien
A: 

Have you tried altering the Toast notifications duration?

Toast.makeText(context, "My message", Toast.LENGTH_SHORT).show();

I would be wary of having a notification appear and disappear any quicker than this as the short duration is optimised for the minimum time a user takes to see and read the toast.

disretrospect
I did try, but i made toolbar from the gallery widget. And I wanted when you scroll on the toolbar to display a very short tooltip. The standard toast duration LENGTH_SHORT is too long. When you scroll fast to the last image/button you can still see old toast text from previous image on the the toolbar as they take much time to disappear.
Fabien
+1  A: 

Create a Toast object:

final Toast toast = Toast.makeText(this, "message", Toast.LENGTH_SHORT);

Create a Timer object:

Timer timer = new Timer();

Create a task which cancels the Toast object:

TimerTask task = new TimerTask() {

  @Override
  public void run() {
    // make sure to cancel the Toast in UI thread
    runOnUiThread(new Runnable() {

      @Override
      public void run() {
        toast.cancel();
      }
    });
  }
};

Run the cancel task after specified period of time

timer.schedule(task, 100);
radek-k
Thanks a lot, I may try this to see.
Fabien
A: 

you can do it this way. create a custom toast with a textview in it (R.id.text). use a class which extends from CountDownTimer class to control the amount of time you want to show the toast for. for

eg: MyCount counter = new MyCount(5000,1000);

will show the toast for 5 seconds. lower the values to get your desired results.

LayoutInflater inflater = getLayoutInflater();

View layout = inflater.inflate(R.layout.toastxml, (ViewGroup) findViewById(R.id.toast_layout_root)); toast = new Toast(this); toast.setView(layout);

TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Hello! This is a custom toast!");

MyCount counter = new MyCount(5000,1000); counter.start();

class MyCount extends CountDownTimer{

    public MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }
    @Override
    public void onFinish() {
        toast.cancel();
    }
        @Override
        public void onTick(long millisUntilFinished) {
            toast.show();
        }
    }
Umesh
also interesting!
Fabien