0

I have a linear layout wrapping a button, a gridview and a textview set in a xml file. When the screen is in portrait orientation all these elements are displayed fine. However, when I change the orientation of the screen into landscape orientation the textview disappears. I'd like to change the layout on configuration change. I'd like to display the elements horizontally in a way that each of them takes up a certain percentage of the screen. I also have to change the number of items shown per row in the gridview, and maybe some others parameters. Until now I have this, but i don't know how to change the rest of the parameters.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    LinearLayout linearlayout= (LinearLayout) findViewById(R.id.LinearLayout02);
    GridView gridview=(GridView) findViewById(R.id.gridview);
    if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){           
       linearlayout.setOrientation(LinearLayout.HORIZONTAL);     
    }
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        linearlayout.setOrientation(LinearLayout.VERTICAL);         
    }
}
TronicZomB
  • 8,667
  • 7
  • 35
  • 50
user2071976
  • 51
  • 1
  • 8
  • what parameters do you need to change? – Blackbelt Mar 27 '13 at 14:07
  • I'm not sure yet, i may have to use a relativelayout instead of a linear layout... the point is that I need the gridview on the left, and the textview on the right. The button would be on the upper right side. – user2071976 Mar 27 '13 at 14:17

1 Answers1

3

You'll want to create a folder in your /res/ directory that is titled layout-land and then create a new layout xml in that folder that is the same name as your other layout and place the items (TextView, etc.) where you would like them when the device is in landscape and from there Android will take care of the rest.

Edit: Use the following code:

@Override
public void onConfigurationChanged(Configuration newConfig) {
     super.onConfigurationChanged(newConfig);
     setContentView(R.layout.my_main_layout);

     //save your text from textviews, edittexts, etc. in temp variables

     InitializeUI(); //take your findViewById stuff out of onCreate and put it in its own method that can be called here as well.

     //set you textviews, etc back to previous values from temp vars

 }

That should do it.

TronicZomB
  • 8,667
  • 7
  • 35
  • 50
  • the thing is that i don't want my activity to be recreated on configurationchanged, so I modified the AndroidManifest.xml in order to avoid this. And since my activity is not recreated on configuration changed, placing the xml in the layout-land folder doesn't work for me. – user2071976 Mar 27 '13 at 14:08
  • Ok, so you have ` android:configChanges="keyboardHidden|orientation|screenSize"` in your manifest? Create the folder and file that I mentioned anyways and I will update my answer with how to deal with `onConfigurationChanged`. – TronicZomB Mar 27 '13 at 14:11
  • yes i have this in my manifest. Okay, i have changed it but the app doesn't apply the view in layout-land. And I dont want to reset a new layout at runtime, because i'll lose the current state of the views... i just want to place the elements somewhere else – user2071976 Mar 27 '13 at 14:23
  • 1
    What sort of state infromation do you not want to lose? This method does not call the onDestroy/onCreate that a normal rotation does so all that you end up losing is layout info that can be stored in temp variables. And this should apply the layout in the landscape folder. Did you change the name of the layout xml and/or did you reposition the items in the xml as you wanted? – TronicZomB Mar 27 '13 at 14:27
  • 1
    what should go in InitializeUI()? `gridview = (GridView) findViewById(R.id.gridview);` for example? – user2071976 Mar 27 '13 at 14:32
  • I have the `(Button/TextView/EditText/etc.) findViewById(R.id.my_names);` as well as the `.setOnClickListener(myOnClickListenerNames);` within the `InitializeUI()`. Also make sure to call `InitializeUI()` in both the onConfigurationChanged and your onCreate. – TronicZomB Mar 27 '13 at 14:35
  • Yes the `gridview` is one example that would go in there. – TronicZomB Mar 27 '13 at 14:36
  • should I also set the content view again `setContentView(R.layout.activity_show);`? – user2071976 Mar 27 '13 at 14:36
  • Yes and it will pull the layout from the approriate folder (layout or layout-land) and display that view. – TronicZomB Mar 27 '13 at 14:40