gwt tooltip with a delay - gwt

I need to implement a tooltip with a time delay, say, when user point to a widget, the tooltip popup 2 seconds later. i tried the timer but then it shows again and again, i guess that's because the timer fire the tooltip every VISIBLE_DELAY seconds. is there anyway i can fire it only once? or is there any Class that can sleep for 2 seconds then i can call tooltip.show()? Thanks.
removeDelay = new Timer() {
#Override
public void run() {
ToolTip.this.show();
}
};
removeDelay.schedule(VISIBLE_DELAY);

Call cancel after you show the tooltip.
new Timer() {
#Override
public void run() {
ToolTip.this.show();
cancel();
}
};

Related

Second activity is still start on handler although press back button for quiting app

My application has an Introduce activity that show process bar before using app.
pb = (ProgressBar) findViewById(R.id.pb_loader);
final Handler h = new Handler() {
#Override
public void handleMessage(Message message) {
pb.setVisibility(View.INVISIBLE);
Intent it = new Intent(FirstIntroActivity.this, SecondIntroActivity.class);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(it);
}
};
h.sendMessageDelayed(new Message(), 3000);
But after I press BACK button to exit application, my phone is turn back to application and go to SECOND activity ( after 3000ms ). How to resolve this error?
Alternative is to use a Timer to schedule start of your second activity.we can cancel starting the second activity by cancelling timer in OnBackPressed() callback.
private Timer timer;
#Override
protected void onResume() {
super.onResume();
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
// add code to start your second activity
}
}, 2000);
}
#Override
public void onBackPressed() {
timer.cancel();
super.onBackPressed();
}

Get delay on UI thread

I'm setting color of a listview item using the following code parent.getChildAt(itemPosition).setBackgroundColor(Color.parseColor("#FF9494"));
This piece of code I'm writing in OnItemClickListener.
After setting the color I want to keep this color for a time of 4 Seconds and then restore the color of the item to its previous(say White).
I tried putting a sleep on the UI thread, but I know that it is not a correct approach.
Can anyone suggest me how to achieve this?
parent.getChildAt(itemPosition).setBackgroundColor(Color.parseColor("#FF9494"));
// Start new Thread that sets the color back in 4 seconds
new Thread(new Runnable() {
#Override
public void run() {
SystemClock.sleep(4000); // Sleep 4 seconds
// Now change the color back. Needs to be done on the UI thread
runOnUiThread(new Runnable() {
#Override
public void run() {
parent.getChildAt(itemPosition).setBackgroundColor(Color.parseColor("#000000")); // use whatever other color you want here
}
});
}
}).start();
The main thread has a looper running within. For this it is possible to schedule a Runnable delayed. Within an OnItemClickListener your code could be as simple as:
#Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
view.setBackgroundColor(Color.parseColor("#FF9494"));
view.postDelayed(new Runnable() {
#Override
public void run() {
view.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
}, 4000);
}
May be you can try to implement an asyncTask which can then be called from onItemClickListener. The doInBackground method of this asyncTask can contain a sleep method to avoid calling the onPostExecute for a while. In the onPostExecute, you can then reset the color as desired.
If sleep method can't be written in the doInBackground method as I am expecting, then put the sleep method also inside the onPostExecute method before changing the text color.

continuous actions to be performed when I long tapped MGWT button until the touch end event fired

I want to invoke a method continuously when I used MGWT button long tap handler, this should be done until I release the button. i.e; until the touch end event fired. For this I had written Timer inside the MGWT Button's long tap handler.I continuously calling the my task method inside the run method of the Timer. my code:
upButton.addLongTapHandler(new LongTapHandler() {
#Override
public void onLongTap(LongTapEvent event) {
upBtnTimer = new Timer() {
#Override
public void run() {
if(getValue() >= maxValue){
Window.alert("max val reached");
upBtnTimer.cancel();
}else{
setValue(getValue() + RATE);
}
}
};
upBtnTimer.scheduleRepeating(100);
}
});
And I also wrote touch end handler to the upButton. this is:
upButton.addTouchEndHandler(new TouchEndHandler() {
#Override
public void onTouchEnd(TouchEndEvent event) {
if(upBtnTimer!=null){
upBtnTimer.cancel();
upBtnTimer = null;
}
}
});
this is OK when I'm testing my mobile application on browser, but when I installed my application in iOS/Android device, this is not working. Only single tap event firing.
If you are clear with my requirement please tell me if there is another approach to do this. Thanks in advance.

GWT Delaying an action after an event trigger

I want to have the user click a button, then they will see a "Toast" message popup, which fades away , then the action that the button click performs should happen. What is the best way to do this? Should I trigger a Timer on catching the click event and let it timeout (While the toast is being displayed ) and then call the handling code or is there any built in delay mechanism in event handling I can use?
I don't want my toast to be involved at all in the event handling
If I really follow your requirements the following code should do:
// interface of the "Toast" no matter what the implementation actually is
public interface Toast
{
void open( String message );
void closeFadingAway();
}
// calling code
public class ClientCode
{
private static final int myDelay = 1000; // 1 second in millis
private Toast myToast;
void onMyAction()
{
myToast.open( "Your action is being handled..." );
Scheduler.get().scheduleFixedDelay( new RepeatingCommand()
{
#Override
public boolean execute()
{
myToast.closeFadingAway();
performAction();
return false; // return false to stop the "repeating" = executed only once
}
}, myDelay );
}
void performAction()
{
// do something interesting
}
}
Now, if you actually mean to be able to interrupt the action when the user presses some button in the toast this is a different story.
If you are using a popup panel you could use the addCloseHandler on the popup panel and from here call the method that would have otherwise been called by the button.
popUpPanel.addCloseHandler(new CloseHandler<PopupPanel>(){
#Override
public void onClose(CloseEvent<PopupPanel> event) {
// TODO Do the closing stuff here
}
});
So when the PopupPanel disappears and the close event triggers you can do your magic there.

GWT onResize & mouse up

I have a window resizeHandler, it is working fine. Except that I'm just interested in the final dimension of the window, in other words, I'm interested in the dimension of the window when the mouse button is released. Is there a way I can listen to window mouse events?
I've written a piece of code that accomplishes my goal but I'd prefer something more obvious. resizeRequestId is a field:
private void processResize() {
final Timer timer = new Timer() {
final Size windowSize = getWindowDimention();
final int requestId = ++resizeRequestId;
#Override
public void run() {
final boolean isLatestRequest = requestId == resizeRequestId;
if (isLatestRequest) {
//DO SOMETHING WITH windowSize
}
}
};
timer.schedule(100);
}
The browser doesn't pass along events that happen outside of the page, and the window resize counts as outside the page. That said, you still get a hook for resize actions of the entire browser window:
Window.addResizeHandler(new ResizeHandler() {
public void onResize(ResizeEvent event) {
//get, process window resize behavior
}
});
For some browsers and some resizes, you'll get lots of events as the mouse moves, and for others, you'll only get the complete one. In firefox, for example, the resize handle in the corner sends every change that is made, while the side handles each send only once the user releases the mouse. Minimizing and maximizing the window also result in a single event.
Colin is right.
Moreover, if you do a lot of calculations "on resize" (e.g. forceLayout), it is a good idea to add a Timer. This way the calculations will be fired once every...10th of second?
final Timer resizeTimer = new Timer() {
#Override
public void run() {
mainPanel.forceLayout();
}
};
Window.addResizeHandler(new ResizeHandler() {
public void onResize(ResizeEvent event) {
int height = event.getHeight();
mainPanel.setHeight(height + "px");
resizeTimer.cancel();
resizeTimer.schedule(100);
}
});
That's it