BIG T3 view

On BIG T3, the flags are resources loaded dynamically.

This was my problem: in the application Brothers In Games T3, I needed to display some images depending on a parameter. Those images are country flags and are the one of the country where the event is located. Those flags images are resources stored in the app. Their name is standardized with the country ISO code.

Now, how to load a resource when you have to compose its name ? I mean something more elegant than a brute if country then load resource elsif other country…

Fortunately, Android offers an elegant and easy way to do so, but maybe not that obvious, so lets see how it works.First, to understand the problem and how it works, you have to know how resources are managed on Android. Android only know about resources IDs which are generated in an R class during compilation (see the resources documentation). You can’t access this ID during your development phase, that is why you use something like:


But my flags names contains the (ISO) name of the country. So I should have something like


for France and


for Japan.

It is not possible to dynamically build the R.drawable.flag_ definition. But to be honest, we don’t really care as this statement only reference a ID which is an integer. setImageResources parameter is only an integer. So we just have to ask Android to give us that ID. And yes, the API allows that.

To get the resource identifier, we need to call the getIdentifier method from the Resources class. Yes, everything is explained in the API. This method expects only Strings. So it is now easy to build the resource name. In the following example, the getCountry method from the tournament object returns an ISO code of the country.

int resourceId = context.getResources().getIdentifier("flag_" + tournament.getCountry(), "drawable", "net.labasland.big.t3");

if (resourceId > 0) {
} else {
    // Do something is the resource does not exist

This method makes resources discovery very easy. getIdentifier will return an identifier as an integer which we just have to set to our view. If that identifier is 0, then no resources have been found and we can easily deal with it. There is nothing hard-coded here except the usage of a standardized naming. This mean that if we add any new resource (as a new flag), we don’t have to modify the code.

So, it is pretty easy to retrieve a resource ID on Android, deal with it and deal of a missing resource.

About Darko Stankovski

Darko Stankovski is the founder and editor of Dad 3.0. You can find more about him trough the following links.