Search code examples
androidlistviewandroid-listviewtouch-event

Call onTouchEvent() before listView


In my activity I have a ListView that is populated by an ArrayList, and it implements the Android MediaPlayerControl.

MusicController is using the default behaviour so it disappears after 3000ms, and I would like to be able to show it any time the user touches the screen once. At the moment when the user touches the screen, the item in the ListView is selected. I would like to simply call controller.show() instead. Unfortunately, the code below does not work. It simply activates whatever item is selected in the listview.

Do I need to add some kind of overlay to my activity? I just want some sort of "super touch listener" that listens for an on touch event anywhere on the screen, responds with controller.show().

I've added this to my MainActivity:

import android.widget.MediaController.MediaPlayerControl;
import android.view.MotionEvent;


    private MusicController controller;

 @Override
public boolean onTouchEvent(MotionEvent event) {
    //the MediaController will hide after 3 seconds - tap the screen to make it appear again
    controller.show();
    return false;
}

EDIT: This is after I attempted what Marcus suggested

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:id="@+id/wholeScreenLayout">

<ListView
    android:id="@+id/media_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</ListView>

mainActivity.java snippet. Added to onCreate() method

//when user touches screen show controls

    RelativeLayout wholeScreen = (RelativeLayout) findViewById(R.id.wholeScreenLayout);
    wholeScreen.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            controller.show();
            return false;
        }
    });

Solution

  • You could add a OnTouchListener for your application layout. In your layout file, add this to your root layout element:

    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:id="@+id/wholeScreenLayout"
    

    Then you add the listener

    //Assuming it's a RelativeLayout element
    RelativeLayout yourRelativeLayout = (RelativeLayout) findViewById(R.id.wholeScreenLayout);
    yourRelativeLayout.setOnTouchListener(new View.OnTouchListener() {  
        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
    
            //Do your work here
    
            return true;//always return true to consume event
        }
    });
    

    To address "ignores subsequent events until 3000ms later.", you would need to keep track of the time somehow. You can get the current time with System.currentTimeMillis();, save that value then check it in the onTouch method.