Android - Difference between View.getResources() and View.getContext().getResources()

Viewed 6381

I wanted to understand the real difference in using View.getResources() and View.getContext().getResources().

For example, I have to set a color to a TextView from resource..

view.setTextColor(view.getResources().getColor(R.color.Blue));

or

view.setTextColor(view.getContext().getResources().getColor(R.color.Blue));

Both works , but as per the documents...

View.getResources() - Returns the resources associated with this view.

View.getContext() - Returns the context the view is running in, through which it can access the current theme, resources, etc.

Your thoughts are welcome....

3 Answers

Nothing. As seen in the source code:

private final Resources mResources;

public View(Context context) {
    mContext = context;
    mResources = context != null ? context.getResources() : null;
    ...
}

public Resources getResources() {
    return mResources;
}

I would guess the View.getResources() is a shortcut for getContext().getResources().

You could search the source code if you really want to know.

getContext().getResources() returns resources of the contest in which View is running i.e. this can be used when views are implemented dynamically while View.getResource(). Return a Resources instance for your application's package.

Related