Search code examples
androidtouch-eventontouchlistenerpreferenceactivity

onTouchEvent not called for PreferenceActivity - Why?


I have this very simple code implemented within my app:

public class EditPreferences extends PreferenceActivity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.v(TAG, "onCreate");   
  }


  @Override
  public boolean onTouchEvent(MotionEvent me) {
    Log.v(TAG, "onTouchEvent");   
    return false;
  } 

}

When the preferences screen/activity shows up, I expect to see the "onTouchEvent" log messages when I touch anything on that screen.

But I don't get any messages. Which tells me that onTouchEvent isn't even called.

(I can see the "onCreate" message, though. Of course.)

Why isn't PreferenceActivity's onTouchEvent() called?

Is it possible that it is being intercepted earlier by some other component in the app?

Or am I missing something in the implementation steps?


Solution

  • Thats because, you are actually touching a ListView which is by default implemented by PreferenceActivity, you need to call onTouch of your ListView instead, check following code :

    getListView().setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Toast.makeText(getApplicationContext(), "touched", Toast.LENGTH_SHORT).show();
             return false;
        }
    });
    

    in your onCreate method.

    GOOD LUCK.