Slight lag using button setOnTouchListener - android

I am working on a project that requires the exact moment a button was touched to be recorded. For this, I am using setOnTouchListener.
I have simplified the listener down to a simple print statement. Within Logcat, there is a very slight delay between when the button is touched, and when "TOUCHED" gets printed.
class MainActivity : AppCompatActivity() {
private lateinit var mp: MediaPlayer
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tapButton.setOnTouchListener { v, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
println("ACTION_DOWN")
mp = MediaPlayer.create (this, R.raw.blip)
mp.start()
Toast.makeText(this#MainActivity, "ACTION_DOWN", Toast.LENGTH_SHORT).show()
background.setBackgroundColor(Color.parseColor("#ff0000"))
v.isPressed = true
} else if (event.action == MotionEvent.ACTION_UP) {
println("ACTION_UP")
background.setBackgroundColor(Color.parseColor("#ffffff"))
v.isPressed = false
}
true
}
}
}
XML (as basic as it can get!)
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:text="Button"
android:layout_width="188dp"
android:layout_height="288dp"
android:id="#+id/tapButton" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
How can I prevent this lag, so that I can get the precise moment the button was touched?
If it requires not using a button, and using a tappable view - that is fine too. It's imperative that this lag is removed. Additionally - if this lag is something that can't be addressed - perhaps I can offset the UI if I have the exact length (in milliseconds) that this lag occurs for. Open to ideas.
EDIT:
I believe I found the solution here; just don't know how to fix it.
How can I make a Button more responsive?
This thread states there's something called 'getTapTimeout' that intentionally puts a delay on a touch event to determine if it will be a tap or scroll. THE CULPRIT?!
How do I set this to 0??

Don't use logcat to tell. Logcat sends data to a separate process. It can be used for basic timings, but not for ms exact timings.
Every MotionEvent has a timestamp in it. You can get it with motionEvent.getEventTime(). Use that for your timing data.

You could create your own CustomView that extends View and overrides dispatchTouchEvent. You'll need to do something along the following lines,
interface CustomListener {
fun customOnTouch()
}
class CustomTouchView : View {
constructor(context: Context?) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)
private var customOnTouchListener:CustomListener? = null
public fun setCustomOnTouchListener(listener: CustomListener) {
this.customOnTouchListener = listener
}
override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
customOnTouchListener?.customOnTouch()
return super.dispatchTouchEvent(event)
}
}

Related

Using OnStateChanged listener simultaneously with OnTouchListener

I am implementing a rotary knob based on this library.
This View comes with a onStateChanged listener that tells me the current position of the knob.
val knob = findViewById<View>(R.id.knob) as Knob
knob.setOnStateChanged(object: Knob.OnStateChanged{
override fun onState(state: Int) {
// do stuff
}
})
In addition to that, I want to know when the user is no longer pressing/holding the knob (similar to a button release). I tried to achieve this with a onTouch listener.
knob.setOnTouchListener(object: View.OnTouchListener{
override fun onTouch(view: View?, event: MotionEvent?): Boolean {
// do stuff
}
})
Problem: When I add a second onTouch listener, it is no longer possible to hold and rotate the view for some reason. I do not know whether this is a problem of this particular library or Android in general.
Any suggestions on how to implement the wanted features?
A view can only have a single OnTouchListener. Unfortunately, the author of that library implemented some of its functionality using an OnTouchListener, which prevents users from using an OnTouchListener.
An alternative for you would be to subclass Knob and override onMotionEvent, like this:
class MyKnob: Knob {
constructor(context: Context): super(context)
constructor(context: Context, attrs: AttributeSet): super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int): super(context, attrs, defStyleAttr)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int): super(context, attrs, defStyleAttr, defStyleRes)
override fun onTouchEvent(event: MotionEvent): Boolean {
val result = super.onTouchEvent(event)
if (event.action == MotionEvent.ACTION_UP) {
// User released knob
}
return result
}
}
Then you'd need to use this class in your layout instead of the original Knob class.

How to make a custom view inherit it's parent's style in Android

With a Switch I can do this:
< Switch
android:id="#+id/normal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:gravity="end|center_vertical"
android:switchPadding="16dp"
android:thumbTextPadding="16dp"
android:text="Hello World" />
Pay attention to this lines:
android:switchPadding="16dp"
android:thumbTextPadding="16dp"
Now, I made a custom view that extends this Switch. I didn't made any special change:
import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.SwitchCompat
class BetterSwitchCompat : SwitchCompat {
private var listener: OnCheckedChangeListener? = null
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int)
: super(context, attrs, defStyleAttr)
override fun setOnCheckedChangeListener(listener: OnCheckedChangeListener?) {
if (listener != null) {
this.listener = listener
}
super.setOnCheckedChangeListener(listener)
}
fun setCheckedSilent(checked: Boolean) {
toggleListener(false)
isChecked = checked
toggleListener(true)
}
private fun toggleListener(value: Boolean) {
if (value) setOnCheckedChangeListener(listener)
else setOnCheckedChangeListener(null)
}
}
As you can see, there is nothing more than a business logic for check method.
Why I can't use the attributes I referred to before for the new class?
There are just a few posts about this, but none of them clarify what I am asking.
Android custom view inherit all styles and attributes from parent
How to make a custom view inherit it's parent's style
Switch isn't SwitchCompat. SwitchCompat comes from AndroidX so it uses application-defined attributes.
Try prefixing the attributes in question with app: instead of android:, such as app:switchPadding.

Intercept touch event in RecyclerView while ignoring EditText inside

