2

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 :)

GAMA
  • 5,958
  • 14
  • 79
  • 126

3 Answers3

5

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.

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • okay, so do I need to implement `onConfigurationChanged()` method? – GAMA Dec 16 '11 at 10:50
  • 1
    No you don't need too, but since the activity is destroyed and created you should save and restore application state properly to give the application context back to user. – Vince Dec 16 '11 at 11:08
  • With Bundle: http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state, With onRetainNonConfigurationInstance (never tried...) http://developer.android.com/guide/topics/resources/runtime-changes.html – Vince Dec 16 '11 at 11:19
  • Some code example in exercice 3 of notepad tuto (not dedicated to configuration changes): http://developer.android.com/resources/tutorials/notepad/notepad-ex3.html – Vince Dec 16 '11 at 11:22
  • @Vince but 1st link from former comment explains about saving variables like boolean,String.etc to retrieve. I want to retrieve the state. – GAMA Dec 16 '11 at 11:35
  • Yes the state of your application is the sum of all its data in memory, at the end it's a list of simple type variable. If your code have classes you can code a save and restore code inside each one (ie serialization/deserialization system) – Vince Dec 16 '11 at 12:35
1

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">
Vince
  • 1,036
  • 1
  • 10
  • 17
  • My application is always oriented as landscape. I've overriden the method to disable auto orientation. I had some code before in the overrided onConfigurationChanged but there is no more. I posted this because with only this 2 code addition it worked for me. – Vince Dec 16 '11 at 10:28
  • It can not, application is always run as landscape – Vince Dec 16 '11 at 10:31
  • I'm using too a library called cocos2d-android, and i call a utility method to force landscape (for safety...): https://github.com/ZhouWeikuan/cocos2d/blob/master/cocos2d-android/src/org/cocos2d/nodes/CCDirector.java#L278 theApp is Activity: http://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation(int) – Vince Dec 16 '11 at 10:35
  • What do you mean with that: If I write android:screenOrientation="landscape" in my manifest and then launch the app in portrait mode, it is working – Vince Dec 16 '11 at 10:38
  • The hard requirement you pointed before is just an hardware requirement for market filter. I can't remember for emulator i would have to try but don't have one here. I think it works nice in emulator. I keep looking. – Vince Dec 16 '11 at 10:44
1

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.

curioustechizen
  • 10,572
  • 10
  • 61
  • 110
  • yup exactly. Log is not getting printed and I'm not even getting the toast. But if I don't write code for adding graphView in `onCreate()`, how my `graphView` will be displayed? So I'm calling `graphView` in `onCreate()` and another class i.e. `graphViewLand` in `onConfigurationChanged()` but it's not getting called. – GAMA Dec 16 '11 at 10:43
  • If you are not even seeing the logs, then the second part of my answer is irrelevant. I suggest you look at Sunil Kumar Sahoo's answer to this question. That is more relevant to your case. – curioustechizen Dec 16 '11 at 11:02