set screen orientation for Fragment Manager - Android - android

I want to rotate my app when the landscape mode is activated, and to be fixed in Portrait mode(like a normal app).
As I use a fragment manager, I decided to create an init method to programmatically decide to rotate or not my app instead of doing rotation for every fragments.
here's the method:
public class ActivityHelper {
public static void initialize(Activity activity) {
int orientation = activity.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
The problem is that the app always stays in Portrait mode.
Does someone have an idea? Thanks in advance

Related

Handling orientation inside fragment

I have a activity in which I load a fragment. Lets say I have two sections inside fragment layout(section A and B) , but while I switch to landscape then I have to show only section A . How will i know the orientation change inside fragment.
you can override onConfigurationChanged to be notified when the orientation changed add code below in your fragment class:
#Override public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
//landscape
}else{
//portrait
}
}
When the orientation of the screen is changed the Fragment will be destroyed and created again, so onDestroy() and onCreate() are called. in your onCreate() or onCreateView() (or wherever you need to) you can check for the current screen rotation with the following code:
int screenRotation = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
if(screenRotation == Surface.ROTATION_90 || screenRotation == Surface.ROTATION_270) {
//LANDSCAPE
} else {
//PORTRAIT
}
For this functionality android provides Sliding pane layout SlidingPaneLayout
and watch this tutorials also official google developers tutorial https://www.youtube.com/watch?v=Jl3-lzlzOJI&t=1007s

Rotate videoview while livestreaming

I have a videoview which play live streaming links fine in potrait mode with some channels listing below the videoview.
My problem is that i want to rotate my videoview to landscape mode (fullscreen and without showing the channel lists in the bottom) without buffering again or loading the stream again.
Thanks.
Add android:configChanges="orientation" to Activity tag on AndroidManifest.xml
it will stop the calling of onCreate() while rotation and prevent your video from rebuffering.
and use the following code in your activity.
//this is called when the screen rotates.
// (onCreate is no longer called when screen rotates due to manifest, see: android:configChanges)
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT)
{
Log.d("tag", "Portrait");//Do your layout changes here
}
else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Log.d("tag", "Landscape");//Do your layout changes here
}
}
Mark as up if it works for you.

Get orientation of the android phone screen

Is there some differences between getRequestedOrientation and getResources().getConfiguration() to get the orientation of the android phone screen ??
if you are in activity then use
getResources().getConfiguration().orientation
else if not in activity then use your activity instance by passing it,
your_activity_instance.getResources().getConfiguration().orientation
Another way:
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getOrientation();
if(myCurrentActivity.getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT)
{
// code to do for Portrait Mode
} else {
// code to do for Landscape Mode
}

How to detect the orientation mode while launching the app first

I am developing an app in which I need to provide different activity's background image on different orientation. So I've approached in this way:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setLanguage();
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// set background for landscape
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
// set background for portrait
}
}
And
android:configChanges="locale|orientation|screenSize"
and in onCreate() I set the background image for portrait assuming user will launch the app staying in portrait mode.
Everything works fine as when users change their mode, the corresponding background is set and so on.
But if a user starts this app when the phone is in landscape mode, as It is shown the portrait image at the first launch as I assumed before user will launch app in portrait mode.
So how can I solve this problem? In one sentence, what is the best way to set different background image for different orientation ? am I in a right track?
In onCreate of your activity you can check for the current orientation using the this
Configuration newConfig = getResources().getConfiguration();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// set background for landscape
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
// set background for portrait
}
In one sentence, what is the best way to set different background image for different orientation ?
Step #1: Delete everything you've done, most notably the whole android:configChanges stuff. Ignore the background image for now, and get the rest of your configuration change logic working.
Step #2: Create -land versions of the requisite resource directories, for whatever densities of this image that you have (e.g., res/drawable-land-hdpi/ to match your res/drawable-hdpi/)
Step #3: Move the landscape versions into the -land directories, naming them the same as their portrait equivalents (e.g., res/drawable-hdpi/background.png and res/drawable-land-hdpi/background.png)
Step #4: Just refer to common resource name in your android:background attribute (e.g., #drawable/background)
This way:
You stick to better configuration-change behavior, and
Android will give you the correct background at the correct time
You can check for orientation in onCreate
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int orientation = display.getRotation();
if (orientation == Surface.ROTATION_0 || orientation == Surface.ROTATION_180)
{
// Portrait
}
else
{
// landscape
}
check orientation changes after setContentView method
int orientation = getResources().getConfiguration().orientation;
if (orientation == 1){
utility.toast("portrait");
}else {
utility.toast("landscape");
}

