Reputation: 5975
I'm trying to dynamically change the UI based on whether or not the device is less than 5 inches and is in landscape mode. I don't want to use XML layouts if possible to do this, but it's the only way that's working so far. Is it possible to do what I'm trying to do?
Upvotes: 0
Views: 45
Reputation: 10712
If you insist using code to determine the device dimensions an orientation you could use the resource configuration class that exposes various device configuration variables.
In your case here are the values you need:
getResources().getConfiguration().orientation
getResources().getConfiguration().screenWidthDp
getResources().getConfiguration().screenHeightDp
please note that it is highly recommended to use the provided folder naming conventions to change the app UI according to device orientation. If you incorporate fragments as well then you could achieve simpler more maintainable code.
Upvotes: 1
Reputation: 2313
Your best bet is to take advantage of the configuration qualifiers built into android. You can find more information about them here:
http://developer.android.com/guide/practices/screens_support.html
If you scroll down to the Using configuration qualifiers section, you will see a list of folder extensions you can use. These files in these folders overwrite sections of the matching files in folders without the extension.
IE.
A boolean could be set in dimens.xml in the values-land-small folder that will overwrite the corresponding boolean in the dimens.xml file stored in the values folder. You can then use the value of this boolean in code to target specific devices and specific configurations without having multiple xml layouts directly.
Upvotes: 0