Video doesn't re-size when orientation changes - android

I have a video displayed using SurfaceView in a fragment. If I change the orientation of the screen while the video is playing it doesn't resize. For example, if the video is playing in landscape size (full screen), it doesn't go to portrait size when the screen is rotated. It remains if landscape size. If I start the video in landscape, it won't change to portrait.I am using the following in my manifest:
<activity
android:name=".BassActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
>
I have this in my "onCreate"
#Override
public void onCreate(Bundle savedInstanceState) {
if (VERBOSE) Log.v(TAG, "+++ onCreate +++");
super.onCreate(savedInstanceState);
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
setRetainInstance(true);
I would like for the video to re-size, for example from portrait to landscape when I rotate the screen, while it is playing.
I also added a toast message to onSurfaceChanged to see when it is triggered,but it is not trigger when I rotate the screen. How can I re-size the video after it has started playing?

use this config changes
if android:configchanges = "orientation"
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// for example the width of a layout
int width = 300;
int height = LayoutParams.WRAP_CONTENT;
WebView childLayout = (WebView) findViewById(R.id.webview);
childLayout.setLayoutParams(new LayoutParams(width, height));
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}

Related

How can I recreate toolbar when rotation occurs

Reading this so question one can see that toolbar uses different heights on landscape vs portrait mode.
But when android:configChanges="orientation|screenSize" is specified, toolbar height remains static according to the orientation in which the activity was first created and won't change when rotating the device.
How can I work this around? I'm thinking that either I should resize the toolbar inside onConfigurationChanged() method or I should destroy and recreate the toolbar and let it get its default height from scratch. I actually like this 2nd way better but I don't know how to do any of these so any suggestions are welcome.
to get rotation event:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
to resize layout:
First you have to declare the layout u wanna resize:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.yourId);
And resize:
rl.getLayoutParams().height = 100; // replace 100 with your dimensions
rl.getLayoutParams().width = 100;

how to play video from same point in full landscape mode when videoview is in ListView android?

I am working on a app where I have list of videos shown in a Listview. And that Listview contains videoview to play video in the ListView itself.
(Note: I have restricted the screen mode to Portrait only)
When I try to play video in VideoView inside a ListView, it is working without any trouble. But what I actually want to achieve is, while the video is playing in videoView inside a Listview and if the user changes the screen mode to Landscape then video should be visible in fullscreen in landscape and start from the same point which it was while playing in Portrait mode.
Please help me out
Thanks
Use the ConfigurationChanged method of Activity.
You may put your VideoView in RelativeLayout
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
switchToFullScreen();
}
}
}
private void switchToFullScreen(){
RelativeLayout mContainer = (RelativeLayout) yourRelativeLayouyt;
mContainer.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
mContainer.setPadding(0, 0, 0, 0);
mContainer.requestFocus();
mContainer.bringToFront();
}

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.

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");
}

SplashScreen not changed while changing the orientation in Android PhoneGap

I tried to set two splash screen for landscape and Portrait. For that, I wrote below code in my activity class
if (getResources().getConfiguration().orientation == 2) {
super.setIntegerProperty("splashscreen", R.drawable.equipsplashscreen);
Log.d("Orientation", "Landscape");
}
else {
super.setIntegerProperty("splashscreen", R.drawable.splashscreen);
Log.d("Orientation", "Portrait");
}
screens are displayed according to the orientation, but the problem is.. screens are not changed while loading(i.e I launched my app in portrait mode, while loading the splash screen I changed the orientation from portrait to landscape, at that time my landscape image was not loading.. only portrait image changed as landscape style ).
I am using cordova-1.8.0 and jquery-1.7.1
How can I do this.. Thanks in Advance..
In Android we have to override the onConfigurationChanged method like this
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.splashscreen);
init();
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.splashscreen);
init();
}
}
And adding one tag in Manifest like this
<activity
android:name=".YourActivity"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize|adjustPan" />
Now check the requirements in phonegap because i don't know phone gap I give you the tip what had done in android.

Categories

Resources