Phantomazi Phantomazi - 10 months ago 36
Android Question

How to pass a resource to a method call in Android?

At the moment I have one huge switch-case statement, which I want to simplify. I want to extract the all-repetitive statements to a method and call this method with the appropriate parameter. Example of what it is now could be:

switch (colouprivate void setRoundCornersStyle(Resources resource){
chooseCategory_spinner.setBackgroundResource(resource);r) {
case "#FF9800":

where this is shortened. All the calls are the same, the only difference being the actual colour passed.

The question in place is as follows, how do we pass resources to method calls in Android, something like this:

case "#FF9800":

private void setRoundCornersStyle(Resources resource){

I hope that this way I will be able to actually extract all the repetitions and make this clear and simple. Any suggestions are welcome!


The resource identifiers that are stored in the R object are really nothing more than a bunch of integers, representing the actual resources.

You can see this also in the API docs. The signature of the view's setBackgroundResource method looks like this:

void setBackgroundResource (int resid)

For this reason, you can pass the resource id as an int into your method:

private void setRoundCornersStyle(int resource){