• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Motion Event only inside the button view, and unable to be clicked more than once

Dasheno

Lurker
I have two questions right now:

1) Using the code below this works great to show where the touch event is happening from the user on screen, however whenever the "Auto" button is clicked it only sends a motion event inside of the button view at the dimensions (X: 300, Y: 300) which is technically correct! However, I want it to send the motion event outside of the button's view dimensions.

2) Whenever the "Auto" button is clicked it only appears to send the motion events what looks like twice in quick succession and then the button is unable to be clicked again unless the app is closed and re-started.

Code:
class MainActivity : AppCompatActivity() {

   override fun onCreate(savedInstanceState: Bundle?) { //On creation of the activity this code runs
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main);

       editText.isFocusable = false
       editText.isFocusableInTouchMode = false

       editText2.isFocusable = false
       editText2.isFocusableInTouchMode = false
   }


   private var isTouch = false

   @SuppressLint("SetTextI18n")
   override fun onTouchEvent(event: MotionEvent): Boolean {

       val X = event.x.toInt()
       val Y = event.y.toInt()

       val eventaction = event.action


       editText.setText("X: $X")
       editText2.setText("Y: $Y")
       return true
   }


fun button (view: View) {
   Toast.makeText(this, "AutoClicked", Toast.LENGTH_SHORT).show()
}


fun auto (view: View) {

   // Obtain MotionEvent object
   val downTime = SystemClock.uptimeMillis()
   val eventTime = SystemClock.uptimeMillis() + 100
   val x = 300.0f
   val y = 300.0f
// List of meta states found here:     developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
   val metaState = 0
   val motionEvent = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, metaState)
   val motionEvent2 = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, metaState)


// Dispatch touch event to view
   view.dispatchTouchEvent(motionEvent)
   view.dispatchTouchEvent(motionEvent2)
}

}

I have posted code above and a picture with a quick summary of what happens in the picture linked below.

(1.jpg)

I have seen ideas on how to make a layout that this motion event references, but it doesn't seem to make sense to me for this implementation of code I currently have.
 

Attachments

  • 1.jpg
    1.jpg
    28.5 KB · Views: 320
Back
Top Bottom