I have an activity where user can see the "about" about the simple app I'm trying to develope. I'm using "Android About Page" to show it but plus I'd like to detect the number of taps on the screen so that a Toast message can appear when the right number of taps are detected...sort of easter egg :) The problem I'm facing is that touch events are not triggered... this is the code I'm using:
public class About extends AppCompatActivity {
@Override
public void onTouchEvent(MotionEvent event) {
Log.i("LOG_RESPONSE", "Screen touched!");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//// other code
}
}
This is my XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".About">
<View
android:id="@+id/myView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
No errors, it just does nothing.
EDIT as suggested using onTouch():
public class About extends AppCompatActivity {
View test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
test = (View)findViewById(R.id.myView);
test.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("LOG_RESPONSE", "Screen touched!");
return false;
}
});
//// other code
}
}
This way the app crashes:
Caused by: java.lang.NullPointerException
EDIT 2 - It does not crash anymore but no touch is recognized:
public class About extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
View test = findViewById(R.id.layer);
Log.i("LOG_RESPONSE", test.toString());
test.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("LOG_RESPONSE", "Screen touched!");
return false;
}
});
}