I am wanting to create an Android app (using Android Studio) whereby a user will see a random image that I've generated in an array. They then have to select the correct image from a horizontal scroll view that corresponds to the random image that was displayed. However, I can not seem to write the correct code to check whether the user has clicked on the correct, corresponding image. I am using an if-statement.
This is my activity_main.xml:
<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/face_with_tears_of_joy"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/funny_emoji"
android:onClick="clickedFaceWithTearsOfJoy"
android:clickable="true"
app:srcCompat="@drawable/face_with_tears_of_joy" />
<ImageView
android:id="@+id/grinning_face"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/funny_emoji"
android:onClick="clickedGrinningFace"
android:clickable="true"
app:srcCompat="@drawable/grinning_face" />
<ImageView
android:id="@+id/grinning_face_with_smiling_eyes"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:clickable="true"
android:contentDescription="@string/funny_emoji"
android:onClick="clickedGrinningFaceWithSmilingEyes"
app:srcCompat="@drawable/grinning_face_with_smiling_eyes" />
<ImageView
android:id="@+id/rolling_on_the_floor_laughing"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/funny_emoji"
app:srcCompat="@drawable/rolling_on_the_floor_laughing" />
<ImageView
android:id="@+id/smiling_face_with_open_mouth"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/funny_emoji"
app:srcCompat="@drawable/smiling_face_with_open_mouth" />
<ImageView
android:id="@+id/smiling_face_with_open_mouth_and_cold_sweat"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/funny_emoji"
app:srcCompat="@drawable/smiling_face_with_open_mouth_and_cold_sweat" />
<ImageView
android:id="@+id/smiling_face_with_open_mouth_and_smiling_eyes"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/funny_emoji"
app:srcCompat="@drawable/smiling_face_with_open_mouth_and_smiling_eyes" />
</LinearLayout>
</HorizontalScrollView>
This is my MainActivity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
random = new Random();
textView = (TextView) findViewById(R.id.textView);
int randomInt = random.nextInt(hard.length);
imageView.setBackgroundResource(hard[randomInt]);
}
public void clickedFaceWithTearsOfJoy(View view) {
if (imageView.equals(R.id.face_with_tears_of_joy)) {
textView.setText("Well Done!");
}
}
public void clickedGrinningFace(View view) {
if (imageView.equals(R.drawable.grinning_face)) {
textView.setText("Well Done!");
}
}
public void clickedGrinningFaceWithSmilingEyes(View view) {
if (imageView.equals(R.drawable.grinning_face_with_smiling_eyes)) {
textView.setText("Well Done!");
}
else {
textView.setText("Unlucky!");
}
}
Aucun commentaire:
Enregistrer un commentaire