Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8157535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:19:07+00:00 2026-06-06T17:19:07+00:00

When taping on an ImageView , I want to replace it with other drawable

  • 0

When taping on an ImageView, I want to replace it with other drawable depending on the previous drawable, with some researches I made this:

ImageView imgview = (ImageView) findViewById(R.id.imageView1);
imgview.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {

        ImageView imageView = (ImageView) view;
        assert(R.id.imageView1 == imageView.getId());
        Integer integer = (Integer) imageView.getTag();

        integer = integer == null ? 0 : integer;

        switch (integer) {
        case R.drawable.number1:

            imageView.setImageResource(R.drawable.number2);
            imageView.setTag(R.drawable.number2);
            break;

        case R.drawable.number2:
            imageView.setImageResource(R.drawable.number3);
            imageView.setTag(R.drawable.number3);
            break;

        case R.drawable.number3:
            imageView.setImageResource(R.drawable.number4);
            imageView.setTag(R.drawable.number4);
            break;
        default:
            Toast.makeText(MainActivity.this, "exceeded", Toast.LENGTH_SHORT).show();
        }
    }
});

}

The problem is the app always calls to the toast message in default. It ingores all other treatments/verifications in case sections.

Any idea please to solve that problem?

Thank you very much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:19:08+00:00Added an answer on June 6, 2026 at 5:19 pm

    The reason is

    You have written following statement

    integer = integer == null ? 0 : integer;
    

    So very first time control reached in click block that time tag will be null and above statement will make it 0.But you are doing nothing to handle case 0.

    So in every case your tag will be 0 and it will cause default block execution.If you are setting tag as 0 then you must have switch case for 0.Otherwise it will always be 0 and default block keep on executing.

    Your code snippet should look something like follow.

    ImageView imgview = (ImageView) findViewById(R.id.imageView1);
    imageView.setImageResource(R.drawable.number1);
    imageView.setTag(R.drawable.number1);
    
    imgview.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
    
            ImageView imageView = (ImageView) view;
            assert(R.id.imageView1 == imageView.getId());
            Integer integer = (Integer) imageView.getTag();
    
            switch (integer) {
            case R.drawable.number1:
    
                imageView.setImageResource(R.drawable.number2);
                imageView.setTag(R.drawable.number2);
                break;
    
            case R.drawable.number2:
                imageView.setImageResource(R.drawable.number3);
                imageView.setTag(R.drawable.number3);
                break;
    
            case R.drawable.number3:
                imageView.setImageResource(R.drawable.number4);
                imageView.setTag(R.drawable.number4);
                break;
            default: 
                Toast.makeText(MainActivity.this, "exceeded", Toast.LENGTH_SHORT).show(); 
                imageView.setImageResource(R.drawable.number1);  <----You should reset it to number 1
                imageView.setTag(R.drawable.number1);
            }
        }
    });
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just want to ask that how can I display subsequent images on imageview.
I'm working on a project, that read image file from drawable folder through ImageView,
Taking the jQuery framework for example, if you run code like this: $(document).ready(function init()
Taking a look at my question HERE , I now want to return the
Taking a JavaScript object with 4 properties: function Object() { this.prop1; this.prop2; this.prop3; this.prop4;
taking value in 1st textbox and want to display it in 2nd.. 1st <input
Taking in consideration a table tbl_users , I want to get a list of
I have a view in the UI that is technically an extended ImageView inside
I am creating an application that sends and SMS while taping on a button.
I'm using a UISearchDisplayController with a UISearchBar. I put this UISearchBar in my app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.