The answer to this question may be too advanced for me, but I would still like to give it a try. I am able to determine and set the value for the screen size within a method, but it does not have scope outside of that method. My question is, how do I give it scope inside the entire class? Also inside the entire project? Thanks. Here is what I am using to determine the screen size, but it only works inside a single method:
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Thanks!
If you only want to worry about setting those values once, extending the
Applicationclass would probably be a good start.To access the getter methods, set the
android:nameattribute on the application tag in your project’s manifest:You can then cast the result of any
getApplication()andgetApplicationContext()call toMyApplicationand call the methods; e.g.Do note that since the width and height values are initialized only once, they will not change on screen orientation changes. If you want to take that into account too, a simple util method accepting a context instance (can be e.g. an
ActivityorService) is probably a more straightforward solution.