Wednesday, March 9, 2011

Get Current Screen Orientation in ANDROID

AIM:  Get Current Screen Orientation in ANDROID
Solution: 
The following method does the required functionality.


public int getScreenOrientation() {


// Query what the orientation currently really is.
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)                {
   // The following message is only displayed once.
    return 1; // Portrait Mode

}else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
   // The following message is only displayed once.
    return 2;   // Landscape mode
}
return 0;
}


This is simple and effective.

Please let me know if any.

9 comments:

  1. wow, this is a good snippet :)personally I like to return portrait or landscape but I guess that's a bit more byte lol

    ReplyDelete
  2. Great method thanks,
    I think its also a good idea to check if the orientation is undefined eg;
    if(orientation==Configuration.ORIENTATION_UNDEFINED)
    {
    Configuration config = getResources().getConfiguration();
    orientation = config.orientation;
    // if(orientation==Configuration.ORIENTATION_UNDEFINED)
    // {
    //if height and width of screen are equal then
    // it is square orientation
    if(getOrient.getWidth()==getOrient.getHeight())
    { orientation = Configuration.ORIENTATION_SQUARE;
    }//if width is less than height than it is portrait
    if(getOrient.getWidth() < getOrient.getHeight())

    orientation = Configuration.ORIENTATION_PORTRAIT;
    }
    else
    { // if it is not any of the above it will defineitly be landscape
    Log.d("DEBUG","HERE - LANDSCAPE");
    orientation = Configuration.ORIENTATION_LANDSCAPE;
    }
    // }
    }

    ReplyDelete
  3. not working FORCED TO CLOSE

    ReplyDelete
  4. how to get orientation for SCREEN_ORIENTATION_REVERSE_LANDSCAPE and SCREEN_ORIENTATION_REVERSE_PORTRAIT in that case it is sending 2 and 3 but value which does not match with the value of above mentioned orientation for reverse portrait it is 9 and reverse landscape it is 8

    ReplyDelete
  5. Ϻanаgіng bοth busіness
    halves is іmpoгtаnt. Firѕt and forеmost, уou haνe to
    be prepared I wаs а misfit, сonfesses
    Jаson, 39, an evеnts оrganiser, handling partу arrangementѕ.



    Look at my web blog ... http://www.textualscholarship.org/

    ReplyDelete
  6. Based on the sегvicеs offered by thе company.
    Besides, a mоre direct approach to Seaгсh Engine Ranking Tool - conveгt the quick searchеr into a quiсk clіcker.
    Knοw the AuԁienceWhat type
    of readers would benefit from reading a person's article? Among thousands of search engine ranking tool companies is increasing day by day. Are you interested in increasing your profit is a difficult choice. Effectively use search engine ranking tool to market you site.

    Feel free to visit my weblog: the paleolithic diet food list

    ReplyDelete
  7. Not allowed in paleo diet - Flax Meal Has a nutty taste and ground from flax seeds.



    Feel free to visit my web site; paleo diet menu plan

    ReplyDelete
  8. Believe this, no one can learn all the skills
    sets that I didn't, and eventually reach a conclusion. This is why you may want to screen both pet and owner. The team player understands that it takes a lot of ways to advertise a spamdexing for free - forum posting, social networking, blogging, etc. Strong strategies help to catch attention and present your thoughts in a more effective and efficient customer service model. You can obtain plans with instructions on how to get there, and I am satisfied with the result.

    Here is my webpage - Seo optimisation tips

    ReplyDelete

Android Developers Blog

Ram's shared items