0

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.

Patr3xion
  • 122
  • 2
  • 15
  • Have you tried doing setContentView(R.layout.desired_resource); ? – Nathaniel D. Waggoner Feb 10 '14 at 21:24
  • At one point, all those setVisibility lines of code was were setContentView lines for the respective layout files that corresponded to them, but the app was crashing when I had all of those. – Patr3xion Feb 10 '14 at 21:36
  • Assuming we keep it the way you have it, are all of these views in the same heiarchy? Where are they contained? I only see you showing views, not hiding them. Is it possible that the top most view is always visible, thus obscuring the underlying views? – Nathaniel D. Waggoner Feb 10 '14 at 21:38
  • Also - what is the actual issue with this implementation? Crashes? Log spam? Not showing views? – Nathaniel D. Waggoner Feb 10 '14 at 21:38
  • I wasn't aware I had to hide views. I assumed that making a view visible automatically made all other views invisible. Edit: I'll change the code back to the setContentView situation I had before and I'll let you know. – Patr3xion Feb 10 '14 at 21:39
  • Is this something you could make a github for? I suspect you have issues with the way you've structured things which are making this overly complicated. Have a look at: http://developer.android.com/reference/android/app/Activity.html#setContentView(android.view.View) I think you may be confused about the way view heirachies work and it may be worth your time to read on these for awhile before continuing. – Nathaniel D. Waggoner Feb 10 '14 at 21:44
  • Please post the logs from the setContentView method if it crashes. I think* i know whats up, but I'll need to see a log to tell you for sure. – Nathaniel D. Waggoner Feb 10 '14 at 21:47
  • Ok, I went ahead and changed all those back to the setContentViews that corresponded to each screen rotation. The app isn't crashing, but I'm not getting the results that I thought I had programmed. Also, I'm not sure how to create a GitHub and I'm not entirely sure I feel comfortable doing so because I don't want to give away my friend's website. – Patr3xion Feb 10 '14 at 21:48
  • Ah yea, def don't do a git if it's not your code. What results are you seeing now? Edit your question and add the new code (leave the old stuff). – Nathaniel D. Waggoner Feb 10 '14 at 21:49
  • I added the edit above and explained what's happening. – Patr3xion Feb 10 '14 at 21:54
  • Is it the default block or the case(0) block which is executed? – Nathaniel D. Waggoner Feb 10 '14 at 22:43
  • How would I be able to figure that out? – Patr3xion Feb 10 '14 at 22:58
  • Alright so I went ahead and took out those default cases and just left them blank. Now whenever the app is in landscape mode in either of the layout groups (either the main screen or the stream screen), it crashes. The log is showing that it's crashing at a line for a button's setOnClickListener because of a NullPointerException. – Patr3xion Feb 10 '14 at 23:23
  • Ok so you're always executing the Default block - this means that your rotation value isn't what you expect it to be. Do you understand how to use the eclipse debugger? Post the logcat for the NPE. – Nathaniel D. Waggoner Feb 10 '14 at 23:32
  • I do not use Eclipse. I use IntelliJ. – Patr3xion Feb 10 '14 at 23:42
  • Thats fine, use the intellij debugger. – Nathaniel D. Waggoner Feb 10 '14 at 23:43
  • I'm getting this: Caused by: java.lang.NullPointerException at com.example.*****.MyActivity.appBody(MyActivity.java:112) and that line 112 is as follows: btnHello.setOnClickListener(new View.OnClickListener() – Patr3xion Feb 11 '14 at 02:08

2 Answers2

0

You can do that programatically, this way:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
  // Do the changes within the portrait orientation
  ...
}
else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
  // Do the changes within the landscape orientation
  ...
}

This assuming you're doing that within an Activity or derivated. If not, you'll have to provide a Context instance to the getResources() method:

mycontext.getResources()...

---- EDIT ----

  • getResources(): It's a method within the Context class. That means that if you run this method within a class that extends another class that a Context defined (such as Activity), you won't need to provide anything else. You can read more on Contexts here. You'll know if you're extending an Activity having a look at your class definition, it should be something like this:

    public class MyActivity extends Activity { ... }
    

So basically, this method will return you a Resources object with all your resources. You can read more on it here. But you don't need all the resources so let's give another step.

  • getConfiguration(): The getResources() method returns a Resources instance. This class provides a getConfiguration() method which returns a Configuration instance (more on this here), which contains lots of configuration parameters which you can use. One of them is the orientation, so you have two constants for this, ORIENTATION_PORTRAIT and ORIENTATION_LANDSCAPE. More on this here.

However, if you're within a class that is static, i.e., doesn't have a Context, you'll have to explicitely call getResources() over a previously saved instance of a Context. For example, saving it in the onCreate() method as mycontext = this;. This may lead to memory leaks though, so the best way to handle this (just if you need to) is explained in this example.

Community
  • 1
  • 1
nKn
  • 13,691
  • 9
  • 45
  • 62
  • I'm sorry, but I'm rather new to Android programming. Is there any way you can simplify your answer a bit? – Patr3xion Feb 10 '14 at 21:29
0

All you need to do is to put two layouts one for portrait and one for landscape. The portrait in the layout folder and the landscape in layout-land folder.

youssefhassan
  • 1,067
  • 2
  • 11
  • 17
  • I've tried that, but that only seems to work for one set of screens. I have two sets of screens that I need to switch between. – Patr3xion Feb 10 '14 at 21:34