Kanika
Kanika

Reputation: 10708

Locking Screen Orientation

I want to lock my screen orientation for Android tabs and for handsets too. I want to lock my phone screen orientation as Portrait and tabs screen orientation as Landscape.

When i searched the solution for the same,then I got the solution as in manifest file,.But If i will use it,then it work for both the same,but my requirement is to use different orientation for different device..Suggest me something..

Upvotes: 1

Views: 344

Answers (1)

Balaji Khadake
Balaji Khadake

Reputation: 3479

One each Activity you can take one check to know device type. In my app, I have used screen resolution as check; as tablets are having much more resolution than mobile devices:

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int height_pixel = dm.heightPixels;
        int width_pixel = dm.widthPixels;

After this you can take resolution bounds to set orientation for mobile and tablets. like

 if((height_pixel*width_pixel)>***your_resolution_check***){
      this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else{
      this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

This works fine for me.

Upvotes: 1

Related Questions