Siddarth G
Siddarth G

Reputation: 779

How to prevent reverse-landscape orientation but allow landscape orientation in Android?

I have an Activity where i want the view to be on left side of the device(opposite side of volume/power button) in all cases.

I have no problem doing this in portrait and landscape orientation for the Activity, but when it goes to reverse-landscape and reverse-portrait the view will go to the right side of the device(volume/power button side).

Is it possible to allow screen orientation changes but block reverse-orientations?

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <CustomView
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        >
    </CustomView>

    <include
        layout="@layout/some_layout"
        >
    </include>
</LinearLayout>

activity_main.xml(land):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <CustomView
        android:layout_width="wrap_content"
        android:layout_height="150dp">
    </CustomView>

    <include layout="@layout/some_Other_layout" />

</LinearLayout>

I have tried orientationListener, whenever it goes to reverse-landscape i try to set orientation to landscape and then release the orientation by setting unspecified but that did not work as screen was getting locked and further changes in orientation where not getting detected.

Any ideas on how this can be solved?

Upvotes: 1

Views: 555

Answers (1)

Pratik Dodiya
Pratik Dodiya

Reputation: 2687

You can prevent the reverse landscape as below Kotlin code

requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

You can use below Java code

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Upvotes: 0

Related Questions