how to hide things after certain time in android studio - java

I am creating a video player in android studio. I want to hide buttons, layout and media controller after 5 seconds and I am using gestures for different properties but the issue is that when I apply 2 to 3 gestures in a certain time, after 5 seconds the buttons and media controller start blinking. I use this code for stying on screen
centerlayout.setOnTouchListener(new LinearLayout.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent m) {
if (gestureDetectorc.onTouchEvent(m)) {
if(m.getAction()==MotionEvent.ACTION_UP){
handler.postDelayed(new Runnable() {
#Override
public void run() {
hide();
}
},5000);
}
}
return true;
}
});

Declare the runnable in global
Runnable mRunnable = new Runnable() {
#Override
public void run() {
hide();
}
};
call removeCallbacks before you call postDelay
centerlayout.setOnTouchListener(new LinearLayout.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent m) {
if (gestureDetectorc.onTouchEvent(m)) {
if(m.getAction()==MotionEvent.ACTION_UP){
handler.removeCallbacks(mRunnable);//add this
handler.postDelayed(mRunnable, 5000);
}
}
return true;
}
});

Related

Can't change Android ImageView's Y in Java file

I am trying to make a Helicopter-like game (as you can probably tell, I'm using "Rocket Man" instead of a helicopter). I'm new to Android programming and to Stack Overflow, so don't pick apart my code too much (please). Anyway, right now I'm just trying to get the rocketman to move up while touch is held down, and move down while touch is released.
It isn't working out very well.
I'm using a Moto XT886 for testing, (in case that's relevent). When I run the application, it starts up and loads the MainActivity perfectly well. When I touch the screen the first time (boolean start == true), the text views are hidden, as they are supposed to. However, on the second, third, fourth and all the rest of the times I touch the screen (Start == false), nothing happens. There are no errors in Log Cat, the code doesn't seem to recognize the touch.
I'm pretty sure that it has something to do with those while loops, but I'm using this application mainly to learn Android Java and I don't know enough (yet) to recognize that I'm doing something wrong. Are you able to put a while loop with the conditions as the MotionEvent of an onTouchListner? Or is it something else?
Either way, I appreciate the help, this is an awesome website (this is the first question I couldn't find already asked on Stack Overflow, and for all I know, mine could've already been answered)!
Here's the code to my MainActivity, if you need the .xml code as well, (though I'm pretty sure that's unnecessary) just say the word.
public class MainActivity extends ActionBarActivity {
boolean Start;
RelativeLayout rLayout;
TextView gameTitle;
TextView clickStart;
TextView highScore;
Handler handler;
ImageView rocketMan;
float Y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
rLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (Start == true) {
gameTitle.setVisibility(View.GONE);
clickStart.setVisibility(View.GONE);
highScore.setVisibility(View.GONE);
Start = false;
}
if (Start = false) {
while (event.getAction() == MotionEvent.ACTION_DOWN) {
Y = -7;
new Thread(new Runnable() {
#Override
public void run() {
while (true) {
try {
Thread.sleep(100);
handler.post(new Runnable() {
#Override
public void run() {
HeliMove();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
while (event.getAction() != MotionEvent.ACTION_DOWN) {
Y = 7;
new Thread(new Runnable() {
#Override
public void run() {
while (true) {
try {
Thread.sleep(100);
handler.post(new Runnable() {
#Override
public void run() {
HeliMove();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
return true;
}
});
}
private void initialize() {
rLayout = (RelativeLayout) findViewById(R.id.mainlayout);
Start = true;
gameTitle = (TextView) findViewById(R.id.rocketmanTitle);
clickStart = (TextView) findViewById(R.id.tapToStart);
highScore = (TextView) findViewById(R.id.highScore);
handler = new Handler();
rocketMan = (ImageView) findViewById(R.id.rocketMan);
}
private void HeliMove() {
rocketMan.setY(rocketMan.getY() + Y);
}
}
Thanks a million!

Android - OnTouchListener() is triggered too early

I have an activitiy where I have a button and when a click on this button I want to set a TextView with some value, so I used onClickListening and it is working:
ButtonPlus.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
ponts = ponts + 1;
resultadoTextView.setText(Integer.toString(ponts));
}
});
But the problem is that I want to keep increasing this textView's value while the button keep being pressed so I tried to use the OnTouchLister:
ButtonPlus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
ponts = ponts + 1;
resultTextView.setText(Integer.toString(ponts));
}
});
the problem is that when I give a fast click in the button it increments the TextView's value too much and I want the onTouchListener to be activated just after some time that the button was pressed.
any help please?
Use some additional counter.
int additionalCounter = 0;
ButtonPlus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
++additionalCounter;
if (additionalCounter % X == 0) {
ponts = ponts + 1;
resultTextView.setText(Integer.toString(ponts));
}
}
});
You can set X as you want, i.e. setting it to 5 would make touch event working 5 times slower.
Try this code.
ButtonPlus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
ponts = ponts + 1;
resultTextView.setText(Integer.toString(ponts));
ButtonPlus.setClickable(false);
//wait 1 second
ButtonPlus.postDelayed(new Runnable() {
#Override
public void run() {
ButtonPlus.setClickable(true);
}
}, 1000);
return false;
}
});

Do action while long click

Hello I am trying to perform some actions while the user is holding down a button.
My problem is that my Runnable wont "run".
Here is my code:
#Override
public boolean onLongClick(View v) {
final Runnable r = new Runnable()
{
public void run()
{//do the forwarding logic here
int test = 0;
if(holdingDown)
test++;
else
return;
Log.i("test", test+"");
}
};
r.run();
}
return false;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
{
holdingDown= false;
Log.i("holdingDown", "false");
break;
}
}
return false;
}
The onTouch is for detecting when the user stop pressing the button. When I look at my logs I see at that the Runnable runs only once.
My test log get only the value 1.
The log call for Log.i("holdingDown", "false") is getting triggered at the right time, only when I stop touching the button.
Why is it that my Runnable won't run? Thanks.
EDIT:
I tried this code:
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
holdingDown = true;
new Thread(new Runnable() {
#Override
public void run() {
if(holdingDown)
{
int test = 0;
test++;
Log.i("test", test+"");
}
else
return;
}
}).start();
return false;
}
Its till not working.
You could try using a Thread instead of a Runnable like this:
Thread thread = new Thread() {
#Override
public void run() {
//code you want to run on long press
} };
thread.start();
OR
You could try putting the Runnable inside a Thread like this:
Thread thread = new Thread(new Runnable() {
public void run() {
// code you want to run on long press
}
});
thread.start();
UPDATE: - try this?
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
holdingDown = true;
new Thread(new Runnable() {
#Override
public void run() {
if (holdingDown) {
int test = 0;
test++;
Log.i("test", test + "");
} else {
Log.i("test", "else");
}
return;
}
}).start();
return false;
}
You don't do r.run() to start a thread, that only runs it once.
You either do new Thread(r).start(); or you use a ScheduledExecutorService.
You can create a class called HoldingDown overriding Runnable and instantiate a Thread object passing a new instance of your HoldingDown and call the start method.
Also, you can use the mousePressed and mouseReleased events of MouseListener.
You should start a new Thread in your mousePressed, store the instance somewhere and stop it on mouseReleased.
See the documentation for more details.

