How to set "ImageView" to be clickable or non-clickable

Viewed 655

I am making a simple memory game. It basically, some ImageView come up and after that user should click ImageView according to order. However, ImageView can also be clickable while sorting. So, I want the ImageViewto be non-clickable while sorting is not finish and then I want ImageViewcan be clickable when the sorting is finished. How can I do that?

-edit 1 After your solutions, i tried image.setEnable = false method in kotlin and i have the result.

here related section kotlin

fun orderImages() {
    index=0
    Collections.shuffle(controlArray)
    println(controlArray)
    runnable = object : Runnable {
        override fun run() {
            for (image in imageArray) {
                image.visibility = View.INVISIBLE
                image.isEnabled = false
            }
            if (index < controlArray.size) {
                imageArray[controlArray[index]].visibility = View.VISIBLE
                index++
            } else {
                handler.removeCallbacks(runnable)
                for (image in imageArray) {
                    image.visibility = View.VISIBLE
                    image.isEnabled = true

                }
            }
            handler.postDelayed(runnable, 1000)
        }
    }
    handler.post(runnable)
    userArray.clear()
}
4 Answers

To programmatically disable clicking listener, use image.setEnabled(false);. However, as you described I think your sorting function is intensive computation so it take along time and it can block UI thread if the time is a few seconds. Therefor you must run the sorting function on a new thread. Assuming you have a button buttonStartSorting in your activity.

final Handler handler = new Handler();
buttonStartSorting.setOnClickListener(new View.OnClickListener() {
    //disalbe listener for images
    for(imv in imageViews){
        imv.isEnable = false;
    }
    //do sort images on new thread
    Thread(Runnable {
        yourSortImagesMethod(imageViews);
        handler.post(Runable{
          //enable listener for images
          for(imv in imageViews){
             imv.isEnable = true;
          }
        })
    }).start()
})

You can simply add setOnClickListener() on imageview and take a flag to make it clickabe or non clickable based on flag state.

private boolean isSorted = false;

somewhere in your code where you are adding view for image list, use click listener-

holder.borrar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(isSorted) {
            //do what you want to do after it is sorted.
        }
    }
});

So in this way onClick will do anything only if the images are sorted. Now somewhere in your code there should be a condition in your class so that the state of the flag will be changed. So it would be something like--

...
//Updating flag based on condition
if(condition) {
    isSorted = true;
}
...

This block of code allows you to enable or disable all views in the layout layout.

fun enableDisableViewGroup(viewGroup: ViewGroup, enabled: Boolean) {
            val childCount = viewGroup.childCount
            for (i in 0 until childCount) {
                val view = viewGroup.getChildAt(i) 
                    view.isEnabled = enabled 
                if (view is ViewGroup) {
                    enableDisableViewGroup(view, enabled)
                }
            }
        }
Related