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?
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"/>
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.
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?
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();
}
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).
精彩评论