Cole
Cole

Reputation: 2815

Changing layouts when the orientation changes error

I'm trying to get my app to change layouts when the orientation changes. This is the code I have:

package com.wish.list;

import com.wish.list.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.OrientationEventListener;
import android.view.WindowManager;
import android.widget.ListView;

public class ListOfLists extends Activity{

 public void onCreate(Bundle savedInstanceState) {


     // Orientation Change starts here:
     private void setContentBasedOnLayout()
         WindowManager winMan = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);

         if (winMan != null)
         {
             int orientation = winMan.getDefaultDisplay().getOrientation();

             if (orientation == 0) {
                 // Portrait
                 setContentView(R.layout.listsportrait);
             }
             else if (orientation == 1) {
                 // Landscape
                 setContentView(R.layout.listslandscape);
             }            
         }

     }
     // End of Orientation Change

        ListView listsList = (ListView) findViewById(R.id.lists);
    }
    }
 }

On the line

public void onCreate(Bundle savedInstanceState) {

I get this error:

"Multiple markers at this line:

- Syntax error, insert "}" to complete MethodBody

- overrides android.app.Activity.onCreate"

Also, is there any way to change it so it doesn't have to restart the activity on Orientation change? I have it so Facebook checks the user's login details at the start of the app, and it has to recheck it every time the orientation changes.

EDIT:

So the code should look like this now?

package com.wish.list;

import com.wish.list.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.OrientationEventListener;
import android.view.WindowManager;
import android.widget.ListView;

public class ListOfLists extends Activity{

 public void onCreate(Bundle savedInstanceState) {
 }
 @Override
 public void onConfigurationChanged(Configuration newConfig) 
 {
     super.onConfigurationChanged(newConfig);

         // Checks the orientation of the screen
     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
     {
         setContentView(R.layout.listslandscape);
     } 
     else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
     {
         setContentView(R.layout.listsportrait);
     }
 }
     // Orientation Change starts here:
     private void setContentBasedOnLayout(){
         WindowManager winMan = (WindowManager)getBaseContext().getSystemService(Context.WINDOW_SERVICE);

         if (winMan != null)
         {
             int orientation = winMan.getDefaultDisplay().getOrientation();

             if (orientation == 0) {
                 // Portrait
                 setContentView(R.layout.listsportrait);
             }
             else if (orientation == 1) {
                 // Landscape
                 setContentView(R.layout.listslandscape);
             }            
         }


     // End of Orientation Change

        ListView listsList = (ListView) findViewById(R.id.lists);
 }     
}

Or do I delete the setContentBasedOnLayout section and replace it with the onConfigurationChanged code?

Upvotes: 0

Views: 5346

Answers (1)

dreamtale
dreamtale

Reputation: 2913

Insert "}" after the onCreate

If you don't want the activity restart when orientation changed,

1.Declare below in the manifest:

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

2.Override the onConfigurationChanged in the activity.

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

        // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    {
        setContentView(R.layout.listslandscape);
    } 
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        setContentView(R.layout.listsportrait);
    }
}

Edit

Yes, you should remove the setContentBasedOnLayout section. When the orientation changed, the onConfigurationChanged will be called instead of the onCreate.

Upvotes: 4

Related Questions