1

For a hobby project, I assembled a GridView that contains multiple TextViews each containing a single letter. When the user swipes across the grid, a String should be constructed from the letters, similar to the behaviour of a swipe lock pattern or a swipe keyboard:

App Screenshot

I'm using a Board Adapter that overrides the getView and getItem functions to generate the TextViews dynamically, where board wraps a 2D-Array that's automatically filled with the letters:

    override fun getItem(position: Int): String {
        return board.getString(getCol(position),getRow(position))
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val textView : SquaredTextView

        textView.text = getItem(position)
        textView.gravity = Gravity.CENTER
        textView.textSize = 40f

        return textView
    }

I don't want to use hacky methods that require manually checking if the x- and y- coordinates of a touch event are within the bounding box of one of the TextViews like this. What kind of Listeners can I use in order to track the touch events of the swipe gestures across multiple TextViews, and build a String from their contents?

Some excerpts of what I tried so far:

I subclassed TextView to log onTouchEvent, onHoverEvent etc. to console, but only touch events are fired, and the touch gets cancelled as soon as the swipe gesture leaves the TextView it started on:

2020-10-29 19:06:22.059 5169-5169/com.example.myfirstapp D/Touch/L: MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=103.94824, y[0]=116.98364, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=3726741, downTime=3726741, deviceId=0, source=0x1002, displayId=0 }
2020-10-29 19:06:22.119 5169-5169/com.example.myfirstapp D/Touch/L: MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=94.778076, y[0]=137.42896, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=3, eventTime=3726841, downTime=3726741, deviceId=0, source=0x1002, displayId=0 }
2020-10-29 19:06:22.136 5169-5169/com.example.myfirstapp D/Touch/L: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=918.45154, y[0]=162.47711, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=2, eventTime=3726858, downTime=3726741, deviceId=0, source=0x1002, displayId=0 }

I also tried adding an OnItemClickListener to the GridView, but that never get's fired. When using an OnTouchListener on the GridView, I can't find a way to figure out the individual TextViews from the touch events.

I've also searched the internet and StackOverflow for my problem, but just found results like this or this, where none of them have an accepted answer. If this has been answered somewhere already, feel free to point me to it. Thanks in advance!

LukeLR
  • 1,129
  • 1
  • 14
  • 27
  • actually it seems `(x,y)` coordinate based detection is quite well used as also in here https://stackoverflow.com/questions/11104301/ontouchlistener-for-multiple-textviews-inside-tablerow to use `id` of views have you tried this: https://stackoverflow.com/questions/42923286/touch-listener-on-multiple-textviews other option could be to use canvas – ruben Oct 29 '20 at 20:15

0 Answers0