I am loading a video url into webview using webview.loadUrl(). The player is encoded in the html itself so I only have to load the url into web view.
In case if I dont give android:configChanges="orientation" in my manifest everything works fine. The webview resizes properly on orientation change and the video is adjusted to fit the screen properly, ofcourse then the video restarts.
Therefore I gave android:configChanges="orientation" in manifest so that the video doesnt reload. Now although the video doesn't reload and continues to play but the video/webview doesnt resize properly and half of the video goes off screen if the device was initially in portrait and then changed to landscape. In case the device was initially in landscape mode and then is changed to portrait mode only half of the screen is used.
Please reply if you can figure out a way or have any suggestions.
*if configchanges = "orientation" - not specified,*
the android system handles the change and restarts the layout, so layout is freshly created and video loads from start.
if android:configchanges = "orientation" , -
This tell the system that this activity will handle the orientation change by it self.
so,the android system will not load the layout refreshly . so video continues to play .The width of the parent layout will not change . so width in portrait mode is used in landscape . so , ur webview seems small.
you need to override the onconfigurationchanged () in activity and handle the orientation change.
You need to specify the layoutparams for layout.
#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();
// 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();
}
}
Try:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
webView.setLayoutParams(new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
Just add the following line to your activity in the manifest file:
<activity android:configChanges="orientation|screenSize">
facing same issue. and solve it with best way
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
mWebview.setLayoutParams(new LinearLayout.LayoutParams(
width,
height-100));
}
As far as I can see, there must be some error in H5 page, similar resize problem
Just use other app (like chrome browser) test your url.
In fragments you have to use FrameLayout for WebView
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int width = FrameLayout.LayoutParams.MATCH_PARENT;
int height = FrameLayout.LayoutParams.WRAP_CONTENT;
your_webview.setLayoutParams(new FrameLayout.LayoutParams(width, height));
}
My answer is very similar to the above, most closely to #Arash Sharif. This is the variant that worked for me.
Context: My app is a web app and uses only HTML, CSS and JavaScript. I run it as an app on Android by wrapping it in a WebView control. The app is designed to run full screen and I need it to resize when the screen gets reoriented.
I work in Android Studio. I am neither an Android nor Java programmer, so my explanation may not be in the right lingo but I am pretty sure it is valid.
First you need to tell Android that you will handle screen events.
AndoidManifest.xml
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden">
<activity android:name="com.babyclix.babyclix_test.MainActivity"
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Now Android will pass screen events to you to handle, so you need to over-ride the function that handles screen events.
MainActivity.java
// For screen handling on changes (eg. reorientation, resizing)
import android.content.res.Configuration;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
// your stuff
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mywebview.setLayoutParams(new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
}
// more stuff
Related
I have used ActivityInfo.SCREEN_ORIENTATION_LOCKED and ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED to control screen rotation in Android 4.4,it is good.But when I set the ActivityInfo.SCREEN_ORIENTATION_LOCKED in Android 4.2 ,it is useless.
How can I do it?
You have to declare in your manifest
<activity android:name=".MyActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
also you can use android:screenOrientation="landscape"
And in your code something like
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Also you can see this guide.
You'll have to override it programatically. Get the current orientation, then if you toggle to fix the state, set that orientation as your application's orientation.
For Example:
int rotation=getResources().getConfiguration().orientation;
//ORIENTATION_LANDSCAPE = 2, ORIENTATION_PORTRAIT=1
if(rotation==1){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
This will lock it to the current orientation.
And in order to release it, simply use ActivityInfo.SCREEN_ORIENTATION_SENSOR like this:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
That's it.
Now, the final code will look something like this:
if(allow_screen_change.equals("yes")){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}else if(allow_screen_change.equals("no")){
int rotation=getResources().getConfiguration().orientation;
//ORIENTATION_LANDSCAPE = 2, ORIENTATION_PORTRAIT=1
if(rotation==1){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
I just made this and tested on HTC One X, running Android 4.2 and it works just fine. Hope it helps!
i am developing a game in which i have to use both landscape mode for my game scene.
but when i change orientation my game restart and load from splash screen how to stop this.
could any one please help me.
i am using
final EngineOptions eo = new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR,
new FillResolutionPolicy(), _camera);
http://developer.android.com/guide/topics/resources/runtime-changes.html. Please check the documentation under the heading Handling the Configuration Change Yourself.
<activity android:name=".Activity_name"
android:configChanges="orientation|keyboardHidden|screenSize">
Screen size to be added for 3.2 and above.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
//do something
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//do something
}
}
I assume you have a splash screen and after displaying splash screen navigates to activity called Main. In this case your splash screen should run only once when the app is started. You have to call finish() before navigating to next activity. Splash Screen gets destroyed and you navigate to next screen.
add following in your manifest file in your activity tag
android:ConfigChanges="keyboardHidden|orientation|screensize"
add screenOrientation tag in your GameActivity in AndroidManifest.xml
<activity
android:name=".YourgameActivity"
android:screenOrientation="landscape" >
</activity>
I have an app with several 'normal' activities which can run on either landscape or portrait. They are designed for and mostly used on portrait.
This app has one single activity which uses the camera and is locked on landscape. I 'simulate' this activity is on portrait by rotating images and texts 90 degree, so it looks like the rest of activities.
On some device, such as Samsung Galaxy Tab 7 and Galaxy S3, a rotation animation is shown when going from a normal portrait activity to camera landscape activity and back. This is confusing for user because landscape activity simulates being on portrait.
Is there a way to remove this rotation animation? Ideally I'd like to change to default portrait to portrait animation, but just removing rotation animation would be enough.
I've tried
overridePendingTransition(0, 0);
an other variations of that method without success.
[ADDED]
Following suggestions by #igalarzab, #Georg and #Joe, I've done this (still with no luck):
Added android:configChanges="orientation|screenSize" to Manifest
Added onConfigurationChanged
Created a dummy animation which does nothing and added overridePendingTransition(R.anim.nothing, R.anim.nothing);
I had these results:
onConfigurationChanged is called only when rotating same Activity (Activity A on portrait -> Activity A on landscape). But it's not called when going from Activity A on portrait -> Activity B on landscape
This prevented Activity from being restarted when rotating, but it did NOT removed rotation animation (tested on Galaxy S3, Galaxy Nexus, Galaxy Tab 7.0 and Galaxy Tab 10.1)
overridePendingTransition(R.anim.nothing, R.anim.nothing); removed normal transitions (portrait->portrait and landscape->landscape) but it didn't removed rotation animation (portrait->landscape and vice versa).
[VIDEO]
I've uploaded a video that shows animation I want to disable. This happens when changing from camera activity (locked to landscape) to other activity while holding phone on portrait:
http://youtu.be/T79Q1P_5_Ck
Sorry there is no way to control the rotation animation. This is done way outside of your app, deep in the window manager where it takes a screenshot of the current screen, resizes and rebuilds the UI behind it, and then runs a built-in animation to transition from the original screenshot to the new rebuilt UI. There is no way to modify this behavior when the screen rotation changes.
This is the way how the stock camera app disables rotation animation:
private void setRotationAnimation() {
int rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
winParams.rotationAnimation = rotationAnimation;
win.setAttributes(winParams);
}
Note: According to API Reference and comment below, this only works:
On API level 18 or above
The FLAG_FULLSCREEN flag is set for WindowManager.LayoutParams of the Activity
The Activity is not covered by another window (e.g. the Power Off popup triggered by long pressing the Power button)
You can set in the AndroidManifest a property called android:configChanges where you can decide which changes you want to manage in code. In this way, the rotation change animation will be disabled and you can handle it as you want in the method onConfigurationChanged of your Activity.
<activity
android:name=".MySuperClass"
android:label="#string/read_qrcode"
android:screenOrientation="portrait"
android:configChanges="orientation" />
android:rotationAnimation="seamless"
add this attribute in activity will reduce animation when rotate. I try to find it when i want to make smooth camera app
I've put that in the mainActivity and it cancelled the rotation animation:
#Override
public void setRequestedOrientation(int requestedOrientation) {
super.setRequestedOrientation(requestedOrientation);
int rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_JUMPCUT;
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
winParams.rotationAnimation = rotationAnimation;
win.setAttributes(winParams);
}
More details here.
You might have tried this already, but just in case:
Try defining a "do nothing" animation and call overridePendingTransition() with its id. Maybe something like:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<translate
android:fromXDelta="0"
android:toXDelta="0"
android:duration="100" />
</set>
Sorry, I didn't have a Galaxy device to test with :)
You can set in the AndroidManifest a property called
android:configChanges where you can decide which changes you want to
manage in code. In this way, the rotation change animation will be
disabled and you can handle it as you want in the method
onConfigurationChanged of your Activity.
This should work, but according to this page you should also add screenSize to configChanges by adding android:configChanges="orientation|screenSize" to your Activity Tag.
If you want to set your activities in portrait mode only you can do it in your manifest file like this
<activity
android:name=".Qosko4Activity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Successfully checked on: Java - Android 9 (Pie) - Android Studio 4.1.3 - Huawei P10
For six months I could not solve this need. The problem was in the unassigned flag "FLAG_FULLSCREEN" in code.
First step MainActivity.java:
public class MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); */ // If the animation still is, try applying it.
setRotationAnimation();
}
private void setRotationAnimation() {
int rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_JUMPCUT;
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
winParams.rotationAnimation = rotationAnimation;
win.setAttributes(winParams);
}
public void Button (View view) {
// Connected to android:onClick="Button" in XML.
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
}
Next step MainActivity2.java:
public class MainActivity2 {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
/* getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); */ // If the animation still is, try applying it.
setRotationAnimation();
}
private void setRotationAnimation() {
int rotationAnimation =
WindowManager.LayoutParams.ROTATION_ANIMATION_JUMPCUT;
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
winParams.rotationAnimation = rotationAnimation;
win.setAttributes(winParams);
}
public void Button (View view) {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
startActivity(intent);
}
}
Next step styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:windowFullscreen">true</item>
</style>
</resources>
Simple things I understood:
You must first anywhere start "setRotationAnimation()", before running activity2.
When start activity2 you have to run in it "setRotationAnimation()".
Works incorrectly.
Against them who said no I say yes it is possible and it is very simple! The first thing may sound stupid but lock your application to the desired orientation! Then keep asking the gyrometer what orientation the device has an last but not least rotate or animate your views to the new orientation!
Edit: you may want to hide the system ui since it won't rotate.
Mario
I have prepared one application.My application will be supports both landscape and portrait.
When i change the orientation from portrait to landscape it is working fine,But when i change landscape to portrait it is not working.my code is,
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
System.out.println("orientation---"+getResources().getConfiguration().orientation);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.main);
System.out.println("landscape-----");
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.main);
System.out.println("portrait-----");
}
}
Every time time it taking only landscape mode.I made mistake some where. please help.
System.out.println("orientation---"+getResources().getConfiguration().orientation);
The above SOP is printing 2 means landscape.Please help me.(i am using 2.3.3)
You might be hitting this bug "Orientation does not change from landscape to portrait on Emulator on 2.3"
More than 45 people are facing the same issue with the androidd 2.3 emulator
http://code.google.com/p/android/issues/detail?id=13189
Host OS: Windows 7 SDK tools version : revision 8 Eclipse version: 3.5
ADT plug-in version: 8.0.1 Platform targeted by your project: 2.3
Version of the platform running in the emulator:2.3
STEPS TO REPRODUCE:
1. Activity 1 (no screen orientation) - Activity 2 (screen orientation = landscape)
Going from Activity 1 to 2 works fine. Pressing back changes the orientation of Activity 1 to landscape
EXPECTED RESULTS: Activity 1 should be in portrait mode
OBSERVED RESULTS: Behavior is observed only in gingerbread, works as
expected on froyo.
The issue is with the android:configChanges="orientation|keyboardHidden" .
if you remove |keyboardHidden from the androidmanifest.xml, then the onConfigurationChanged is only fired when you rotate from landscape to portrait, not when you go from portrait to landscape (at least in the default emulator).
Hope this helps.
EDIT: DID some sample program to test it and works fine for me.
CheckOrientationActivity.java
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class CheckOrientationActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
Log.i("DayElevenActivity", "onCreate Start");
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("DayElevenActivity", "onConfigurationChanged");
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.i("ORIENTATION_LANDSCAPE", "ORIENTATION_LANDSCAPE");
Toast.makeText(getBaseContext(), "ORIENTATION_LANDSCAPE", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.i("ORIENTATION_PORTRAIT", "ORIENTATION_PORTRAIT");
Toast.makeText(getBaseContext(), "ORIENTATION_PORTRAIT", Toast.LENGTH_SHORT).show();
}
}
}
In AndroidManifest.xml
<activity
android:label="#string/app_name"
android:configChanges="**orientation|keyboardHidden**"
android:name=".CheckOrientationActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Corresponding Logs when i run and change the orientation
03-05 18:54:31.644: I/CheckOrientationActivity (20314): onCreate Start
03-05 18:54:35.014: I/CheckOrientationActivity (20314): onConfigurationChanged
03-05 18:54:35.014: I/ORIENTATION_LANDSCAPE(20314): ORIENTATION_LANDSCAPE
03-05 18:54:41.984: I/CheckOrientationActivity (20314): onConfigurationChanged
03-05 18:54:41.984: I/ORIENTATION_PORTRAIT(20314): ORIENTATION_PORTRAIT
There's multiple reasons why this could happen, hard to tell without seing more code.
Here's some pointers:
you haven't forced the layout to some particular orientation via manifest file or programatically (something like <activity android:screenOrientation="portrait"> in your manifest)
by default the Activity is restarted when configuration changes happend. If you want to NOT have your activity restarted when the orientation changes, you need to use
android:configChanges="orientation"
on your activity's configuration in the manifest.
the above point will in fact make Android call your activity's onConfigurationChanged(Configuration) method when the orientation changes
==update==
Also, if you have the same layout (like "main") declared in layout-land and layout-port you only need to specify that you want to use that layout in the beginning:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
and that's it. THERE IS NO NEED to also implement the
public void onConfigurationChanged(Configuration newConfig) {
//...
}
method as well. For the sake of chosing the correct layout based on orientation all you have to do is declare the same layout in those two folders.
(Also, there is NO NEED to add the android:configChanges="orientation" to your manifest, apparently it's on by default). I've tried all this just now, works ok
I want to run a Hello World sample application only in landscape mode on my Device.
If the device changed to portrait, I would then like to raise one toaster. For example "please change to landscape mode to run your application".
How should I do this?
You can go for both programmatically as follows:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
or also in manifest file you can give here as
<activity android:name=".YourActivity" android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation"/>
add this to your activity tag in your manifest file:
android:screenOrientation="landscape"
It will fix your orientation to landscape mode and you need not to show any toast to user.
in your Manifestfile(xml)
<activity android:name=".ActivityName" android:screenOrientation="landscape">
</activity>
refer this also Is that possible to check was onCreate called because of orientation change?
You may try something like this
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Log.v("log_tag", "display width is "+ width);
Log.v("log_tag", "display height is "+ height);
if(width<height){
Toast.makeText(getApplicationContext(),"Device is in portrait mode",Toast.LENGTH_LONG ).show();
} else {
Toast.makeText(getApplicationContext(),"Device is in landscape mode",Toast.LENGTH_LONG ).show();
}
You can use the configChanges-attribute for the Activity you want to catch the screen-orientation change for.
After that, the onConfigurationChanged()-method in your Activity will be called when the orientation changes.
To check which orientation is currently displayed, check this older post: Check orientation on Android phone
After that, you can show off your message or do whatever you like (of course you can also set it to only show in landscape).