0

I am creating a Android application in Android Studio.

The screen displays an ImageView that generates a random emojicon (I used an array). A keyboard (3rd-party keyboard implemented) then pops up and a user must find that same emojicon. Once the user has found the emojicon and click it, it then displays the emojicon on the screen in an EmojiconTextView. And then if the 2 emojicons are the same, it will display "Well Done!".

I am using this library in order to use the emojicons in my app::

com.github.hani-momanii:SuperNova-Emoji:1.0

I think the problem might be in my 'if statement' at the bottom in my MainActivity.java. Because I am checking if the ImageView is equal to to the EmojiconTextView, and the objects are different. However, how can I write it where it will work?

package com.rptest.phase3;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Random;

import hani.momanii.supernova_emoji_library.Actions.EmojIconActions;
import hani.momanii.supernova_emoji_library.Helper.EmojiconEditText;
import hani.momanii.supernova_emoji_library.Helper.EmojiconTextView;

public class MainActivity extends AppCompatActivity {

View rootView;
EmojiconEditText emojiconEditText;
EmojiconTextView emojiconTextView;
ImageView emojiButton;
ImageView submitButton;
EmojIconActions emojIcon;
Button myButton;
ImageView myRandomImage;
Button checkButton;
TextView checkingText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rootView = (RelativeLayout) findViewById(R.id.activity_main);
    emojiButton = (ImageView) findViewById(R.id.emoji_button);
    submitButton = (ImageView) findViewById(R.id.submit_button);
    emojiconEditText = (EmojiconEditText) findViewById(R.id.emojicon_edit_text);
    emojiconTextView = (EmojiconTextView) findViewById(R.id.emojicon_text_view);
    myButton = (Button) findViewById(R.id.myButton);
    myRandomImage = (ImageView) findViewById(R.id.myRandomImage);
    checkButton = (Button) findViewById(R.id.checkButton);
    checkingText = (TextView) findViewById(R.id.checkingText);
    emojIcon = new EmojIconActions(getApplicationContext(), rootView, emojiButton, emojiconEditText);
    emojIcon.ShowEmojicon();

    //shows what emotion user has selected
    submitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String message = emojiconEditText.getText().toString();
            emojiconTextView.setText(message);
        }
    });

    //generates random emotion from list (array)
    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int[] cards={R.drawable.emoji_1f600,R.drawable.emoji_1f004,R.drawable.emoji_1f6ac,R.drawable.emoji_1f3a1,R.drawable.emoji_1f3cb_1f3ff,R.drawable.emoji_1f3a9,R.drawable.emoji_0030};
            Random r = new Random();
            int n = r.nextInt(7);
            myRandomImage.setImageResource(cards[n]);
        }
    });

    //checks the emotion input with the emotion displayed
    checkButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (myRandomImage.equals(emojiconTextView)) {
                checkingText.setText("Well done!");
            }
        }
    });
 }

}

And this is my activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_main"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/emoji_button"
        android:src="@mipmap/smiley"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" />

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/submit_button"
        android:padding="4dp"
        android:src="@drawable/ic_action_send_now"
        android:layout_above="@+id/emojicon_edit_text"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp" />

    <hani.momanii.supernova_emoji_library.Helper.EmojiconEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/emojicon_edit_text"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/emoji_button"
        app:emojiconSize="28sp"
        android:layout_alignParentEnd="true" />

    <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:layout_marginTop="26dp"/>

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myButton"
        android:text="Generate Random!"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkButton"
        android:text="Check!"
        android:layout_below="@+id/myButton"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Checking..."
        android:id="@+id/checkingText"
        android:layout_below="@+id/checkButton"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

If you need further information regarding this question, please let me know!

MSP
  • 89
  • 1
  • 2
  • 10
  • may be something like ((EmojiconTextView)myRandomImage.getImageResource()).equals(emojiconTextView) would work for you. I am not entirely sure if EmojiconTextView is extended from ImageView or the cast is possible, but the idea is .equals() works for objects from same class. If that does not work, try switching to comparing some property like emoji name instead of comparing the image object. – digidude Jul 05 '17 at 17:13
  • @digidude - It doesn't seem to work. I'll try comparing the ID's or the name like you said. But thank you for your help anyways. – MSP Jul 05 '17 at 17:39
  • yeah. I suspected that and I was just speculating because you were setting R.drawable.emoji as imageResource for myRandomImage and I thought it could be cast to EmojiconTextView. Anyways as I mentioned earlier .equals() compares objects from same class so comparing ID's (int/long) or Name(String) would reduce it to comparison of similar objects. – digidude Jul 05 '17 at 17:53
  • @digidude - Yeah, I understand that the .equals() only compares the same objects, and I knew it wouldn't work. But i can't think of a way to solve this problem. I think I must try use the emoji's name/id, as they have unique names under the drawable folder, and write the 'if statement' around that. – MSP Jul 05 '17 at 18:08

0 Answers0