Reputation: 24476
I want to run a Hello World sample application only in landscape mode on my Device.
If the device changed to portrait, I would then like to raise one toaster. For example "please change to landscape mode to run your application".
How should I do this?
Upvotes: 18
Views: 30754
Reputation: 17247
You may try something like this
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Log.v("log_tag", "display width is "+ width);
Log.v("log_tag", "display height is "+ height);
if(width<height){
Toast.makeText(getApplicationContext(),"Device is in portrait mode",Toast.LENGTH_LONG ).show();
} else {
Toast.makeText(getApplicationContext(),"Device is in landscape mode",Toast.LENGTH_LONG ).show();
}
Upvotes: 1
Reputation: 18489
You can go for both programmatically as follows:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
or also in manifest file you can give here as
<activity android:name=".YourActivity" android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation"/>
Upvotes: 48
Reputation: 25755
You can use the configChanges
-attribute for the Activity you want to catch the screen-orientation change for.
After that, the onConfigurationChanged()
-method in your Activity will be called when the orientation changes.
To check which orientation is currently displayed, check this older post: Check orientation on Android phone
After that, you can show off your message or do whatever you like (of course you can also set it to only show in landscape).
Upvotes: 0
Reputation: 163
in your Manifestfile(xml)
<activity android:name=".ActivityName" android:screenOrientation="landscape">
</activity>
refer this also Is that possible to check was onCreate called because of orientation change?
Upvotes: 3
Reputation: 24021
add this to your activity tag in your manifest file:
android:screenOrientation="landscape"
It will fix your orientation to landscape mode and you need not to show any toast to user.
Upvotes: 8