0

I want to perform the actions when the button is pressed not just clicked

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var ib:ImageView=findViewById(R.id.imageView4) as ImageView
        var final=MediaPlayer.create(this,R.raw.`m1`)
        var anim=AnimationUtils.loadAnimation(this,R.anim.rotate)
        ib.setOnTouchListener(){
            textView.text="rip"
            final.start();
            ib.startAnimation(anim);
        }

    }
}

I want the actions to be performed when the button is held and stop when they are not .

Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71
  • Possible duplicate of [Q: Android - Hold Button to Repeat Action](https://stackoverflow.com/questions/4284224/android-hold-button-to-repeat-action) – Gustavo Pagani Jun 21 '19 at 21:53

1 Answers1

0

this seems to be working fine but the compiler gives a warning

ib.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
        when (motionEvent.action){
            MotionEvent.ACTION_DOWN -> {
                textView4.text="IGNORING"
                ib.startAnimation(anim)
                final.setLooping(true)
                final.start()
            }
            MotionEvent.ACTION_UP -> {
                textView4.text="IGNORE"
                ib.clearAnimation()
                final.pause()

            }
        }
        return@OnTouchListener true
    })