I've been having trouble with this issue for some time now and I'm looking for some help. I'm a beginner, so I apologize if my terminology is not correct. I'll try to respond as much as I can.
I'm developing an Android application that links to a friend's website where he streams various videos. My application is very basic in that I want it to open with one layout and when you click a button to open up one of the streams, it opens to a different layout. I also want the layouts to change depending on the screen's orientation. I have gone ahead and created 4 different XML files in the app's layout folder. I have the main screen portrait layout file, main screen landscape layout file, stream screen portrait file, and stream screen layout file.
My problem is that I'm not exactly sure how I'd go about switching between 4 different layout files. The following code is my function for how I feel like I should go about switching to different views:
public void screenOrientation()
{
int rotation = ((WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getRotation();
LinearLayout mainPortrait = (LinearLayout) view.findViewById(R.layout.main);
LinearLayout mainLandscape = (LinearLayout) view.findViewById(R.layout.mainlandscape);
LinearLayout streamPortrait = (LinearLayout) view.findViewById(R.layout.streaminappportrait);
LinearLayout streamLandscape = (LinearLayout) view.findViewById(R.layout.streaminapplandscape);
if (Global.appShowingMain == true && Global.appShowingStream == false)
{
switch (rotation)
{
case 0:
mainPortrait.setVisibility(View.VISIBLE);
break;
case 90:
mainLandscape.setVisibility(View.VISIBLE);
break;
case 180:
mainPortrait.setVisibility(View.VISIBLE);
break;
case 270:
mainLandscape.setVisibility(View.VISIBLE);
break;
default:
mainPortrait.setVisibility(View.VISIBLE);
break;
}
}
else if (Global.appShowingStream == true && Global.appShowingMain == false)
{
switch (rotation)
{
case 0:
streamPortrait.setVisibility(View.VISIBLE);
break;
case 90:
streamLandscape.setVisibility(View.VISIBLE);
break;
case 180:
streamPortrait.setVisibility(View.VISIBLE);
break;
case 270:
streamLandscape.setVisibility(View.VISIBLE);
break;
default:
streamPortrait.setVisibility(View.VISIBLE);
break;
}
}
}
I'm really stuck as to how I can go ahead and switch among these different layouts based upon the screen's orientation. Can anybody help?
Edit: As per my conversation going on below, I have changed the code in question to the following:
public void screenOrientation() { int rotation = ((WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getRotation();
if (Global.appShowingMain == true && Global.appShowingStream == false)
{
switch (rotation)
{
case 0:
setContentView(R.layout.main);
break;
case 90:
setContentView(R.layout.mainlandscape);
break;
case 180:
setContentView(R.layout.main);
break;
case 270:
setContentView(R.layout.mainlandscape);
break;
default:
setContentView(R.layout.main);
break;
}
}
else if (Global.appShowingStream == true && Global.appShowingMain == false)
{
switch (rotation)
{
case 0:
setContentView(R.layout.streaminappportrait);
break;
case 90:
setContentView(R.layout.streaminapplandscape);
break;
case 180:
setContentView(R.layout.streaminappportrait);
break;
case 270:
setContentView(R.layout.streaminapplandscape);
break;
default:
setContentView(R.layout.streaminappportrait);
break;
}
}
}
What's happening is that whether you open the app in landscape or portrait mode, the main portrait layout is only showing. When you change the orientation, that portrait layout is the only one visible. When you attempt to go to the stream, whether you're in landscape or portrait, the portrait version of the stream layout opens. When you've got that stream layout open and the screen orientation changes, it takes you back to the main menu portrait orientation.