How to Send Drawable as a parameter to a function

1.5k views Asked by At

I am trying to set Imageview using a function

that if we send Image drawable to a function that function set that drawable to the respective Imageview.

This is function code in which it gets "id" and after that respective id function will call

public void showpeg(int id) {
    switch (id) {
    case 1:
        showSeagreen(R.drawable.seagreen);
        break;
    }

}

This is my ShowSeagreen() function

public void showSeagreen(Drawable draw) {
    if (iRow == 1) {
        if (iPlace == 1) {
            seagreen = 1;
            one_first.setBackgroundResource(R.drawable.draw);
            iPlace++;
        } else if (iPlace == 2) {
            seagreen = 2;
            one_two.setBackgroundResource(R.drawable.draw);
            iPlace++;
        }
 }

I know i am doing it wrong but i can't find a way to do this Please any help

Thanks in advance

3

There are 3 answers

2
A.S On BEST ANSWER

instead of

showSeagreen(R.drawable.seagreen);

use:

showSeagreen(getResources().getDrawable(R.drawable.seagreen));
1
SiloƩ Bezerra Bispo On

Any Resources is a int. Do like this to "showSeagreen":

public void showSeagreen(int draw) {
    if (iRow == 1) {
        if (iPlace == 1) {
            seagreen = 1;
            one_first.setBackgroundResource(draw);
            iPlace++;
        } else if (iPlace == 2) {
            seagreen = 2;
            one_two.setBackgroundResource(draw);
            iPlace++;
        }
 }
0
Nadeem On

If what i understand from your question is true then i think your aim is to add drawable image as a parameter if thats what you want then you should add the

  @DrawableRes int draw

like

public void showSeagreen(@DrawableRes int draw) {

}

as your parameter then you can add drawable files in here