Rotate activity by 180 degrees in Kotlin on a Button click

538 Views Asked by At

I have an activity that I would like to be able to rotate 180 degrees. This is for a game for two people where if the players want to 'switch sides' they can hit a rotate button to go from portrait to reverse portrait and back..

I found this java code which looks like the right place. mainActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);

I would like it to work like in this manner but I'm not sure the best way to do this in Kotlin.

rotate_button.setOnClickListener{
            reverseOrientation(PORTRAIT or REVERSE_PORTRAIT)
        }

Any recommendations on how to implement? Thank you.

1

There are 1 best solutions below

0
jwright On

Setting the views 'rotation' in the XML via Kotlin is working well.

      val rotate_button = rotate_button as FloatingActionButton
      rotate_button.setOnClickListener{

            val constraint_view = constraint_view as ConstraintLayout

            if(constraint_view.rotation == 180.0F) {
                constraint_view.rotation = 0.0F
            } else {
                constraint_view.rotation = 180.0F
            }
        }