The second approach is more elegant, since inside the TextView
(or any View
class) it will perform the task of obtaining a string for the specified resource.
Providing components to internal work is always preferable. In addition, it is shorter and more readable.
About the internals that I talked about: if you look at the source code of Androids, you can see that the setText(int)
method of TextView
is executed as follows:
public final void setText(int resid) { setText(getContext().getResources().getText(resid)); }
So, it internally uses the Context
class to get the string from the resource identifier. Now, if you look at the getText()
method (which also comes from the Context
class), you can see that it is implemented the same way :
public final String getString(int resId) { return getResources().getString(resId); }
Thus, for reasons of performance or reliability, this does not matter. However, it is shorter and more readable.
source share