Last active
October 28, 2021 16:02
-
-
Save CreatorB/062b9897e224942f35b4ba7b53182566 to your computer and use it in GitHub Desktop.
Android Perform Touch on View - https://situbondoprogrammer.blogspot.com/2021/10/android-perform-click-perform-touch.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private lateinit var binding: ActivityMainBinding | |
override fun onCreate(savedInstanceState: Bundle) { | |
super.onCreate(savedInstanceState) | |
binding = ActivityMainBinding.inflate(layoutInflater) | |
val view = binding.root | |
setContentView(view) | |
b.apply { | |
btn1.setOnClickListener(listenerVoice) | |
tv1.setOnClickListener { requestAPI() } | |
if(!permission){ | |
tv1.isEnabled = false | |
} | |
} | |
} | |
fun performTouch(v: View) { | |
val TAG = "onTouch" | |
var originalDownTime: Long = SystemClock.uptimeMillis() | |
var eventTime: Long = SystemClock.uptimeMillis() + 100 | |
var x = 0.0f | |
var y = 0.0f | |
var metaState = 0 | |
var motionEvent = MotionEvent.obtain( | |
originalDownTime, | |
eventTime, | |
MotionEvent.ACTION_DOWN, | |
x, | |
y, | |
metaState | |
) | |
var returnVal = v.dispatchTouchEvent(motionEvent) | |
Log.d(TAG, "rteurnVal: " + returnVal) | |
//Create amd send the ACTION_UP MotionEvent | |
eventTime = SystemClock.uptimeMillis() + 100 | |
motionEvent = MotionEvent.obtain( | |
originalDownTime, | |
eventTime, | |
MotionEvent.ACTION_UP, | |
x, | |
y, | |
metaState | |
) | |
returnVal = v.dispatchTouchEvent(motionEvent) | |
Log.d(TAG, "rteurnVal: " + returnVal) | |
} | |
val listenerVoice = View.OnClickListener { view -> | |
//perform your global function here like custom sound each button or etc | |
//and you can assign value to most of views with tag, especially if it's layout | |
//example : SimpleAudioPlayer().playAudio(c, findViewById<LinearLayout>(view.id).tag.toString()) | |
//source for SimpleAudioPlayer() = https://gist.github.com/CreatorB/08da5488ebcd8d1d916c7b9f0d4926a3 | |
when (view.getId()) { | |
R.id.btn1 -> { | |
//this the trick, performTouch only trigger function requestAPI() if it view enabled | |
performTouch(b.tv1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment