0

I want to assign the path-String to an imageview and a value to the button, so when a user clicks on the button, I can ask for the value. But I don't want to show the value to the user, so I don't want to write the text on the button. Is this possible and how?

M_B
  • 49
  • 7

1 Answers1

1

Using a tag is good if you're looking for a simple solution, but it's also somewhat of a patchwork approach.

  • It may not be obvious to others what the tag is used for.
  • You lose type safety because the tag is an Object.
  • There's no guarantee the tag will be right, someone may change it inadvertently. - There's also no good way to pass multiple values (although that doesn't seem to be your case).

See also this answer. I recommend using a custom view class with a property instead:

class PathImageView extends AppCompatImageView {
    private String path;

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

    public void setPath(String path) {
        this.path = path;
    }

    public String getPath() {
        return path;
    }
}

You can even improve that a bit and allow for setting the value with an XML attribute.

Nicolas
  • 6,611
  • 3
  • 29
  • 73
  • Thank you for your answer. Does it work the same for buttons with AppCompatButtonView? – M_B Jan 23 '21 at 22:17