Question

Which is the difference between using:

getResources().getText(R.string.hello_world)

and:

R.string.hello_world

The second way, should return an int object. I've just tried:

Toast.makeText(getApplicationContext(), getResources().getText(R.string.hello_world), Toast.LENGTH_LONG).show();

And:

Toast.makeText(getApplicationContext(), R.string.hello_world, Toast.LENGTH_LONG).show();

And seems to work in both ways.

Thanks for help!

Was it helpful?

Solution

Toast.makeText(getApplicationContext(), R.string.hello_world, Toast.LENGTH_LONG)

calls

Toast.makeText(Context, int, int) and it is "translated" like

public static Toast makeText(Context context, int resId, int duration)  
                                throws Resources.NotFoundException {    
   return makeText(context, context.getResources().getText(resId), duration); 
}

all in all it is equal to your first makeText

OTHER TIPS

getResources().getText(R.string.hello_world) : will return String.. And

R.string.hello_world: will return integer (reference location of objects).

And makeToast() method is available for both parameters.

If you passed it string it treat is message.

if you passed it any integer it will treat it as a reference location of String and control will find that String. If No string will available with provided integer then it will throw exception. (ResourceNotFoundException)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top