Question

I saw some related questions on SO, but I'm not able to resolve my issue. I've created separate layout for PORTRAIT and LANDSCAPE mode.

I've modified the AndroidManifest.xml file for corresponding orientation change but onConfigurationChanged() is not working when I'm implementing it in activity.

Now the issue is at layout.addView(graphView, lp); in onCreate(). I've written absolute hard-coded value in GraphView class.

So it works perfectly foe PORTRAIT mode but graphView is not correctly placed when I switch to LANDSCAPE mode.

To resolve this I've created GraphViewLand class which is coded exclusively for LANDSCAPE mode. But it is not getting called from onConfigurationChanged().

Rest of the layout is coming perfectly as I've created separate main.xml file for each orientation. But since graphView is created programmatically, it is not placed properly.

Am I doing anything wrong here?

I just read this from here:

For any types of configuration changes you say that you handle there, you will receive a call to your current activity's onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration) will not be called.

onCreate() method

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    init();

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.MainLayout);

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.ALIGN_TOP, btnindex.getId());

    GraphView graphView = new GraphView(this, values, "CurrentGraph",
                horlabels, verlabels);
    layout.addView(graphView, lp);
    }

onConfigurationChanged() method

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

Log.v("LAND", "SCPAE");
    }
}

Any help appriciated :)

Était-ce utile?

La solution

If you create two different types of view (for landscape and portrait) in xml and you have to write logic on different types of view then donot use android:configChanges in manifest.

If you have no android:configChanges in manifest and you have different sets of layout for landscape and portrait then when you change orientation your control will come to onCreate() inside that method you can write your logic.

Autres conseils

Did you code onConfigurationChanged as an override in your Activity class? Basic override:

// Called on rotation. 
    // Does not call onDestroy anymore due to android:configChanges="keyboardHidden|orientation" in manifest
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
    }

My manifest declaration for info:

<activity android:name=".Slime"
                  android:label="@string/app_name"
                  android:configChanges="keyboardHidden|orientation"
                  android:screenOrientation="landscape">

How do you say that the onConfigurationChanged() method is not called? Is the log not being printed? Isn't the toast being shown?

Secondly, you mention that the issue is at layout.addView in onCreate(). Do note that if android:configChanges is present in your manifest, then onCreate() is not called on orientation change. You need to move the piece of code that programmatically modifies the UI layout into the onConfigurationChanged() method.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top