1. Are you ready for the Galaxy S20? Here is everything we know so far!

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

Discussion in 'Android Development' started by Dasheno, Sep 23, 2019.

  1. Dasheno

    Dasheno Lurker
    Thread Starter

    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 (Text):
    1.  
    2. class MainActivity : AppCompatActivity() {
    3.  
    4.    override fun onCreate(savedInstanceState: Bundle?) { //On creation of the activity this code runs
    5.        super.onCreate(savedInstanceState)
    6.        setContentView(R.layout.activity_main);
    7.  
    8.        editText.isFocusable = false
    9.        editText.isFocusableInTouchMode = false
    10.  
    11.        editText2.isFocusable = false
    12.        editText2.isFocusableInTouchMode = false
    13.    }
    14.  
    15.  
    16.    private var isTouch = false
    17.  
    18.    @SuppressLint("SetTextI18n")
    19.    override fun onTouchEvent(event: MotionEvent): Boolean {
    20.  
    21.        val X = event.x.toInt()
    22.        val Y = event.y.toInt()
    23.  
    24.        val eventaction = event.action
    25.  
    26.  
    27.        editText.setText("X: $X")
    28.        editText2.setText("Y: $Y")
    29.        return true
    30.    }
    31.  
    32.  
    33. fun button (view: View) {
    34.    Toast.makeText(this, "AutoClicked", Toast.LENGTH_SHORT).show()
    35. }
    36.  
    37.  
    38. fun auto (view: View) {
    39.  
    40.    // Obtain MotionEvent object
    41.    val downTime = SystemClock.uptimeMillis()
    42.    val eventTime = SystemClock.uptimeMillis() + 100
    43.    val x = 300.0f
    44.    val y = 300.0f
    45. // List of meta states found here:     developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
    46.    val metaState = 0
    47.    val motionEvent = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, metaState)
    48.    val motionEvent2 = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, metaState)
    49.  
    50.  
    51. // Dispatch touch event to view
    52.    view.dispatchTouchEvent(motionEvent)
    53.    view.dispatchTouchEvent(motionEvent2)
    54. }
    55.  
    56. }
    57.  
    58.  
    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.
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      28.5 KB
      Views:
      17

    Advertisement

  2. ocnbrze

    ocnbrze DON'T PANIC!!!!!!!!!

    this sounds like development question......yes?
     
  3. Dasheno

    Dasheno Lurker
    Thread Starter

    Yes it is.
     
Loading...

Share This Page

Loading...