Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

android - orientation is not working in 2.3.3?

I have prepared one application.My application will be supports both landscape and portrait. When i change the orientation from portrait to landscape it is working fine,But when i change landscape to portrait it is not working.my code is,

  public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
        System.out.println("orientation---"+getResources().getConfiguration().orientation);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setContentView(R.layout.main);
            System.out.println("landscape-----");
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.main);
            System.out.println("portrait-----");
        }

    }

Every time time it taking only landscape mode.I made mistake some where. please help.

System.out.println("orientation---"+getResources().getConfiguration().orientation);

The above SOP is printing 2 means landscape.Please help me.(i am using 2.3.3)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The issue is with the android:configChanges="orientation|keyboardHidden" .

if you remove |keyboardHidden from the androidmanifest.xml, then the onConfigurationChanged is only fired when you rotate from landscape to portrait, not when you go from portrait to landscape (at least in the default emulator).

Hope this helps.

EDIT: DID some sample program to test it and works fine for me.

CheckOrientationActivity.java

import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class CheckOrientationActivity extends Activity {


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
        Log.i("DayElevenActivity", "onCreate Start");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.i("DayElevenActivity", "onConfigurationChanged");
        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.i("ORIENTATION_LANDSCAPE", "ORIENTATION_LANDSCAPE");
            Toast.makeText(getBaseContext(), "ORIENTATION_LANDSCAPE", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            Log.i("ORIENTATION_PORTRAIT", "ORIENTATION_PORTRAIT");
            Toast.makeText(getBaseContext(), "ORIENTATION_PORTRAIT", Toast.LENGTH_SHORT).show();
        }
    }

}

In AndroidManifest.xml

<activity
            android:label="@string/app_name"   
            android:configChanges="**orientation|keyboardHidden**"    
            android:name=".CheckOrientationActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Corresponding Logs when i run and change the orientation

03-05 18:54:31.644: I/CheckOrientationActivity (20314): onCreate Start
03-05 18:54:35.014: I/CheckOrientationActivity (20314): onConfigurationChanged
03-05 18:54:35.014: I/ORIENTATION_LANDSCAPE(20314): ORIENTATION_LANDSCAPE
03-05 18:54:41.984: I/CheckOrientationActivity (20314): onConfigurationChanged
03-05 18:54:41.984: I/ORIENTATION_PORTRAIT(20314): ORIENTATION_PORTRAIT

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...