3

I have different layouts for portrait and landscape mode and I also need to override the onConfigurationChanged() callback. But problem is when I change the phone orientation to landscape my landscape layout does not work.

Can anybody tell me is this onConfigurationChanged call back problem or something else causing that?

Any help will be appreciative.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
sajjoo
  • 6,576
  • 20
  • 65
  • 86

2 Answers2

10

i also need to override the onConfigurationChanged() callback

Why?

but problem is when i change the phone orientation to landscape my landscape layout does not work.

I am going to guess that "does not work" means that the landscape layout does not take effect. This is expected behavior given what you have done.

To resolve this problem, ideally you delete android:configChanges="keyboardHidden|orientation". Putting in that attribute should be done as a last resort, and typically for activities that do not have separate portrait versus landscape layout files.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I have to override onCofigurationChanged call back because i have to set the bitmap size dynamically. – sajjoo Mar 17 '11 at 13:31
  • @sajjoo: You do not need to override `onConfigurationChanged()` to do that. You will be loading in the bitmap as part of setting up your UI in `onCreate()` -- "set the bitmap size dynamically" there. – CommonsWare Mar 17 '11 at 13:40
  • I was using app code from 3rd source, and it did have that very `android:configChanges` in Manifest. The glitch was that it worked OK (a layout was picked from layout-land folder) on Android 4.x devices/emulators, but on Android 5 the layout was not picked up, and default one for portrait was used. I cracked my head but finally spotted the matter with `android:configChanges` in Manifest, thanks to this post – Mixaz Apr 23 '16 at 21:11
2

I am sure that it will definitively help you...

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int ot = getResources().getConfiguration().orientation;
    switch (ot) {
    case Configuration.ORIENTATION_LANDSCAPE:
        setContentView(R.layout.main_land);
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        setContentView(R.layout.main);
        break;
    }
    Toast.makeText(this, "Helloo", Toast.LENGTH_SHORT).show();
}
enter code here
@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);

    int ot = getResources().getConfiguration().orientation;
    switch (ot) {
    case Configuration.ORIENTATION_LANDSCAPE:
        setContentView(R.layout.main_land);
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        setContentView(R.layout.main);
        break;
    }
}

@Override
public Object onRetainNonConfigurationInstance() {
    // TODO Auto-generated method stub
    return super.onRetainNonConfigurationInstance();
}

}

and add this line in your manifest file.. android:configChanges="keyboardHidden|orientation"

Deepak Sharma
  • 4,999
  • 5
  • 51
  • 61
  • So we should add the configuration code to both `onCreate` and to `onConfigurationChanged`? What is the reason for that if so? Thanks. – Azurespot Mar 31 '15 at 08:02