How to create a fast auto dismiss message box under ANDROID?
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 anothe开发者_开发技巧r 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 ?
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);
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
}
});
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.
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();
}
}
精彩评论