I am using the following function in one of my activities of my app:
public static float convertDpToPixel(float dp, Context context){
return dp * ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}
What context should I use?
Application Context: tied to the Lifecycle of an Application
Activity Context: tied to the life cycle of activity
Base Context: the base context as set by the constructor or setBaseContext
I have done it like this and it seems to work:
In my_activity.java:
float conversion = convertDpToPixel(10, this);
But also I could do the following:
float conversion = convertDpToPixel(10, getApplicationContext());
And also the following:
float conversion = convertDpToPixel(10, getBaseContext());
I think that in this specific case it doesn't really matter as it will get the resources of the app and it is included in all the contextes types but I would like to ensure what I am saying. Thanks