Android determine screen orientation at runtime

Here's a pseudo code to detect screen rotate event, and decide to retain or changes the screen orientation.
public boolean onOrientationChanges(orientation) {
if(orientation == landscape)
if(settings.get("lock_orientation"))
return false; // Retain portrait mode
else
return true; // change to landscape mode
return true;
}
How do I make similar things in Android?
EDIT:
I'm actually looking answer on Where to handle orientation changes. I do not want to fix the orientation by adding screenOrientation="portrait".
I need something, similar to onConfigurationChanges(), where I can handle the orientation, but do no need me to manually redraw the view.
You need a Display instance firstly:
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
Then orientation may be called like this:
int orientation = display.getOrientation();
Check orientation as your way and use this to change orientation:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
I hope it helps.
Update:
Okay, let's say you've an oAllow var which is Boolean and default value is False.
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getOrientation();
switch(orientation) {
case Configuration.ORIENTATION_PORTRAIT:
if(!oAllow) {
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
break;
case Configuration.ORIENTATION_LANDSCAPE:
if(!oAllow) {
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
break;
}
}
You can add more choices.
I didn't try this sample, but at least tells you some clues about how to solve. Tell me if you got any error.
UPDATE
getOrientation() is already deprecated see here. Instead Use getRotation(). To check if the device is in landscape mode you can do something like this:
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay();
int orientation = display.getRotation();
if (orientation == Surface.ROTATION_90
|| orientation == Surface.ROTATION_270) {
// TODO: add logic for landscape mode here
}
Try running
getResources().getConfiguration().orientation
From your context object to figure out what is the screen orientation at runtime, the possible values are documented here
In order to catch the orientation change event you can find the answer in the Android Dev Guide: Handling the Configuration Change Yourself
From the guide :
For example, the following manifest code declares an activity that
handles both the screen orientation change and keyboard availability
change:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
Now, when one of these configurations change, MyActivity does not restart. Instead, the MyActivity receives a call to onConfigurationChanged(). This method is passed a Configuration object that specifies the new device configuration. By reading fields in the Configuration, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your activity's Resources object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your activity.
...
if (this.getWindow().getWindowManager().getDefaultDisplay()
.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
// portrait mode
} else if (this.getWindow().getWindowManager().getDefaultDisplay()
.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
// landscape
}
You don't need to intercept the event and then override it. Just use:
// Allow rotation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
// Lock rotation (to Landscape)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE);
Points to note here are, if on Jellybean and above this will allow a 180 degree rotation when locked. Also when unlocked this only allows rotation if the user's master settings is to allow rotation. You can forbid 180 degree rotations and override the master settings and allow rotation, and much much more, so check out the options in ActivityInfo
In addition, if you have pre-set that there is to be no rotation, then your activity will not be destroyed and then restarted, just for you to set the orientation back which will again cause the activity to be restarted; Thus setting what you want in advance can be much more efficient.
Pre Jellybean use ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE -- no 180 degree rotation with this.
Check your android screen orientation at Runtime:
ListView listView = (ListView) findViewById(R.id.listView1);
if (getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
//do work for landscape screen mode.
listView.setPadding(0, 5, 0, 1);
} else if (getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
//Do work for portrait screen mode.
listView.setPadding(1, 10, 1, 10);
}
Another solution to determine screen orientation:
public boolean isLandscape() {
return Resources.getSystem().getDisplayMetrics().widthPixels - Resources.getSystem().getDisplayMetrics().heightPixels > 0;
}

Categories

Resources