I have recyclerView with and item as seen below:
Now I want to be able to click anywhere on the item and editText should come into focus.
I can do that by setting onTouchListener on my view like this:
row_item.setOnTouchListener{ _, _ ->
editText.requestFocus()
view.background = Color.GREEN.toDrawable()
true
}
I also want to run some additional code whenever the item is clicked. Here I'm putting background color change for the sake of the example.
The problem is that whenever I click editText itself it is getting focused, but row_item touchListener is ignored, and the background doesn't change its color.
From my research, I've found that I should somehow intercept touch event. I thought I can do that by returning true in row_item.setOnTouchListener, but it doesn't work as you can see.
How can I intercept such touch event?
You should create a custom container class and then override onInterceptTouchEvent method and do your stuff there, then using this custom container class as the root of the item. Look at the following code:
class CustomFrameLayout : FrameLayout {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)
private var mOnInterceptTouchEventListener: OnTouchListener? = null
fun setOnInterceptTouchEventListener(onInterceptTouchEventListener: OnTouchListener) {
this.mOnInterceptTouchEventListener = onInterceptTouchEventListener
}
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
if(mOnInterceptTouchEventListener != null && mOnInterceptTouchEventListener?.onTouch(this, ev) == true)
return true;
return super.onInterceptTouchEvent(ev)
}
}
And then adding this listener to your row_item
row_item.setOnInterceptTouchEventListener(object: View.OnTouchListener {
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
editText.requestFocus()
v?.background = Color.GREEN.toDrawable()
return false
}
})
it may not be the best answer but it works.

android customViews in kotlin - is it ok to initialize in the init block?

I am so confused why no one uses the init block in a android custom view to initialize and inflate the view. lets take an example of how i do this:
class MyCompoundView : ConstraintLayout {
constructor(p0: Context) : super(p0)
constructor(p0: Context, p1: AttributeSet?) : super(p0, p1)
constructor(p0: Context, p1: AttributeSet?, p2: Int) : super(p0, p1, p2)
init {
inflate(context, R.layout.my_view_container, this)
//etc
}
}
is there anything wrong with this as opposed to what i see all over the internet:
class MyCompoundView : ConstraintLayout {
constructor(p0: Context) : super(p0){initialize()}
constructor(p0: Context, p1: AttributeSet?) : super(p0, p1){initialize()}
constructor(p0: Context, p1: AttributeSet?, p2: Int) : super(p0, p1, p2){initialize()}
private fun initialize() {
inflate(context, R.layout.ride_hail_otp_container, this)
}
}
ps. i do not favor jvmOverload in customViews so no need to mention that. just want to know about init block vs calling it in each constructor. i see no one doing it online and i wonder why ?
Yes, It's totally fine, I my self used this approach many times and ain't facing any problems.
one of the examples I'm currently working with :
class MaterialSearchBar (context: Context, val attributeSet: AttributeSet) : Toolbar(context, attributeSet) {
init {
inflate(context, R.layout.material_search_toolbar, this)
updateUi()
requestFocus()
setUpListeners()
}
//...
}
There's nothing wrong with inflating the view in the init block (I do it in my custom views) just like there is nothing wrong with #JvmOVerloads.

How to 2-way bind a Seekbar in custom view / component?

I'm trying to use 2-way databinding on a custom view that contains a SeekBar. The layout is rather simple, but I need to reuse it across the project, hence wrapping it into a custom view/component
<androidx.constraintlayout.widget.ConstraintLayout ... />
<TextView .../>
<TextView .../>
<SeekBar
android:id="#+id/ds_seekbar"
android:layout....
android:max="9"
android:min="0"
android:progress="0"
</androidx.constraintlayout.widget.ConstraintLayout>
The backing code looks like so (reduced)
CustomView #JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr), View.OnClickListener {
init {
LayoutInflater.from(context).inflate(R.layout.custom_view, this, true)
ds_description.setOnClickListener(this)
}
override fun onClick(view: View) {
//onClick implementation
}
}
I can do the binding in the ViewModel for the layout where this custom view is going to be used, with a BindingAdapter there with custom attribute (ex. app:seekbar), but the custom view would be used multiple times and I'd prefer to have the a lot of the logic that is required into the view and have a "lighter" handling in the ViewModel.
I read Android 2-Way DataBinding With Custom View and Custom Attr and a bunch of other articles which seem to be a little different but oon the same topic, however no matter how I wrote the getter and setters I always run into the kapt exception that it cannot find the getter/setter.
Either I'm not annotating properly the methods or they have wrong signatures.
Ideally I want to have something like:
CustomView #JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr), View.OnClickListener, SeekBar.OnProgressChangedListener {
... ds_seekbar.setOnProgressChangedListener(this)
And then in the main layout have the app:progress (or even better if someone can show how it's done android:progress) on the custom view for binding when passing my object.
Okay after more and more headscratching, here's what I've come with, that seems to work. Whether this is the proper way or how performant/reliable is - I'm not sure
#InverseBindingMethods(InverseBindingMethod(type = CustomView::class, attribute = "progress", event = "progressAttrChanged"))
CustomView #JvmOverloads constructor(...
private var progress = 0
private var mInverseBindingListener: InverseBindingListener? = null
cv_seekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {
progress = i + 1
if (mInverseBindingListener != null) {
mInverseBindingListener!!.onChange()
cv_indicator.text = progress.toString()
}
}...
})
fun getProgress(): Int {
return progress
}
fun setProgress(p: Int) {
if (progress != p) {
progress = p
}
}
fun setProgressAttrChanged(inverseBindingListener: InverseBindingListener?) {
if (inverseBindingListener != null) {
mInverseBindingListener = inverseBindingListener
}
}
Then the XML is
<com.xxx.CustomView
android:id="#+id/xxx"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:progress="#={viewModel.dataobject.value}"
....
/>

Categories

Resources