android - Why is changing of ImageButton's image resource not changing the button's image? -
i'm writing memory game app android , calling setimageresource() method of imagebutton has no effect whatsoever.
so idea have random number of hidden images on screen (in particular case have 4 buttons), implemented imagebuttons r.drawable.image_default image resource , when button "start" gets clicked game begins. when user clicks 1 button shows it's image. when user clicks one, shows image behind , after brief delay 1 of 2 things can happen:
- shown images same , disappear screen
- shown images different both buttons containing them revert images image_default
the problem - when user clicks on first button, setimageresource() gets called , (the button) changes it's image supposed to, when user clicks second button (one of other 3 buttons facing down), setimageresource() gets called again image on button doesn't change.
despite this, else works if change happened (meaning if (images) same, buttons disappear screen, if different, button images flipped face down (simulated image resource getting set image_default)).
the question why button clicked second not change it's image resource , redrawn on screen?
here implementation
import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.imagebutton; public class buttonexample extends appcompatactivity implements view.onclicklistener { private imagebutton imagebutton0, imagebutton1, imagebutton2, imagebutton3; private imagebutton firstimage, secondimage; private button buttonstart; private int[] arrayofimages = {r.drawable.image0, r.drawable.image0, r.drawable.image1, r.drawable.image1}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_button_example); imagebutton0 = (imagebutton) findviewbyid(r.id.imagebutton0); imagebutton0.setonclicklistener(this); imagebutton1 = (imagebutton) findviewbyid(r.id.imagebutton1); imagebutton1.setonclicklistener(this); imagebutton2 = (imagebutton) findviewbyid(r.id.imagebutton2); imagebutton2.setonclicklistener(this); imagebutton3 = (imagebutton) findviewbyid(r.id.imagebutton3); imagebutton3.setonclicklistener(this); buttonstart = (button) findviewbyid(r.id.button_start); buttonstart.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { setdefaultbackgrounds(); } }); } @override public void onclick(view v) { //if first image shown if (firstimage == null) { firstimage = (imagebutton) v; showimage(firstimage); } else { //else first showing, must second image secondimage = (imagebutton) v; //if open image has been clicked if (firstimage.equals(secondimage)) { //just hide image hideimage(firstimage); } else { /* else show second image. (this problem lies, method gets called imagebutton not redrawn) */ showimage(secondimage); //sleep let user see image clicked on try { thread.sleep(200); } catch (interruptedexception e) { e.printstacktrace(); } //if user opened 2 same images if (equalimages(firstimage, secondimage)) { //make buttons invisible firstimage.setvisibility(view.invisible); secondimage.setvisibility(view.invisible); } else { //else hide images hideimage(firstimage); hideimage(secondimage); } } firstimage = null; secondimage = null; } } //initialize button images private void setdefaultbackgrounds() { imagebutton0.setimageresource(r.drawable.image_default); imagebutton0.setvisibility(view.visible); imagebutton0.settag(arrayofimages[0]); imagebutton1.setimageresource(r.drawable.image_default); imagebutton1.setvisibility(view.visible); imagebutton1.settag(arrayofimages[1]); imagebutton2.setimageresource(r.drawable.image_default); imagebutton2.setvisibility(view.visible); imagebutton2.settag(arrayofimages[2]); imagebutton3.setimageresource(r.drawable.image_default); imagebutton3.setvisibility(view.visible); imagebutton3.settag(arrayofimages[3]); } //show image private void showimage(imagebutton ib) { if (ib.equals(imagebutton0)) { ib.setimageresource(arrayofimages[0]); } else if (ib.equals(imagebutton1)) { ib.setimageresource(arrayofimages[1]); } else if (ib.equals(imagebutton2)) { ib.setimageresource(arrayofimages[2]); } else { ib.setimageresource(arrayofimages[3]); } } //are images same private boolean equalimages(imagebutton ib1, imagebutton ib2) { return ib1.gettag().equals(ib2.gettag()); } //hide image private void hideimage(imagebutton ib) { ib.setimageresource(r.drawable.image_default); } }
and here layout
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginbottom="30dp" android:layout_marginend="30dp" android:layout_marginstart="30dp" android:layout_margintop="30dp" android:background="#cdcdcb" android:baselinealigned="false" android:orientation="vertical" tools:context="com.example.schonn.myproject.buttonexample"> <relativelayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3"> <button android:id="@+id/button_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="97dp" android:text="@string/start" /> </relativelayout> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <imagebutton android:id="@+id/imagebutton0" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:adjustviewbounds="true" /> <imagebutton android:id="@+id/imagebutton1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:adjustviewbounds="true" /> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <imagebutton android:id="@+id/imagebutton2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:adjustviewbounds="true" /> <imagebutton android:id="@+id/imagebutton3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:adjustviewbounds="true" /> </linearlayout> </linearlayout>
may suggest alternate approach problem. question, getting when click on image1, should change image2, , when click on image2, should change image1. approach use this
firstly put both of images in framelayout. , make image2 invisible default image2.setvisibility(view.invisible)
. see image1. when click on image1, in onclick listener of image1, set image1 invisible , image2 visible . in image2, onclicklistener, set image2 invisible , image1 visible
Comments
Post a Comment