1

I've got to do a check in my android app for orientation screen and after that to set the screen orientation to the next phase. I've run out of ideas how to do that avoiding this ugly else if block of code. Could you please give me a suggestion or something?

int currentOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; // Default orientation
.
.
.
switch (item.getItemId()) {
.
.
.
case R.id.change_orientation:
if (currentOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                    currentOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                } else if (currentOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                    currentOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                } else if (currentOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
                    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    currentOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                }
Stefan DOK
  • 61
  • 5

5 Answers5

1

Oops, I did not notice Android in your Question. I have no idea whether this works or not in current Android.


tl;dr

this.setRequestedOrientation( 
        switch ( currentOrientation )
                {
                    case SCREEN_ORIENTATION_LANDSCAPE -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                    case SCREEN_ORIENTATION_PORTRAIT -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                    case SCREEN_ORIENTATION_REVERSE_LANDSCAPE -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                }
);

Switch expressions

You can use switch expressions in Java 14 and later. The switch can return a value. See JEP 361: Switch Expressions.

Note that switch expressions are “exhaustive”, meaning the compiler tells you if not all of the enum values are covered by your case statements. And therefore no need for a default: case as would otherwise be indicated for defensive programming.

I noticed you call the same method in each of your if cases. So we can move that to a single method call at the end.

package work.basil.enums;

public class App2
{
    public static void main ( String[] args )
    {
        App2 app = new App2();
        app.demo();
    }

    enum ActivityInfo
    {
        SCREEN_ORIENTATION_PORTRAIT,
        SCREEN_ORIENTATION_LANDSCAPE,
        SCREEN_ORIENTATION_REVERSE_LANDSCAPE
    }

    private void demo ()
    {
        ActivityInfo currentOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;

        ActivityInfo activityInfo =
                switch ( currentOrientation )
                        {
                            case SCREEN_ORIENTATION_LANDSCAPE -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                            case SCREEN_ORIENTATION_PORTRAIT -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                            case SCREEN_ORIENTATION_REVERSE_LANDSCAPE -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                        };
        this.setRequestedOrientation( activityInfo );
    }

    private void setRequestedOrientation ( ActivityInfo activityInfo )
    {
        System.out.println( "Setting requested orientation to: " + activityInfo );
    }
}

Further changes to switch being previewed, in Java 17, by the way. See JEP 406: Pattern Matching for switch (Preview).

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Although I don’t understand Android very well, but you can use hashmap to achieve it.

//init a hashmap
Map<String, Map<Integer, Object>> configureMap = Maps.newHashMap();

//itemId means all values in item.getItemId()
configureMap.put("itemId", Maps.newHashMap());

//this can replace your if else in switch
configureMap.get("itemId").put(currentOrientation, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

......

Object orientation = configureMap.get(item.getItemId()).get(currentOrientation);
this.setRequestedOrientation(orientation);
currentOrientation = orientation;

You can also use ImmutableMap.of() to beautify the code.

Kaiux
  • 37
  • 1
  • 5
0

You can use a map that contains the next orientation value according to the current one:

Map<Integer,Integer> next = new HashMap<>(4);
next.put(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
...

switch (...) {

    case R.id.change_orientation:
        int nextOrient = next.get(currentOrientation);
        this.setRequestedOrientation(nextOrient); // Change this method so it also updates 'currentOrientation'
        break;

Note that it would be more efficient to store those next orientation values into an array, search for the index of the current one and then do a (i+1)%4 to get the index of the next one. I'll leave that to you.

Matthieu
  • 2,736
  • 4
  • 57
  • 87
-2

Use nested switch block; eg

switch(currentOrientation) {
  case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    currentOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

You can know more about switch here: https://www.w3schools.com/java/java_switch.asp

Current currentOrientation is of datatype int [https://developer.android.com/reference/android/content/pm/ActivityInfo#SCREEN_ORIENTATION_LANDSCAPE] others are also of data type int so comparison can be done, if it was of type Object, then we couldn't use Switch

Krish Yadav
  • 165
  • 11
-2

you can use switch and also use ternatory operator (?) in java. ? operator is used for only 2 conditions. like {condition ? true : false}. if you have more than one condition than you should go for switch.

Qadir Ali
  • 58
  • 8