How to check which current image resource is attached to ImageView in android xml?

前端 未结 6 702
说谎
说谎 2020-12-05 01:48

I want to check which image resource is attached to ImageView in xml, I am able to check that which image resource is attached to image view but my requirement

6条回答
  •  -上瘾入骨i
    2020-12-05 02:35

    You can do something like following,

    Set a tag either through xml or dynamically as per your requirement.

    Through xml,

    
    

    Dynamically,

    ImageView imageView=(ImageView)findViewById(R.id.imageview1);
    imageView.setTag("bg");
    

    Use imageView.getTag() to retrieve image information.

    String backgroundImageName = String.valueOf(imageView.getTag()); 
    

    Edit:

    if you are changing ImageView background frequently then,

    imageView.setTag(R.drawable.drawablename);
    imageView.setImageResource(R.drawable.drawablename);
    String backgroundImageName = String.valueOf(imageView.getTag());
    

    Now you can make your check.

    if (backgroundImageName.equals("bg"))  // here "bg" is the tag that you set previously
        {
          Toast.makeText(_con, "Image is ivPic", Toast.LENGTH_LONG).show(); 
          // new RegisterAsyntaskNew().execute(); 
        } 
      else 
        {
         Toast.makeText(_con, "Image isn't ivPic", Toast.LENGTH_LONG).show(); 
         // new RegisterAsyntask().execute(); 
        }
    

提交回复
热议问题