Simulating a button press with another button press - android

I currently want to have a button that when I press it another button flashes as if it has been pressed at the same time and activates the function of the other button. My current code is as such:
fun onTwo(view: View) {
button1.callOnClick()
button1.isPressed = true
}
However the issue I am facing is that it freezes button1 as if it is pressed until it is pressed again. Anyone have a solution for this?

You could add listener in one of the button to check for clicks and then use that event to trigger click event in another button, like this:
val button1 = findViewById(R.id.btn1ID) as Button
button1.setOnClickListener {
val button2 = findViewById(R.id.btn2ID) as Button
button2.performClick()
}
Replace R.id.btn1ID and R.id.btn2ID with their respective id(a).
Reference: performClick()
You could also create a utility function to use it without making redundant variables like this:
#Suppress("UNCHECKED_CAST")
fun Activity.findButtonById(#IdRes res : Int) : Button =
findViewById(res) as Button
// and then in your create method of activity:
findButtonById(R.id.btn1ID).setOnClickListener {
findButtonById(R.id.btn2ID).performClick()
}

Try performClick() method like below:
fun onTwo(view: View)
{
button1.performClick()
}

I ended up fixing this issue using coroutines in the end as such:
fun onTwo(view: View){
GlobalScope.async{
delay(100)
button1.isPressed = false
GlobalScope.cancel()
}
button1.setPressed(true)
button1.performClick()
}

Related

Is there a way to disable Edittext contextmenuitem "Clipboard"?

I want to disable EditText contextmenuitem "Clipboard" to open a popup view. Whenever I long press edittext in my app contextmenuitem appears as "Paste, Clipboard". I want to retain the functionality of "Paste" option but "Clipboard" should not work. I have Made a Custom EditText class where I have callbacks for actions such as cut, paste,copy with respect to their Id. But I am unable to diable "Clipboard" function.
override fun onTextContextMenuItem(id: Int): Boolean {
// Do your thing:
val consumed = super.onTextContextMenuItem(id)
when (id) {
R.id.cut -> onCut()
R.id.copy -> onCopy()
R.id.paste, R.id.pasteAsPlainText -> {
onPaste()
}
android.R.id.keyboardView->{}
R.id.accessibilityActionContextClick->{}
R.id.accessibilityActionHideTooltip->{}
R.id.accessibilityActionImeEnter->{}
}
return consumed
}
Below picture shows the EditText options
and this picture shows when I Click Clipboard option

Is DrawableCompat.setTint() lazy?

I am designing an app that has 3 button in main activity, and several buttons in a fragment. I want to change the color of a button in the fragment, depending on which button of main activity is toggled.
color1.setOnClickListener {
brush_chosen = 1
color1.setBackgroundColor(R.color.black)
color2.setBackgroundColor(0x00000000)
color3.setBackgroundColor(0x00000000)
if (frag_num == 8 ){
frag_8p.set_frag_value(frag_num,brush_chosen)
}
}
The function set_frag_value is :
fun set_frag_value(frag_num:Int,brush:Int) : Int
{
brush_chosen=brush
return brush
}
This change the value of brush_chosen. Then I made a function :
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ib0.setOnClickListener { view ->
Log.d("brush_color","Brush of 0 : "+brush_chosen)
if (brush_chosen==1)
{
Log.d("brush_color","Brush Confirm : "+brush_chosen)
DrawableCompat.setTint(ib0.drawable, ContextCompat.getColor(requireContext(),R.color.rndcolor1))
}
else if (brush_chosen==2)
{
Log.d("brush_color","Brush Confirm : "+brush_chosen)
DrawableCompat.setTint(ib0.drawable, ContextCompat.getColor(requireContext(),R.color.purple_500))
}
else if (brush_chosen==3)
{
Log.d("brush_color","Brush Confirm : "+brush_chosen)
DrawableCompat.setTint(ib0.drawable, ContextCompat.getColor(requireContext(),R.color.teal_200))
}
Log.d("brush_color","End of onclicklistener ")
}
}
I checked the log and theoretically this code should work correctly. However, I found that the button color did not change properly, even I checked my app prints all log correctly. For example, when I clicked button color1 in main activity, variable brush_chosen becomes 1 and the first button in fragment I clicked changes its color. But the second button I clicked does not change its color.
Is there any problem on my code using DrawableCompat ??
Android does some Drawable state caching under the hood. You might need to call mutate() on the Drawable you want to tint and then set the new Drawable in order for the tint to show up properly.

