0

I am working on an android app using Android Studio.

Currently I am trying to figure out how to get a swipe detection to work, even if the cursor starts inside of an edittext.

I have followed the first answer here to get OnSwipeTouchListener to work. (It works outside of the main view - toasts show up saying "You have swiped left")

I call it using

var SV: ScrollView = findViewById(R.id.SV) as ScrollView
SV.setOnTouchListener(object:OnSwipeTouchListener(this@MainActivity) {
            override fun onSwipeTop() {
                Toast.makeText(this@MainActivity, "Top", Toast.LENGTH_SHORT).show()
                println("TOP")
            }
}

Where SV is my parent scroll view.

However, then I create an edittext programmatically and add it:

val myEditText = EditText(this)
linear.addView(myEditText)

Where linear is a child linearlayout inside the main scrollview

I cannot detect a swipe from inside the edittext to outside of it. It DOES work if you start on the outside, then swipe inside.

How can I detect swipes inside of edittexts, and swipes that start inside of an edittext, but end outside of it?

Can anyone help? Any help is appreciated!

tthudium
  • 21
  • 4

1 Answers1

0

Well. Apparently you have an answer in your question. You can use

val myEditText = EditText(this)
linear.addView(myEditText)
myEditText.setOnTouchListener(object:OnSwipeTouchListener(this@MainActivity) {
            override fun onSwipeTop() {
                Toast.makeText(this@MainActivity, "Top", Toast.LENGTH_SHORT).show()
                println("TOP")
            }
}

EditText has its own touch listener so yours will override the default one and some of EditText features may misbehave.

I am not sure why do you need to catch swipe event on EditText though. If you want swiping behaviour on the whole content of the screen or on its part I would recommend you to use ViewPager, ViewPager2 or RecyclerView.

I don't know the requirements though, so maybe catching swipe on EditText is crucial for your app.

Hope it helps somehow.

Pavlo Ostasha
  • 14,527
  • 11
  • 35
  • This helps a lot, thanks! I will look into ViewPager and RecyclerView. I have an activity full of edittexts (workout app - sets and reps) and swiping from left to right changes the exercise you're editing. – tthudium Sep 25 '19 at 22:53