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!
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
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
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With