-2

i am developing one walkie talkie app in that i am using walkie talkie image button. when i click on that image it should show the different image and when realise that button it should show the previous image . bellow code walk1 first image and walk2 is second image which should see when image button is clicked

,,,

walk1=findViewById(R.id.walky)
walk2=findViewById(R.id.walky1)
walk1?.setOnLongClickListener {
            walk2?.visibility=View.VISIBLE
            true // Don't consume event, if return false. Consume event if true.
        }
        walk2?.setOnClickListener {
            walk2?.visibility=View.GONE
        }

,,, in this code its showing second image but when i realised button its not showing first image when i click again that time its showing

  • LongClick and Click are both events that trigger only once. They won't call the method when you release the button, only when you click it. What you need is to change the image when it is pressed, and to revert back when released. This has to be done as described in the question linked above. – shriakhilc Mar 30 '19 at 06:18

1 Answers1

0

You can make use of the setOnTouchListener on your view and then track the motion using the MotionEvent -

    imageView.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
        if (motionEvent.action == MotionEvent.ACTION_DOWN) {
            imageView.setImageResource(R.drawable.istock)
            Toast.makeText(this@MainActivity, "press", Toast.LENGTH_SHORT).show()
        } else if (motionEvent.action == MotionEvent.ACTION_UP) {
            imageView.setImageResource(R.drawable.wo)
            Toast.makeText(this@MainActivity, "release", Toast.LENGTH_SHORT).show()
        }
        false
    })
Kartik Shandilya
  • 3,796
  • 5
  • 24
  • 42