I've search everywhere and can't find any way of doing this at all:
In my project, I have two resources folders for layouts: One is called "layout", and the other is called "layout-land". This is the familiar way of having two separate layouts for each Activity that get used depending on the current orientation. The issue is that I don't have separate "layout-land" layouts for every Activity, and not all Activities even need a different layout for landscape mode.
What I've done is overridden the default onConfigurationChanged
to prevent orientation changes from happening (with the appropriate configChanges="orientation"
in the manifest). This saved me a lot of headaches stuffing things into Bundles every time the user tilted their screen. However, in some cases, I actually want orientation changes to happen. Whenever there's a corresponding layout in the "layout-land" folder, I want the orientation to change as it normally would. Whenever there isn't, I want onConfigurationChanged
to suppress an orientation change event.
I made a parent BaseActivity
class that extends Activity
and is the base class for all of my activities, and overrode onConfigurationChanged
:
@Override
public void onConfigurationChanged(android.content.res.Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if(...there is a layout-land version of the current activity's layout...)
{
setRequestedOrientation(newConfig.orientation);
}
else
{
// Do nothing, since raising the orientation change event
// to change from a portrait layout to the same portrait layout is silly.
}
}
The only snag is that I can't find any way of determining if there's a layout-land version of my layout resource. I already know the resource id of the current layout (obtained elsewhere in my BaseActivity
class by overriding setContentView
), but both the landscape version of the layout and the portrait version share the same id, and I see no easy way for the Resources
object to tell me which version of the layout it gives me when I ask it for a specific resource by id. There's got to be a way to do this. What am I missing?