How do I detect touch input on the Android

75,508

Solution 1

Try code below to detect touch events.

mView.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //show dialog here
        return false;
    }
});

To show dialog use Activity method showDialog(int). You have to implement onCreateDialog(). See documentation for details.

Solution 2

Here is a simple example on how to detect a simple on touch event, get coords and show a toast. The event in this exmple are Action Down, Move and Action up.

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

public class MainActivity extends Activity {

    private boolean isTouch = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int X = (int) event.getX();
        int Y = (int) event.getY();
        int eventaction = event.getAction();

        switch (eventaction) {
            case MotionEvent.ACTION_DOWN:
                Toast.makeText(this, "ACTION_DOWN AT COORDS "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();
                isTouch = true;
                break;

            case MotionEvent.ACTION_MOVE:
                Toast.makeText(this, "MOVE "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();
                break;

            case MotionEvent.ACTION_UP:
                Toast.makeText(this, "ACTION_UP "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }
}

Solution 3

I did it like this:

public class ActivityWhatever extends Activity implements OnTouchListener
{

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourlayout);

        //the whole screen becomes sensitive to touch
        mLinearLayoutMain = (LinearLayout) findViewById(R.id.layout_main);
        mLinearLayoutMain.setOnTouchListener(this);
    }

    public boolean onTouch(View v, MotionEvent event)
    {
        // TODO put code in here

        return false;//false indicates the event is not consumed
    }
}

in the xml of your view, specify:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layout_main">

    <!-- other widgets go here-->

</LinearLayout>

Solution 4

I have tried alot and finally found a solution to detect touch in a screen after 2 days.

Kotlin:

If you are having bottom navigation bar and you want to hide on touch...try this!

Activity.dispatchTouchEvent(MotionEvent) - This allows your Activity to intercept all touch events before they are dispatched to the window.

  override fun dispatchTouchEvent(event: MotionEvent): Boolean {

   if (event.getAction() === MotionEvent.ACTION_DOWN) {
       if (event.getAction() === MotionEvent.ACTION_DOWN) {




       }
   } else if (event.getAction() === MotionEvent.ACTION_MOVE) {
       tabLayout.visibility = View.GONE
       tv_chat.visibility = View.GONE



   } else if (event.getAction() === MotionEvent.ACTION_UP) {

       tabLayout.visibility = View.VISIBLE
       tv_chat.visibility = View.VISIBLE

   }
    return super.dispatchTouchEvent(event)
}

Solution 5

//on finger touch view visible. On finger up gone

    hintView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
          if(event.getAction()==MotionEvent.ACTION_MOVE){
                hintText.setVisibility(View.VISIBLE);
            }else if(event.getAction()==MotionEvent.ACTION_UP){
              hintText.setVisibility(View.GONE);

            }

            return true;
        }
    });
Share:
75,508
RyoxSinfar
Author by

RyoxSinfar

Updated on November 13, 2020

Comments

  • RyoxSinfar
    RyoxSinfar over 3 years

    Right now all I am trying to do is detect when the screen is pressed and then display a log message to confirm it happened. My code so far is modified off of the CameraPreview sample code (it will eventually take a picture) so the bulk of the code is in a class that extends SurfaceView. API for the example code from the SDK is 7.

  • Andrey Busik
    Andrey Busik over 5 years
    How to hide Snackbar when user touches anywhere except it's bounds gist.github.com/and291/ce5704c4163f8dcd9d06b1ab6a9850cf
  • Md. Masum Billah
    Md. Masum Billah about 4 years
    What is mView here? I am new to this line. Please share your answare.
  • Admin
    Admin over 3 years
    I want to call this event when i touched only one view on top of the screen whose width and height is 200. How to achieve that ? currently on-touch is listening throughout screen
  • ggorlen
    ggorlen over 2 years
    @Md.MasumBillah it's a View object. See this answer for how to create one.