Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between getResources(R.string.hello_world) and R.string.hello_world

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!

like image 365
Federico Ponzi Avatar asked Mar 30 '14 14:03

Federico Ponzi


2 Answers

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

like image 157
Blackbelt Avatar answered Nov 01 '22 00:11

Blackbelt


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)

like image 32
Lavekush Agrawal Avatar answered Oct 31 '22 22:10

Lavekush Agrawal