I simply want to detect how much time has passed everytime I tap the screen. I can't seem to recognize the screen taps though. I have an onTouchEvent method that should call my checkTime method. The toast isn't displaying anything and the System.out.println isn't printing.
package com.example.biastester;
import java.util.Calendar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;
public class Test extends ActionBarActivity {
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
public boolean onTouchEvent(MotionEvent event){
checkTime();
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void checkTime(){
int currSeconds = c.get(Calendar.SECOND);
Toast.makeText(this, "tapped", 2);
System.out.println("the time elapsed since the start of this test is" + (currSeconds-seconds) );
if(currSeconds-seconds > 120){
Intent intent = new Intent(this, Results.class);
startActivity(intent);
}
}
}
I have never used android before to I have no idea what I'm doing wrong. Thank you in advance for your help.
You need to capture click event
This code handles clicks in increate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button b = new Button(this);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkTime();
}
});
setContentView(new Button(this));
}