Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8461137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:47:48+00:00 2026-06-10T13:47:48+00:00

Description I have an activity which displays the camera preview on a surface view.

  • 0

Description

I have an activity which displays the camera preview on a surface view. I do not want it to be restarted on orientation changes (as its constantly scanning for QR codes), so the orientation is locked in portrait.

The problem I have is that I need to display instructions on screen to the user at the bottom of the screen. Given the screen is locked in one orientation I do not get notified of the new orientation as the activity is not redrawn. Thus the instructions were rendered in the same place regardless of the phone been in portrait, reversed portrait, landscape or revered landscape.

To address this problem, I hooked up to the sensor manager to read in the orientation in realtime. (See code below) After reading it in I work out the orientation myself and move my instructions as required as each update is sent to me. This has worked for me so far until recently. When testing it on a Asus Transformer Prime and Xoom tablets. The tablet is returning the orientation in as “90” for portrait, instead of “0” like my other 2 phones. So as you can see in the code below, I check for tablets (large screens) and minus the extra 90 degrees.

Problem

The problem is the new Nexus 7 tablet does not have this extra bias. In portrait it returns “0”, but I detect it as a tablet, so minus 90 degree thus the result is 270 and it places my instructions as if the tablet was in landscape mode. I assume the reasoning behind this is the Xoom/Transformer was designed to be used in landscape, and the Nexus in portrait, but unless I know about all the devices affected this doesn’t help.

Question

Is there a reliable way to detect the orientation across all devices and return the results in “realtime”, as in the code below, but taking into accoutn the default orientation of the device (i.e. Phones + Nexus 7 are portrait, but most tablets are landscape)?

Current Code

    boolean large = ((getBaseContext().getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
    boolean xlarge = ((getBaseContext().getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);       
    isTablet = large || xlarge;
    myOrientationEventListener = new OrientationEventListener(getBaseContext(), SensorManager.SENSOR_DELAY_NORMAL)
    {

        @Override
        public void onOrientationChanged(int orientation)
        {
            if (orientation == -1)
            {
                return;
            }

            if (isTablet)
            {
                orientation += -90;
                if (orientation < 0) // keep the result between 0-360
                {
                    orientation += 360;
                }
            }

            // Work out the orientation

            if (orientation >= 60 && orientation <= 140)
            {
                screenOrientation = ScreenOrientation.Landscape_Reversed;
            }
            else if (orientation >= 140 && orientation <= 220)
            {
                screenOrientation = ScreenOrientation.Portrait_Reversed;
            }
            else if (orientation >= 220 && orientation <= 300)
            {
                screenOrientation = ScreenOrientation.Landscape;
            }
            else
            {
                screenOrientation = ScreenOrientation.Portrait;
            }

            //... Do stuff with new orientation here
        }
    };
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:47:50+00:00Added an answer on June 10, 2026 at 1:47 pm

    Fixed it

    Replaced the code

            if (isTablet)
            {
                orientation += -90;
                if (orientation < 0) // keep the result between 0-360
                {
                    orientation += 360;
                }
            }
    

    With the following

                //Check "normal" screen orientation and adjust accordingly
                int naturalOrientation = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
                if (naturalOrientation == Surface.ROTATION_0)
                {
                    Logging.d(TAG, "Rotation ROTATION_0");
                }
                else if (naturalOrientation == Surface.ROTATION_90)
                {
                    Logging.d(TAG, "Rotation ROTATION_90");
                    orientation += 90;
                }
                else if (naturalOrientation == Surface.ROTATION_180)
                {
                    Logging.d(TAG, "Rotation ROTATION_180");
                    orientation += 180;
                }
                else if (naturalOrientation == Surface.ROTATION_270)
                {
                    Logging.d(TAG, "Rotation ROTATION_270");
                    orientation += 270;
                }
    
                if (orientation > 360) // Check if we have gone too far forward with rotation adjustment, keep the result between 0-360
                {
                    orientation -= 360;
                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a view which displays Invoices and InvoiceLines . @model VectorCheck.ViewModels.InvoiceViewModel @{
I have a field 'Description' which can have product descriptions with any unicode characters.
I have the following two tables activity(activity_id, title, description, group_id) statistic(statistic_id, activity_id, date, user_id,
I have one Activity in which I try to show some simple messages, and
In my application, I have a food activity in which the user enters his/her
I have an Activity which provides UI for editing entity properties such as name,
I have an activity which shows 3 random images from JSON data off the
I have a couple of tables which are used to log user activity for
I have a JSON file which is populated to an activity (Main.java). This Activity
I have an activity which contains 2 textviews and an imageview. First textview contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.