LongPress on OSMDroid map is not working

Im creating an OpenStreetMaps application using OSMDroid lib. I need LongPress gesture to add markers into an OverlayItem array to show them all, but i can't access to LongPress anyway.
I have tried with GestureListener, overridden the onTouch event, using GestureDetector(which is deprecated nowadays) and I can't find a solution for this problem.
The only way i have get the gesture is through setOnLongClickListener, but I have to choose between having longpress or panning the map, and I'm greedy and I want both :p
Have anyone any ideas?
Regards!
I have encounter a solution through OSMBonusPack library.
The object MapsEventsReceiver grants you a simple way to handle map's gestures.
You have to override singleTapUpHelper and longPressHelper methods and create a new overlay with this handler.
Here's the code:
MapEventsReceiver mReceive = new MapEventsReceiver() {
#Override
public boolean singleTapUpHelper(IGeoPoint arg0) {
Log.d("debug", "Single tap helper");
//your onSingleTap logic here
return false;
}
#Override
public boolean longPressHelper(IGeoPoint arg0) {
Log.d("debug", "LongPressHelper");
//your onLongPress logic here
setMarker(arg0);
return false;
}
};
//Creating a handle overlay to capture the gestures
MapEventsOverlay OverlayEventos = new MapEventsOverlay(getBaseContext(), mReceive);
map.getOverlays().add(OverlayEventos);
//Refreshing the map to draw the new overlay
map.invalidate();
//I use this method to set the marker on touchPoint
protected void setMarker(IGeoPoint arg0) {
touchPoint = (GeoPoint) arg0;
targetPoint = touchPoint;
path.addPoint(targetPoint);
//Listener to handle item's(markers) events
myOnItemGestureListener = new OnItemGestureListener<OverlayItem>() {
#Override
public boolean onItemLongPress(int arg0, OverlayItem arg1) {
Log.d("debug", "Testing long tap on item");
//your item onLongPress logic here
return false;
}
#Override
public boolean onItemSingleTapUp(int index, OverlayItem item) {
Log.d("debug", "Testing single tap on item");
//your item onSingleTap logic here
return true;
}
};
itemarray.add(new OverlayItem("Marker " + cnt, "Info about marker " + cnt, touchPoint));
ItemizedOverlayWithFocus<OverlayItem> overlayDeItems = new ItemizedOverlayWithFocus<OverlayItem>(getBaseContext(), itemarray, myOnItemGestureListener);
map.getOverlays().add(overlayDeItems);
overlayDeItems.setFocusItemsOnTap(true);
map.invalidate();
itemarray = new ArrayList<OverlayItem>();
cnt++;
}
private void setupGestureListener() {
GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
#Override
public void onLongPress(MotionEvent e) {
//do something
}
};
final GestureDetector gestureDetector = new GestureDetector(getContext(), gestureListener);
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
});
}

Android custom double tap location

I'm currently creating a custom double tap using the onClickListener with the following code:
newImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
counterTap++;
Timer t = new Timer("Double tap counter");
t.schedule(new TimerTask() {
#Override
public void run() {
counterTap = 0;
}
}, 0, 300);
if(counterTap >= 2) {
newImage.setVisibility(FrameLayout.GONE);
counterTap = 0;
}
}
});
The problem I'm facing is as follows:
Whenever I tap the ImageView the event does fire. However, the second time I tap the ImageView, the above code only executes when clicking on the exact same position on the ImageView as before.
Rather use a onTouchListener. In the touch listener in the onTouch method you can return false the first time you tapped which means the event was not handled, the second time you return true and the touch listener will handle the event as finished. See my example below, you can use a similar method
new OnTouchListener() {
boolean doubleTap = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
if (!doubleTap) {
doubleTap = true;
return false;
} else {
return true;
}
}
}
};
this might solve your issue.
EDIT : This is a better opotion
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onDown(MotionEvent e) {
return true;
}
// double tap event
#Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}
}
This question might also help you get to the best answer

Categories

Resources