Hylonomus
Hylonomus

Reputation: 13

How to change the image of an ImageView when clicked?

This is my code. I'm a beginner to this, but I need to do it quickly. Using an ImageView's onClick, how do I check if the image it is displaying is equal to a drawable and how do I change the image that the ImageVIew is displaying?

My code so far

public void grid1Click(View view) {
    if (((ImageView) findViewById(R.id.grid1)).getDrawable().equals(getResources().getDrawable(R.drawable.grid)))
        ((ImageView) findViewById(R.id.grid1)).setImageDrawable(getResources().getDrawable(R.drawable.gridfilled));
}

Upvotes: 0

Views: 79

Answers (1)

Morteza Soleimani
Morteza Soleimani

Reputation: 2670

Try this :

ImageView imageViwe = (ImageView) findViewById(R.id.image_id);
imageViwe.setOnClickListener(new OnClickListener()
{

    @Override
    public void onClick(View v)
    {
        // Set new image to imageview here

    }
});

Upvotes: 1

Related Questions