Karthik Andhamil
Karthik Andhamil

Reputation: 856

Android 2.3.3 & 4.1.2 android:configChanges for detecting Orientation change

I am building an app with Android 2.3.3 API. I need to recognize the change in orientation and perform some action. So I added the following in Android Manifest,

android:configChanges="orientation|keyboardHidden

And I override the method

public void onConfigurationChanged(Configuration newConfig)

It works perfect on Android 2.3.3. But when I install the same app (built with 2.3.3 API) on a 4.1.2, onConfigurationChanged() is not invoked. I searched online for solution and people suggest to add the following in android manifest.

 android:configChanges="orientation|keyboardHidden|screenSize"

If I build the app with above statement and built with 4.1.2 API, It works perfect on 4.1.2 device. But I cannot install it on 2.3.3. 2.3.3 API doesn't have "screenSize" option. So to support both, what should I do?

Thanks, Karthik

Upvotes: 1

Views: 3983

Answers (4)

Bhavdip Sagar
Bhavdip Sagar

Reputation: 1971

FYI Look Below code that i did used :

    @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
        Toast.makeText(getBaseContext(),"On Config Change LANDSCAPE", Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(getBaseContext(),"On Config Change PORTRAIT", Toast.LENGTH_LONG).show();
    }
}

Upvotes: 1

Bhavdip Sagar
Bhavdip Sagar

Reputation: 1971

Hi Look at mine AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="10" 
    android:maxSdkVersion="16"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity 
        android:name=".ConfigrationTask"
        android:configChanges="orientation|keyboardHidden"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>

    </activity>
</application>

Upvotes: 2

Bhavdip Sagar
Bhavdip Sagar

Reputation: 1971

Please note this point in your case : config changes The screen orientation has changed — the user has rotated the device. Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.

Thank you

Upvotes: 0

iagreen
iagreen

Reputation: 31996

Just because you compile against, or target, a given API level does not mean you cannot explicitly support lower API levels. Try adding something like the following to your manifest. I took the levels from the versions mentioned in your question.

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="16"/>

Upvotes: 1

Related Questions