Reputation: 571
I'm new to Android\Java and I have following question: How can I check for screen orientation change?
Of course I have found many articles about this topic, however none of them worked for me so probably I'm doing something wrong.
One thing I don't understand is: Do I need to implement an OrientationEventListener ? Or is "onConfigurationChanged" sufficient ? Or do I need both ?
For example I've tried to run the folloing code that uses "onConfigurationChanged" - but onConfigurationChanged is never called. (I've add android:configChanges="orientation|screenSize|keyboardHidden" to manifest.xml and I build for API >13)
So my question is: Why is "onConfigurationChanged()" never executed ? I've tried it on a real device (Samsung Galaxy Note) and emulator.
public class main_activity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
@Override
public void onConfigurationChanged(Configuration newConfig) <--- this is never called
{
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test.andro" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".main_activity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
build.gradle:
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.test.andro"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
Upvotes: 0
Views: 1303
Reputation: 6179
Use this code where you want to check orientation in your activity:
protected void onResume(Bundle savedInstanceState)
{
super.onResume();
int rotation = getWindowManager().getDefaultDisplay().getRotation();
// DisplayMetrics dm = new DisplayMetrics();
// getWindowManager().getDefaultDisplay().getMetrics(dm);
if(rotation == 0)
text="Portrait";
else if(rotation == 1)
text="Rotated left side";
else
text="Rotated right side";
Toast toast = Toast.makeText(getApplicationContext(), text,Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER | Gravity.CENTER, 10, 0);
toast.show();
}
Upvotes: 0
Reputation: 145
try this
orientationListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_UI) {
public void onOrientationChanged(int orientation) {
if(canShow(orientation)){
show();
} else if(canDismiss(orientation)){
dismiss();
}
}
};
@Override
public void onResume(){
super.onResume();
orientationListener.enable();
}
@Override
public void onPause(){
super.onPause();
orientationListener.disable();
}
private boolean isLandscape(int orientation){
return orientation >= (90 - THRESHOLD) && orientation <= (90 + THRESHOLD);
}
private boolean isPortrait(int orientation){
return (orientation >= (360 - THRESHOLD) && orientation <= 360) || (orientation >= 0 && orientation <= THRESHOLD);
}
public boolean canShow(int orientation){
return !visible && isLandscape(orientation);
}
public boolean canDismiss(int orientation){
return visible && !dismissing && isPortrait(orientation);
}
Upvotes: 1