2

I am working on an android application in which I want to set an image in image view and by clicking the button I got the corresponding text from the string array. I already put my images in drawable folder enter image description here Array of string is :

private String[] captions =new String[] {
        /*0*/"man in white shirt and glasses is sitting at table",
        /*1*/"man in black shirt is playing the guitar",
        /*2*/"man in blue shirt is standing in front of water",
        /*3*/"two dogs are running through the snow",
        /*4*/"group of people are standing in front of crowd",
        /*5*/"two dogs are playing in the snow",
        /*6*/"group of people are sitting at the table",
        /*7*/"man in blue shirt is standing in front of building",
        /*8*/"man in blue shirt is standing in the water of the water",
        /*9*/"man in black shirt playing the guitar",
        /*10*/"man in black shirt and blue is sitting on the beach",
        /*11*/"group of people are standing in front of building",
        /*12*/"group of people are standing in the street",
        /*13*/"man in white shirt is holding his face",
        /*14*/"man in white shirt is cooking",
        /*15*/"two men are playing in the grass",
        /*16*/"man in black shirt and white is playing the guitar",
        
};

I am enable to use the if condition here. I am using view binding in this project that's why I access the button like binding.buttonid.

 binding.detectCaption.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //binding.displayView.setText("two dogs are running through the snow");
            if (binding.imageView==R.drawable.one)//This line shows error{
                binding.displayView.setText(captions[0]);
            }
           

        }
    });

Please tell me how to do this.

Trishant Saxena
  • 388
  • 3
  • 14

3 Answers3

0

In your condition you are checking if your ImageView object is equal to drawable which are not the same objects.

Nice solution would be, that when setting drawable to your imageview, you also put tag like:

binding.imageView.setTag("Your tag");

I am not sure how you set those images but I assume by some Integers, so here, in your tag, you could set Integer of your image ("1" for "one.png", "2" for "two.png" etc) and then you would not even need to use if condition and just set description like

binding.displayView.setText(captions[(int) binding.imageView.getTag()]);
Dorian Pavetić
  • 123
  • 1
  • 8
  • 2
    I set images in ImageView using the gallery of the phone. But I am not able to get the information of that uploaded image. Suggest me a method of how I get this information so that I can easily compare in if-condition. – Trishant Saxena Jan 11 '21 at 15:18
  • I am not sure what exactly are you seeking for. Here is how you can retrieve image name: https://stackoverflow.com/questions/23777027/getting-image-name-of-image-selected-from-gallery-in-android After that you can set any tag that will help you to identify your caption. Use "getTag()" in your if, not ImageView objects – Dorian Pavetić Jan 12 '21 at 19:31
0

The problem is you're trying to compare an ImageView to an Integer (the Drawable's resource id).

Another, more general, problem is that the ImageView's id doesn't match the id of its resource (i.e. the Drawable), so your condition is flawed either way.

What you can do is setting the ImageView's tag according to the current Drawable's id and base your condition on that, but this only makes sense when you set the resources programmatically or if you really need to know what the current Drawable is. Use @Dorian Pavetić's answer if that's not the case.

// first set a drawable as example
binding.imageView.setImageResource(R.drawable.one);
// and now give the imageView a tag that matches the resource's id
binding.imageView.setTag(R.drawable.one);

// change the condition:
binding.detectCaption.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //binding.displayView.setText("two dogs are running through the snow");
            Object tag = binding.imageView.getTag(); 
            if ( tag != null && (int) tag == R.drawable.one) {
                binding.displayView.setText(captions[0]);
        }


    }
});    
TimonNetherlands
  • 1,033
  • 1
  • 6
  • 6
  • 1
    In this method, you already set an image to ImageView, but in my case, I set images from the gallery(the same images which I uploaded in the drawable folder). Here I am unable to get the name or Id of that photo which I uploaded via gallery. Suggest me how do I get that information. – Trishant Saxena Jan 11 '21 at 15:16
0

You cannot get drawable id from ImageView. So you could use tag, customize ImageView, or any other ways. To use tag, it's already mentioned by @TimonNetherlands and @Dorian Pavetić.

So I describe about the customize ImageView.

public class YourImageView extends ImageView {

    private int drawableId;

    public YourImageView(Context context) {
        super(context);
    }

    public YourImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public YourImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
        this.drawableId = resId;
    }

    public int getResourceId() {
        return drawableId;
    }
}
Daniel.Wang
  • 2,242
  • 2
  • 9
  • 27