Reputation: 51
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);
}
}
Upvotes: 0
Views: 2816
Reputation: 8747
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.
Upvotes: 3