OnTouchEvent reacts multiple while touching the screen - java

I've got the problem, that the onTouchEvent reacts to staying with the finger on screen and move the finger. That isn`t my aim. I want, that the OnTouchEvent only reacts if i tap on the screen, after this the OnTouchEvent should reactivate itself and the user have to click another time.
Anyone can solve my problem? I mean it's a little bit tricky with my timer, because i never leave the method OnTouchEvent.
public boolean onTouchEvent(MotionEvent me){
if(!started){
started = true;
taptostart.setVisibility(View.GONE);
expla.setVisibility(View.GONE);
//start the timer
timer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
changePos();
}
});
}
}, 0, 20);
}else{
activateHo();
}
return false;
}

public boolean onTouchEvent(MotionEvent me){
if(me.getAction() == MotionEvent.ACTION_DOWN){
//Do what you want here, when the view is touched
}
return false;
}

Related

android - how to run some code after the "power button" is pushed

I have an app that plays some music file while the application is open but when i press the power button the sound keeps playing for some reason. Is there a way to override the power button so it will also stop the media player when pushed ?
MediaPlayer mp;
protected void onCreate(Bundle savedInstanceState) {
...
...
mp = MediaPlayer.create(this, mAudio[0]);
mp.start();
...
...
}
I would like to call mp.stop() when the power button is pressed.
Override onKeyDown and check if it's KEYCODE_POWER.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
// Stop the media player here
return true;
}
return super.onKeyDown(keyCode, event);
}
Add this method:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
// stop the media from playing
return true;
}
return super.onKeyDown(keyCode, event);
}
This method is being activated when a key is being pressed. It checks what key is pressed and reacts for it.
First of all you need to understand, when you want to pause or stop the sound. If you want to do it, when you exit/hide an application, then overriding onKeyDown won't help. I think better approach in this case will be:
protected void onCreate(Bundle onSaveInstanceState) {
super.onCreate(onSaveInstanceState);
mp = MediaPlayer.create(this, mAudio[0]);
}
protected void onStart() {
super.onStart();
mp.start();
}
protected void onStop() {
super.onStop();
mp.stop();
}
But if you want to stop your sound only when Power button is pressed, then yes, you need to override onKeyDown

How to catch the OnTouchEvent just in a particular situation?

