3

I am developing Android 2.1 API 7 app.

In my Activity, I add the onTouchEvent() callback to handle screen touch event:

public class MyActivity extends Activity{

   ...

   @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            //Did something here
            Log.v("TOUCH SCREEN", "test");
        }

        return super.onTouchEvent(event);
    }
}

I test this on a real device, but when I touch the screen, it seems the callback is not invoked, because I did not see the Log.v(...) information. Why??

Leem.fin
  • 40,781
  • 83
  • 202
  • 354

2 Answers2

1

My questions: is there any onTouchListener? Have you registered the listener? Why don't you return just true, even though you handle the event by yourself?

This will work for sure:

public class TouchTestActivity extends Activity implements OnTouchListener {

    TextView textView;
    String text;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        textView = new TextView(this);
        textView.setOnTouchListener(this);
        textView.setClickable(true);
        textView.setEnabled(true);
        textView.setFocusable(true);
        textView.setFocusableInTouchMode(true);
        setContentView(textView);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            text = "GETTING TOUCHED";
        }
        textView.setText(text);
        return true;
    }
}
Chris
  • 4,403
  • 4
  • 42
  • 54
  • Hi, I would like to dismiss a alert dialog when user touch the screen, but the ".setOnTouchListener(this);" can not be used on dialog instance. – Leem.fin Mar 01 '12 at 14:15
  • Normally dialogs get dismissed with the back key. But it is possible to add a ClickListener to dialogs. Read [this](http://developer.android.com/guide/topics/ui/dialogs.html#AddingButtons) and next use [this](http://developer.android.com/reference/android/content/DialogInterface.OnClickListener.html). – Chris Mar 01 '12 at 14:58
  • No, @ Chris, you did not get what I mean, I want the dialog to be dismiss when user touch the screen which means even user touch the outside dialog area, the dialog should be dismiss. (I am on Android 2.1 platform) – Leem.fin Mar 01 '12 at 15:51
  • Look [here](http://stackoverflow.com/questions/5801634/android-close-dialog-window-on-touch) for the answer... – Chris Mar 01 '12 at 16:20
0

Here is some code, I think it will do what you want.

I used a toast widget so you can easily see it works.

package com.aendroid.tuetsh;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

public class Tuetsh extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.v("TOUCH SCREEN", "test");
        Toast.makeText(getBaseContext(), "How dare you touch me", Toast.LENGTH_SHORT).show();
        return super.dispatchTouchEvent(ev);

        } 
}
zeitue
  • 1,674
  • 2
  • 20
  • 45