0

I am trying to convert a gif when I realised that it should be normal. Code in Kotlin below. I have tried using long press but when I am releasing, its not returning to image.

var walk1: ImageView?=null
var walk2: ImageView?=null
 walk1=findViewById(R.id.walky)
        walk2=findViewById(R.id.walky1)

        walk1?.setOnLongClickListener { walk2?.visibility = View.VISIBLE
           walk2?.setOnClickListener(){ walk2?.visibility=View.GONE
              return@setOnTouchListener true`enter code here`
           }
            return@setOnLongClickListener true

        }
Ryan Godlonton-Shaw
  • 584
  • 1
  • 5
  • 18
  • 2
    Please format your code and explain your problem (both question title and content) more detail then we can help you. Currently I think most of people don't know what is your problem and what do you want to have – Linh Mar 29 '19 at 05:48

1 Answers1

0

Edit:

Your question is asking two thing. One is about OnLongClickListener and I answered below by original answer.

And the other is about displaying gif, and you need to use image library, named Glide.

Read this:
https://stackoverflow.com/a/44493206/850347

First, add this code to your app/build.gradle

repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.9.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}

And then, sync your gradle. After you edit build.gradle file, you can see notice bar (top of the screen).

Next, add this code to your kotlin file. Change R.raw.image_gif to your gif file name.

Glide.with(this).asGif().load(R.raw.image_gif).into(walk1);

Original:

You are setting LongClickListener on walk1, and ClickListener on walk2.

In this case, you need to focus on the return value of the LongClickListener.

Almost every method associated with event, return true means "I want to consume event and don't want it propagated. This point is end of the event." return false means "I just want to process my event, and the event will propagate to it's parent view (or child view)"

Last line of kotlin method (which needs return) means "return". So in this code, false is equivalent to return false.

val walk1: ImageView = findViewById(R.id.walky)
val walk2: ImageView = findViewById(R.id.walky1)

walk1.setOnLongClickListener {
    walk2.visibility = View.VISIBLE
    false // Don't consume event, if return false. Consume event if true.
}

walk2.setOnClickListener {
    walk2.visibility = View.GONE
}
Stanley Ko
  • 3,383
  • 3
  • 34
  • 60