-1

I have been working on a radio app and along the way i decided to force only landscape to appear for tablets and portrait mode for phones. Here is what i have done so far to achieve that.

First i put a bool resource in res.values to check for screen size.

 //  From my res/values/bool.xml(sw600 and 720)dp.

        <?xml version="1.0" encoding="utf-8"?>
        <resources>
            <bool name="landscape_only">true</bool>
        </resources>

// res/values/bool.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="portrait_only">true</bool>
</resources>

I then forced the requested screen orientations with code by checking the boolean in Main Activity.

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // locking out landscape screen orientation for mobiles
        if(getResources().getBoolean(R.bool.portrait_only)){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        // locking out portait screen orientation for tablets
        if(getResources().getBoolean(R.bool.landscape_only)){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        }

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

    }
}

So far, this worked pretty well. Now here's the issue i have.

When i implemented my code to lock out portrait mode for tablets my app started in portrait for a few seconds before switching to landscape. How do i make sure that it doesn't start off first in portrait? It should start off in landscape.

benruty
  • 89
  • 1
  • 1
  • 7
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Selvin Aug 19 '17 at 19:23
  • Are you calling your code before setContentView()? – Juan Aug 19 '17 at 19:58

2 Answers2

1

Just add a null check before cancelling the notification.

 @Override
        protected void onDestroy() {
          if(radioService != null){
             radioService.cancelNotification();
        }
        super.onDestroy();
}

For changing the orientation correctly, you should add following to the activity tag in manifest -

<activity 
    android:name="com.ABC"
    android:configChanges="keyboardHidden|orientation|screenSize">
</activity>

And be sure to call setRequestedOrientation() before setting your layout i.e.

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // locking out landscape screen orientation for mobiles
        if(getResources().getBoolean(R.bool.portrait_only)){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        // locking out portait screen orientation for tablets
        if(getResources().getBoolean(R.bool.landscape_only)){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        }

        setContentView(R.layout.activity_main);
}
Sachin Aggarwal
  • 1,095
  • 8
  • 17
  • this removed the error but the app shows in portrait for a few seconds before switching to landscape. I don't want the activity starting off in portrait. How do i get round that? – benruty Aug 19 '17 at 19:41
  • Have you added this to your manifest file - android:configChanges="keyboardHidden|orientation|screenSize" inside your activity tag. Let me know if this doesn't work and please accept the answer if it does. – Sachin Aggarwal Aug 19 '17 at 19:55
  • i added that but it didn't solve the problem. Instead, it opened by landscape UI in portrait mode for some seconds before rendering properly – benruty Aug 19 '17 at 21:44
  • @benruty Can you post your complete code for onCreate() ? Thanks – Sachin Aggarwal Aug 19 '17 at 21:56
  • You should do the orientation change before setContentView(). I am updating my answer. Let me know if you still have a problem. – Sachin Aggarwal Aug 20 '17 at 06:54
  • it gave me same result. i just found out what i had been missing. you can check my answer above. thanks. i appreciate your effort. – benruty Aug 20 '17 at 14:02
0

I have finally found the solution to my problem. I was not giving a reference to the alternative screen views inside my value resource files. When i edited my bool.xml resource file as you see below, i got the expected result.

bool.xml (mobile)
 <?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="portrait_only">true</bool>
    <bool name="landscape_only">false</bool>
</resources>

// bool.xml (sw600dp)
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="portrait_only">false</bool>
    <bool name="landscape_only">true</bool>
</resources>

// bool.xml (sw720dp)
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="portrait_only">false</bool>
        <bool name="landscape_only">true</bool>
    </resources>

With this, my activities now start in the requested screen orientation as required.

benruty
  • 89
  • 1
  • 1
  • 7