I am writing a program which deals with playing cards. Here, I am trying to show the player's hand. I have six imageButtons sixCard0
-- sixCard5
which will each display one card from the player's hand. Each has its default image (of a question-mark card) set in xml:
<ImageButton
android:id="@+id/sixCard0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0dp"
android:src="@drawable/card0" />
I want to change this image to an image representing the card that is already in the player's hand when the activity starts. The cards are kept in CribbageApplication.cardList
. In the activity's onCreate(...)
I have:
findViewById(R.id.sixCard0).setBackgroundResource(getResources().getIdentifier(Card.getImageName(((CribbageApplication) this.getApplication()).getCardList()[0]), "drawable", getPackageName()));
findViewById(R.id.sixCard0).invalidate();
Accessing these values is not where I am having a problem. My problem is that when I run the app, the default image is still displayed on the imageButtons. See: http://s23.postimg.org/cbmxchonv/Screenshot_2014_05_28_22_15_25_1.png
In debugging, I decided to use a smaller image as the imageButton's default image. (In this case it is an icon with text that reads "Set Hand" which I sourced from elsewhere in my program.) Now when I run the app you can clearly see the default "Set Hand" icon there with the new larger card image behind it! See: http://s12.postimg.org/3ytgenaz1/Screenshot_2014_05_28_21_48_10_1.png
Can someone please tell why this is happening?
EDIT: I have fixed the problem. I should be using setImageResource(...)
instead of setBackgroundResource(...)
. In the example above, a typecast is required to call this function:
((ImageButton) findViewById(R.id.sixCard0)).setBackgroundResource(getResources().getIdentifier(Card.getImageName(((CribbageApplication) this.getApplication()).getCardList()[0]), "drawable", getPackageName()));