Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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....

like image 794
Libin Avatar asked Dec 04 '13 21:12

Libin


2 Answers

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

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

like image 122
Mike Avatar answered Sep 28 '22 23:09

Mike


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;
}
like image 21
Sipka Avatar answered Sep 28 '22 23:09

Sipka