1

I made an app that gets the touch location and now I want to make it tap this place..

Here is the code:

public class MainActivity extends Activity {
    @Override
    //OnCreate
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //GetTouch location
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            Toast.makeText(this, "Location Located",Toast.LENGTH_SHORT).show();
            int eventaction = event.getAction();
            Point point = new Point();
            point.x = (int) event.getX();
            point.y = (int) event.getY();
            switch (eventaction) {
                case MotionEvent.ACTION_DOWN:
                    break;     
            }
            return false;
        }
    }
}

I only want it to programmatically touch this place..

How can I do it ?

Vito Gentile
  • 13,336
  • 9
  • 61
  • 96
DavidBalas
  • 333
  • 7
  • 21
  • You Can perform touch event on i.e clicked event for widgets ( ui elements ) they will have there own location information.. So you will get that as x and y. If you want perform own touch event on the screen u need x and y location information first. How you gonna handle that. – Sush Jun 28 '14 at 11:42

4 Answers4

4

You'll need to use some view to register your touch listener. I chose the default RelativeLayout. I've set the delay of the callback click to be 2 seconds.

Note that you need to save the last simulated touch (simulationEvent) and check inside of your onTouchListener if it's not that event. Without this check you would get repeated clicks in the same place over and over, as the simulation would call itself back.

If you want only the simulated touch to be registered, change the onTouchListener's last return to true.

package com.example.virtualclicker;

import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

/**
 * Created by Simon on 2014 Jul 09.
 */
public class MainActivity extends ActionBarActivity {

    private MotionEvent simulationEvent;

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

        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.myLayout);

        relativeLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event == simulationEvent)
                    return false;
                int action = event.getAction();
                int x = (int)event.getX();
                int y = (int)event.getY();
                Log.e("onTouchListener", "User touch at X:" + x + " Y:" + y);
                long length = 0;
                if (action == MotionEvent.ACTION_DOWN) {
                    click(v, x, y);
                }
                return false;
            }
        });
    }

    public void click(final View view, final int x, final int y) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // SIMULATION BEGINS HERE AFTER 2000 Millis ///
                long touchTime = SystemClock.uptimeMillis();
                simulationEvent = MotionEvent.obtain(touchTime, touchTime,
                        MotionEvent.ACTION_DOWN, x, y, 0);
                view.dispatchTouchEvent(simulationEvent);

                simulationEvent = MotionEvent.obtain(touchTime, touchTime,
                        MotionEvent.ACTION_UP, x, y, 0);
                view.dispatchTouchEvent(simulationEvent);
                Log.e("simulatedTouch","simulated touch executed at X:"+x+" Y:"+y);
            }
        }, 2000);
    }

}

Hope this works for ya.

EDIT:

layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"/>
Simas
  • 43,548
  • 10
  • 88
  • 116
  • Well nope.. : crashing, line "relativeLayout.setontouchlistener....)" anyway, I don't need it anymore, ty anyway. – DavidBalas Jul 12 '14 at 19:10
  • Pretty sure you didn't set an id to your relative layout inside of `activity_main.xml`. Updated the answer with the contents of that file. – Simas Jul 12 '14 at 19:46
  • @DavidBalas Please try with my given layout. – Simas Jul 13 '14 at 08:21
0
int eventaction = event.getAction();
             Point point = new Point();
                point.x = (int) event.getX();
                point.y = (int) event.getY();


                switch (eventaction) {

                case MotionEvent.ACTION_DOWN:




                        break;
                }





        }

poiny.x,point.y is the touched place on the screen

0

Simulating a touch event :

IWindowManager mWm = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
//Here you'll need to create a touch event using obtain method of TouchEvent. 
//Check this : http://stackoverflow.com/questions/5867059/android-how-to-create-a-motionevent
//Once you have the motion event,m, just use it to simulate click. 
mWm.injectPointerEvent(m, false);
Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
  • Wow ty very much :) it gives me two errors with the ServiceManager and the IWindowManager, its says to open classes on these names.. why? – DavidBalas Jun 28 '14 at 11:39
  • actually, no.. it doesn't asked me to add .. which imports to add? – DavidBalas Jun 28 '14 at 11:42
  • Ah, you'll need to create a jar with fake implementation. Check out this link, it tells you how to do that towards the end of the article : http://piece0ftime.blogspot.sg/2012/10/android-public-sdk-development-cycle.html – Shivam Verma Jun 28 '14 at 11:45
  • Well.. I don't know what you mean so I guess I'll choose an other idea for an application :P ty anyway .. – DavidBalas Jun 28 '14 at 11:46
  • Because you gave the article after I added the comment :P – DavidBalas Jun 28 '14 at 11:48
0

Your question is little bit confusing, but if you want to listen to touch event irrespective of any view i.e. on entire screen of an Activity, then you can over write

public boolean dispatchTouchEvent(MotionEvevt me){

   // Dispatch this touch to a View of your choice as follows 
   mView.dispatchTouchEvent(me);

}

on your SubActivity class. It will give you absolute x,y position of touch on device's screen.

CR Sardar
  • 921
  • 2
  • 17
  • 32