Search code examples
androidviewandroid-imageviewandroid-imageonclicklistener

Android how to call another Image after 3 times click on same Image?


I am developing application on Images.

In that if I click on same image the image will replace by another one that already into my Drawable

Right now onclick I am able to display only Toast Message; but I want to replace Image. I don't know How to do? Any Help and suggestion appreciable.

you can take it images name as : img1 ,img2 ,img3.

   ImageView imgV = (ImageView) layout.findViewById(R.id.img1);
    imgV.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {

                        if (v.isClickable()) {
                            i++;
                            String ii = new Integer(i).toString();
                            Log.i("Inside", ii);
                            if (ii.equals("3")) {
                                Toast.makeText(getApplicationContext(), "Call another Image ",Toast.LENGTH_SHORT).show();}

Solution

  • ImageView imgV = (ImageView) layout.findViewById(R.id.img1);
    imgV.setClickable(true);
    int counter = 0;
    
    imgV.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        i++;
        if(i == 3){
        imgV.setImageResource(R.drawable.img2);
        //Or you may want to use
        //imgV.setBackgroundResource(R.drawable.img2);
    
        i = 0;
        }
    }