kotlin change the value of a var inside a function (SetOnClickListner)

I have two buttons btn1 (scan_sac) & btn2 (scan_scelle) and two textviews t1 (ed_idSac) & t2 (ed_scelle). Inside the onClickListner of each button, I will call a function that starts the camera to read a barcode. This camera returns the value of the barcode read in a funtion onActivityResult; And inside this function (onActivityResult) I am updating the value of one of the two textViews
According to the clicked button I will change the value of the appropriate textview :
If btn1 is clicked , t1 will be updated
if btn2 is clicked , t2 will be updated
Now I am stuck at testing which button is clicked to update the appropriate textView.
I tied to use a boolean Scelle to check which button is clicked.
If scelle is true then scan_scelle (btn2) is clicked
override fun onActivityResult(requestCode: Int, resultCode: Int, data:Intent)
Use Diffeerent Request Code on click of b1 & b2....
So if I understand correctly you want to know which button is clicked to that you can update proper text view.
A solution would be to have two boolean values for each button, and when the button is pressed you will set that boolean value to true.
Later onActivityResult you can check those boolean values and update proper text views.
use different requestCode for both buttons in your onActivityResult method check requestCode and place your string to respected textView
var buttonIdentity :false
val button1= findViewById<Button>(R.id.textview)
button1.setOnClickListener(clickListener)
val button2 = findViewById<Button>(R.id.button)
button2.setOnClickListener(clickListener)
val clickListener = View.OnClickListener {view ->
when (view.getId()) {
R.id.button1-> buttonIdentity =true
R.id.button2 -> buttonIdentity =false
}
}

Boolean turns into int number 904 in Kotlin

I'm working on a quiz app for my project at university, and I'm trying to save button and boolean that states if the answer is correct or not in data class and save all buttons in a temporary list so I can add onclick listener for all of them later. But when I'm trying to access boolean value, it just turns into number 904. Here's my code regarding these buttons.
val ansBtnList: MutableList<ButtonDataClass> = mutableListOf()
--------------------------------------------------------------
val ans = ButtonDataClass(Button(this), quizToShow.getValue(planets[0]).answers[i].isRight)
--------------------------------------------------------------
ansBtnList.add(ans)
--------------------------------------------------------------
for (i in 0..3) {
ansBtnList[i].btn.setOnClickListener { Log.d(null, ansBtnList[i].btn.right.toString()) }
}
Thanks in advance!
EDIT: ButtonDataClass code:
data class ButtonDataClass (var btn: Button, var right: Boolean)
It's not the right that you think it is. btn is the button. The button has a right field that is related to its coordinate on screen. You should try ansBtnList[i].right.toString() instead.
btn.right refers to right variable of Button class, not to the one present in the ButtonDataClass
replace: ansBtnList[i].btn.setOnClickListener { Log.d(null, ansBtnList[i].btn.right.toString()) }
with: ansBtnList[i].btn.setOnClickListener { Log.d("your_tag_id", ansBtnList[i].right) }
Of course you are not accessing the right field. ansBtnList[i].btn.right gives you the right position of your button in pixels.
Rather do ansBtnList[i].right

Custom animation triggers twice

I have a listener that listens and makes focused items little bit bigger with animation.
private fun focus() {
itemView?.setOnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
val anim : Animation = AnimationUtils.loadAnimation(itemView.context, R.anim.scale_in)
itemView.startAnimation(anim)
anim.fillAfter = true
} else {
val anim : Animation = AnimationUtils.loadAnimation(itemView.context, R.anim.scale_out)
itemView.startAnimation(anim)
anim.fillAfter = true
}
}
}
Besides this listener I also made custom function, that when focused item is clicked, it actually changes size back to normal
fun customFunction(): Unit = with(itemView) {
val anim : Animation = AnimationUtils.loadAnimation(itemView.context, R.anim.scale_out)
itemView.startAnimation(anim)
anim.fillAfter = true
}
PROBLEM: focus() and customFunction() functions work alright. Problem is, when I hit enter on focused element (customFunction() triggers) and element changes it size to normal - which is okay. But the moment I navigate to other element, the previous one scales out twice. How do I need to modify my onFocusListener to know that I shouldn't scale out twice if I've triggered customFunction() by clicking some item. Any idea is welcomed.
if i understand right your question you may have to try : itemView. setOnFocusChangeListener(null); in your customFunction()

Categories

Resources