Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get drawable from ImageView

I am currently building an app that has 6 images on the main view and it generates random images from my drawable folder (shuffling a deck of cards)

initializing the decks:

public static void initDecks() {
    int m = 0;
    for (int i = 0; i < suites.length; i += 1) {
        for (int j = 0; j < regularCards.length; j += 1) {
            regularDeck[m++] = "drawablw/" + suites[i] + regularCards[j]
                    + ".jpg";
        }
    }
    m = 0;
    for (int i = 0; i < suites.length; i += 1) {
        for (int j = 0; j < trickCards.length; j += 1) {
            trickDeck[m++] = "drawable/" + suites[i] + trickCards[j]
                    + ".jpg";
        }
    }

    Collections.shuffle(Arrays.asList(regularDeck));
    Collections.shuffle(Arrays.asList(trickDeck));
}

shuffle the deck:

public static String[] getCards(int size) {
    String[] result = new String[size];
    for (int i = 0; i < size - 2; i += 1) {
        result[i] = regularDeck[i];
    }
    result[size - 1] = trickDeck[0];
    Collections.shuffle(Arrays.asList(result));
    return result;
}

in my main activity, I assign the cards to the view and when the user clicks on them, I want to know if the image is a trick card or a regular one.

is there a way to find out if the card that was clicked a trick one or not? something like if(image.getImageDrawable().equals(R.drawable.trick.jpg)?

like image 353
thepoosh Avatar asked Apr 29 '26 01:04

thepoosh


1 Answers

Store the name/id of the drawable at the point where you set it to the ImageView. So you always have a member variable in your class/activity that holds the current image identifier.

like image 158
WarrenFaith Avatar answered Apr 30 '26 16:04

WarrenFaith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!