lundi 21 janvier 2019

Can't compare Drawables in Java Android

Good day, I can't compare several Drawables in an Android app that I am making in Android Studio. I have searched all over the Internet but I cannot seem to find the correct resolution.

When the user clicks on image, it must check to see whether the same image(R.drawable) is currently being displayed, which is supposed to be displayed at random in an imageView.

When the Activity loads, it displays a random image from an array inside an imageView. However, when the user clicks on the correct corresponding image, it will not compare the two Drawables.

When the user clicks on an imageView that is in the horizontal scroll layout, it must check to see whether the imageView they clicked on is the same as the one being displayed.

I also know that I am comparing an imageView and an Int

I have thought about just creating 1 onClick method that will compare the Drawables, but not sure how to do that. Please if anyone could help, it would be much appreciated!

My MainActivity:

public class MainActivity extends AppCompatActivity {

    public ImageView imageView;
    public Random random;
    public TextView textView;
    int easy[] = {R.drawable.img1, R.drawable.img2, R.drawable.img3};

    int hard[] = {R.drawable.img1, R.drawable.img2, R.drawable.img3,
            R.drawable.img4, R.drawable.img5, R.drawable.img6,
            R.drawable.img7};

    public ImageView imageView1;
    public ImageView imageView2;
    public ImageView imageView3;
    public ImageView imageView4;
    public ImageView imageView5;
    public ImageView imageView6;
    public ImageView imageView7;


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

        random = new Random();

        textView = (TextView) findViewById(R.id.textView);
        imageView = (ImageView) findViewById(R.id.imageView);

        imageView1 = (ImageView) findViewById(R.id.IMG1);
        imageView2 = (ImageView) findViewById(R.id.IMG2);
        imageView3 = (ImageView) findViewById(R.id.IMG3);
        imageView4 = (ImageView) findViewById(R.id.IMG4);
        imageView5 = (ImageView) findViewById(R.id.IMG5);
        imageView6 = (ImageView) findViewById(R.id.IMG6);
        imageView7 = (ImageView) findViewById(R.id.IMG7);

        int randomInt = random.nextInt(hard.length);
        imageView.setBackgroundResource(hard[randomInt]);

        if (imageView.equals(R.drawable.img1)) {
            textView.setText("img1");

        } else if (imageView.equals(R.drawable.img2)) {
            textView.setText("img2");

        } else if (imageView.equals(R.drawable.img3)) {
            textView.setText("img3");

        } else if (imageView.equals(R.drawable.img4)) {
            textView.setText("img4");

        } else if (imageView.equals(R.drawable.img5)) {
            textView.setText("img5");

        } else if (imageView.equals(R.drawable.img6)) {
            textView.setText("img6");

        } else if (imageView.equals(R.drawable.img7)) {
            textView.setText("img7");
        }

    }

    public void clickedIMG1(View view) {

        if (textView.getText().equals("img1")) {
            textView.setText("Image 1 is displayed");
        }
    }

    public void clickedIMG2(View view) {

        if (imageView.getTag().equals("img2")) {
            textView.setText("Image 2 is displayed");
        }
    }

    public void clickedIMG3(View view) {

        if (imageView.getTag().equals("img3")) {
            textView.setText("Image 3 is displayed");
        }
    }

    public void clickedIMG4(View view) {

        if (imageView.getTag().equals("img4")) {
            textView.setText("Image 4 is displayed");
        }
    }

    public void clickedIMG5(View view) {

        if (imageView.getTag().equals("img5")) {
            textView.setText("Image 5 is displayed");
        }
    }

    public void clickedIMG6(View view) {

        if (imageView.getTag().equals("img6")) {
            textView.setText("Image 6 is displayed");
        }
    }

    public void clickedIMG7(View view) {

        if (imageView.getTag().equals("img7")) {
            textView.setText("Image 7 is displayed");
        }
    }
}



My activity_main:

<android.widget.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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="210dp"
        android:layout_height="177dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="89dp"
        android:layout_marginTop="111dp"
        android:contentDescription="@string/funny_emoji"
        tools:layout_editor_absoluteX="120dp"
        tools:layout_editor_absoluteY="232dp" />

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="205dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="54dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/IMG1"
                android:layout_width="50dp"
                android:layout_height="55dp"
                android:layout_marginLeft="5dp"
                android:contentDescription="@string/funny_emoji"
                android:clickable="true"
                android:onClick="clickedIMG1"
                android:src="@drawable/img1" />

            <ImageView
                android:id="@+id/IMG2"
                android:layout_width="50dp"
                android:layout_height="55dp"
                android:layout_marginLeft="5dp"
                android:contentDescription="@string/funny_emoji"
                android:clickable="true"
                android:onClick="clickedIMG2"
                android:src="@drawable/img2" />

            <ImageView
                android:id="@+id/IMG3"
                android:layout_width="50dp"
                android:layout_height="55dp"
                android:layout_marginLeft="5dp"
                android:clickable="true"
                android:onClick="clickedIMG3"
                android:contentDescription="@string/funny_emoji"
                android:src="@drawable/img3" />

            <ImageView
                android:id="@+id/IMG4"
                android:layout_width="50dp"
                android:layout_height="55dp"
                android:layout_marginLeft="5dp"
                android:contentDescription="@string/funny_emoji"
                android:onClick="clickedIMG4"
                android:src="@drawable/img4" />

            <ImageView
                android:id="@+id/IMG5"
                android:layout_width="50dp"
                android:layout_height="55dp"
                android:layout_marginLeft="5dp"
                android:onClick="clickedIMG5"
                android:contentDescription="@string/funny_emoji"
                android:src="@drawable/img5" />

            <ImageView
                android:id="@+id/IMG6"
                android:layout_width="50dp"
                android:layout_height="55dp"
                android:layout_marginLeft="5dp"
                android:onClick="clickedIMG6"
                android:contentDescription="@string/funny_emoji"
                android:src="@drawable/img6" />

            <ImageView
                android:id="@+id/IMG7"
                android:layout_width="50dp"
                android:layout_height="55dp"
                android:layout_marginLeft="5dp"
                android:onClick="clickedIMG7"
                android:contentDescription="@string/funny_emoji"
                android:src="@drawable/img7" />



        </LinearLayout>
    </HorizontalScrollView>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="154dp"
        android:layout_marginTop="21dp"
        android:text="TextView" />

</android.widget.RelativeLayout>

Aucun commentaire:

Enregistrer un commentaire