Is it possible to get device aspect ratio before launching any activity?
I am trying to setup the width based on whether my device is identified as ‘long’ or ‘not_long’. This should be available long before the activity is launched but I am not sure where I can get this variable. Activity is NOT available at the time this method is executed. Also in order to avoid memory leaks I do not want to pass activity into the class that contains the following method.
Here is the failing call method:
public int getDefaultWidth() {
Configuration myConfig = new Configuration();
myConfig.setToDefaults();
myConfig = getResources().getConfiguration();
switch (myConfig.screenLayout & Configuration.SCREENLAYOUT_LONG_MASK) {
case Configuration.SCREENLAYOUT_LONG_NO: {
return this._defaultWidth;
}
case Configuration.SCREENLAYOUT_LONG_YES: {
return this._defaultWidthLong;
}
case Configuration.SCREENLAYOUT_LONG_UNDEFINED: {
return this._defaultWidth;
}
}
return this._defaultWidth;
}
Your app’s Application context is created before any activities. An Application has a valid context, so you could use that to fetch whatever you need, set a static or member variable, and then reference that in your first activity.
An Application context can be handled by adding a new class that extends Application and making a small change in AndroidManifest.xml
Manifest:
Add the new class to your project that extends Application:
From your activity, simply do this to get width: