0

I am creating an Android program and when a button is clicked, it checks if the 2 images are the same (ImageView and EmojiconTextView). EmojiconTextView is from a library that I am using in my app.

public void clickedCheck(View view) {

    String input = emojiconTextView.getTag().toString();
    String input2 = myRandomImage.getTag().toString();

    if (input.equals(input2)) {
        checkingText.setText("Well Done!");
    } else {
            checkingText.setText("Unlucky!");
    }
}

However, when I click the button, the program displays "Unlucky" even if the images are equal. So it totally disregards my 'if-statement'.

This is my ImageView attribute:

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myRandomImage"
        android:tag="myTag2"
        android:layout_above="@+id/emojicon_text_view"
        android:layout_centerHorizontal="true"/>

And this is is from my EmojiconTextView:

<hani.momanii.supernova_emoji_library.Helper.EmojiconTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/emojicon_text_view"
        android:textColor="@android:color/black"
             android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:text="Emojicon Text View"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:tag="myTag"
        android:layout_marginTop="26dp"/>

If you need further help with understanding the question, please let me know.

3
  • 1
    You compare tags and you use different tags in xml Commented Jul 13, 2017 at 12:55
  • So must the tags be the same? Commented Jul 13, 2017 at 12:59
  • 1
    ur clearly comparing two strings therfore they must be same Commented Jul 13, 2017 at 13:00

5 Answers 5

3

Because you are using different tag in your xml code. See below

you are setting tag EmojiiconTextView as

android:tag="myTag"

on the other hand you are setting tag on ImageView as

android:tag="myTag2"

And you are comparing tags using equal method as

 if (input.equals(input2)) {
    checkingText.setText("Well Done!");
} 

How could they be equal? :)

Hope that makes sense.

Sign up to request clarification or add additional context in comments.

5 Comments

So are you saying that my tags must be the same?
I am not saying they must be equal. I am pointing the wrong thing you are doing. Your testing conditional statements suggests they both must be same to pass equal test.
Ohh, I see. So is there a correct way to do this and get them to compare? :)
However, my code is still not working the way I want it too :)
I would like to compare the ImageView and the EmojiconTextView. I want my program to see whether they're the same images, or different images. See the ImageView displays a random image and then a user selects another image. And if the user selected the correct image (the images are equal in the ImageView and EmojiconTextView then it must say "Well Done", otherwise if the user is wrong, then it must display "Unlucky" :)
1

Like @AbulWaheed said, since you are setting your tag inside of your xml resource file, every time you do

 if (input.equals(input2)) {
    checkingText.setText("Well Done!");
} else {
        checkingText.setText("Unlucky!");
}

you will always compare the tag that set inside of your xml file. To get around this are you setting the drawable that will show up for the ImageView in the Java code?

If you are, you can also set the tag with imageview.setTag("tagName") for I'm assuming both the emojiView and the imageView.

That way, you can have some sort of map HashMap<Drawable, String> map and when you choose what Drawable you are going to use in your imageview, you automatically set the tag.

String tag = map.get(Drawable)
imageview.setTag(tag)

Comments

0

May it be that new String("myTag").equals("myTag2") gives false? Try to print input1 and input2 to corroborate.

Comments

0
public void clickedCheck(View view) {

    int input = Integer.parseInt(emojiconTextView.getTag());
    int input2 = Integer.parseInt(myRandomImage.getTag());

    if (input == input2) {
        checkingText.setText("Well Done!");
    } else {
        checkingText.setText("Unlucky!");
    }
}

2 Comments

It says "Incompatible Types"
I have tried your edited answer, and it 'parseInt (java.lang.String) in Integer cannot be applied to (java.lang.Object)
0

You are comparing tags rather than comparing the images.

Check out this link about how to compare two images :- compare two images in android

1 Comment

Unfortunately that question says a camera image, these are not 2 camera images.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.