Reputation: 89
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.
Upvotes: 0
Views: 662
Reputation: 4678
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.
Upvotes: 3
Reputation: 3349
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)
Upvotes: 1
Reputation: 925
You are comparing tags rather than comparing the images.
Check out this link about how to compare two images :- compare two images in android
Upvotes: 0
Reputation: 1020
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!");
}
}
Upvotes: 0
Reputation: 137
May it be that new String("myTag").equals("myTag2")
gives false?
Try to print input1 and input2 to corroborate.
Upvotes: 0