I am about to write a little quiz app. I have 4 Buttons which represent the possibilities of answers. When one button is depressed It should blink red or green to indicate right or wrong. So far no problem.
But the next step should be to slide out the old question and to slide in the new one. This should initiate by a touch of the user anywhere on the screen.
How can I achieve this? Which methods I have to use?
Code:
#Override
public void onClick(View v) {
Button button = (Button)findViewById(v.getId());
if(button.getText().equals(quizWorld.getCurrentQuestion().getRightAnswer())){
//right
}else{
//wrong
}
}
#Override
public boolean onTouchEvent(MotionEvent event){
switch(event.getAction()){
case MotionEvent.ACTION_UP:
//Game Over?
if(quizWorld.swapQuestion()==false){
viewFlipper.setDisplayedChild(1);
}
break;
}
return true;
}
Just for Explanation: In the onClick the Programm compares the text on the button with the saved answer and initiate the blinking (not coded yet). And on the onTouchEvent it changes the question.
And now: onTouchEvent should be only "activated" when the Programm has just finished the checking process. After that the Programm shouldn't react on OnEventTouch.
If you don't need to handle the touch event, just return false onTouchEvent and let the touch pass through.
#Override
public boolean onTouchEvent(MotionEvent event){
if (I don't need this touch)
{
return false;
}
switch(event.getAction()){
case MotionEvent.ACTION_UP:
//Game Over?
if(quizWorld.swapQuestion()==false){
viewFlipper.setDisplayedChild(1);
}
break;
}
return true;
}

ProgressBar duration and hasEnded methods

I'm working with a progress bar and an animation.
progressBar = (ProgressBar)findViewById(R.id.progressBar1);
Animation animation = new RotateAnimation(30, 180);
progressBar.setAnimation(animation);
further down in my method I
set the duration for this animation
progressBar.getAnimation().setDuration(3000);
so for 3 seconds, the animation will run.
if I want to hide the ProgressBar after the animation ends I have to do the following
if(progressBar.getAnimation().getDuration() == 3000){
progressBar.setVisibility(ProgressBar.GONE);
}
What I tried doing was
if(progressBar.getAnimation.hasEnded()){
progressBar.setVisibility(ProgressBar.GONE);
}
this does not work
Could somebody explain why the hasEnded method doesn't seem to return true where I think it should.
you can call set an Animation Listener on the animation object. The Listener calls tree methods, one of which is onAnimationEnd().
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation anim) {
};
#Override
public void onAnimationRepeat(Animation anim) {
};
#Override
public void onAnimationEnd(Animation anim) {
// here your logic
};
});
I think problem is in below line...
if(progressBar.getAnimation.hasEnded()) {
it should be...
if(progressBar.getAnimation().hasEnded()) {
Edit::
That's because you're starting the animation and immediately checking if the animation has ended which is doubtful unless it simply doesn't do anything. What you want to do is implement an AnimationListener callback and set it to that animation.

Android: How to determinate how long i touch the screen?

for my app i want to determinate how long i touch the screen. I want use the function onTouchEvent, i don't want use some images or some buttons to do it. Some idea? Otherwise, if there isn't a way, with a buttons but not with a image.
You can catch the movement using MotionEvent.ACTION_DOWN to start the timer and stop it when you catch MotionEvent.ACTION_UP.
#Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
//start timer
} else if(event.getAction()==MotionEvent.ACTION_UP){
//end timer
}
return true;
}
ZouZou's answer is good, but here's another solution, without struggling with timers. If you have a MotionEvent event then you can use getDownTime() and getEventTime() methods. The difference between them will be the duration of the touch.
#Override
public boolean onTouchEvent(MotionEvent event) {
final long time = (event.getEventTime() - event.getDownTime();
return true;
}

How can I make a piece of code loop while a Button is pressed and stop it once the Button is no longer Pressed?

I am using the android developer tools in Eclipse, programming in Java, and I need to make an object move across the screen as long as a button is pressed. I've been doing research for hours, and I cannot find any methods to accomplish this. I've tried running threads, which often crash or seemingly don't execute. I've also tried an onClickListener which reads the button state and uses it to determine whether or not the button is still pressed. I'm currently using a while loop, but this just freezes the program. I believe that this is the best method, and I've tried to use Thread.sleep in order to limit the number of iterations per second, as I believe that this is the reason it is freezing. Am I on the right track or am I way off in left field? Here is a snippet of code:
rightButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
while(arg0.isPressed())
{
mover.updateCoordinates(1, 0);
}
}
});
Would you try this another method?
Firstly declare your button as class variable, declare a Handler and a Runnable:
private Button rightButton; // You will assign this in onCreate() method
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
#Override
public void run() {
if(rightButton.isPressed())
{
// If press state is pressed, move your item and recall the runnable in 100 milliseconds.
mover.updateCoordinates(1, 0);
mHandler.postDelayed(mRunnable, 100);
}
}
};
Then your button's onClickListener will looks like this:
rightButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0)
{
// Instead of performing a loop here, just call a runnable, do simple press state checking there.
mHandler.postDelayed(mRunnable, 100);
}
});
Create a loop that updates the views, waits, and calls itself after it finishes waiting. Within the loop, have an animation if statement with a boolean field that move on true and does not move on false. Have the onClick method toggle the boolean field's value.
Try:
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
// Start your animation here
} else if (e.getAction() == MotionEvent.ACTION_UP) {
// Stop your animation here
}
return false;
}
});
References:
MotionEvent
OnTouchListener
Alternatively, override onKeyUp and onKeyDown of the View class and check for the KEYCODE_ENTER KeyEvent

Categories