I'm working with FlashBuilder 4.6 to develop Android app.
In the application there is a requirement of setting some values when device orientation changes. i.e. setting the values when the screen/device orientation changes from Landscape to Portrait and vice-verse.
Although Initially my application has LandScape orientation. And this requirement is on specific view.
I want every time when the screen/device orientation changes the values must be set there.
I am not getting the desired result.
Please guide me and tell me if I am at wrong in code or how can i achieve this.
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="{data}"
xmlns:mx="library://ns.adobe.com/flex/mx"
viewActivate="view1_viewActivateHandler(event)"
creationComplete="view1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
protected function view1_viewActivateHandler(event:ViewNavigatorEvent):void
{
if(Accelerometer.isSupported)
{
accl = new Accelerometer();
accl.addEventListener(AccelerometerEvent.UPDATE,update); //accl.addEventListener(AccelerometerEvent.UPDATE,adjustImage);
}
}
private function update(event:AccelerometerEvent):void
{
this.stage.autoOrients = true;
//in the below line I am attaching StageOrienationEvent that will adjust the
//values.
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE,adjust);
}
private function adjust(event:StageOrientationEvent):void
{
if(event.afterOrientation == StageOrientation.ROTATED_LEFT)
{
testVal.text ="After OR is LEFT";
}
else if(event.afterOrientation == StageOrientation.ROTATED_RIGHT)
{
testVal.text ="After OR is RIGHT";
}
else if(StageAspectRatio.LANDSCAPE)
{
testVal.text ="StageAspectRatio is Landscape";
}
else if(StageAspectRatio.PORTRAIT)
{
testVal.text="StageAspectRatio is Portrait";
}
}
</fx:Script>
Thanks.
Finally found the solution after spending two days on this.
in update function add OrientationChanging Event in stage.addEventListner.
private function update(event:AccelerometerEvent):void
{
this.stage.autoOrients = true;
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING,adjust);
}
private function adjust(event:StageOrientationEvent):void
{
switch(event.afterOrientation)
{
case StageOrientation.DEFAULT:
//Do Something here. The Portrait Position.
break;
case StageOrienatation.ROTATED_LEFT:
//Do Something here when you rotate your phone portrait to left side.
break;
case StageOrienatation.ROTATED_RIGHT:
//Do something here when you rotate your phone portrait to right side.
break;
}
}
Many thanks to all who viewed this question and try put their efforts for solution. :)
i am not a experimented coder! but : private function update(event:AccelerometerEvent):void should be : private function accl(event:AccelerometerEvent):void ?? or something like this in below code if(stage[not event].afterOrientation == ??? its just a opinion!
Related
What does setting android:screenOrientation in the activity of the AndroidManifest.xml actually do?
If I set it, I can still change my screen orientation - so my question is what is its purpose?
I have a Unity game in portrait, that in one section I want to enable rotation - I can do this from Unity without changing the manifest - so it doesn't appear to be preventing me from changing screen orientation - so what is the purpose of it?
Should my game be SensorPortrait, or FullSensor because I enable rotation at one point? What will the difference be?
The docs indicate that it's used for filtering purposes in the Play store, but surely it serves some other purpose?
The android:screenOrientation parameter in the manifest is intended to change the activity orientation.
The reason why it doesn't seem to do anything is because of Unity, it seems. This forum describes a similar problem and solution:
We currently do override the Android manifest with a custom one, which works well in most cases. However, I found that when I try to override the screenOrientation value, it doesn't seem to stick through the build pipeline. At some point, I think Unity overwrites the screenOrientation attribute depending on the PlayerSettings values. Unfortunately though, there doesn't seem to be a PlayerSettings configuration that allows us to use the "userPortrait" setting.
// as an android plugin through unity
public static int GetAutorotateSetting(Activity activity)
{
int setting = 0;
try
{
setting = Settings.System.getInt(activity.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION);
}
catch(Exception e)
{
Log.i("Unity", "Couldn't retrieve auto rotation setting: " + e.getMessage());
}
return setting;
}
// on the unity side:
public static bool AllowAutorotation()
{
bool doAutorotation = false;
#if !UNITY_EDITOR && UNITY_ANDROID
AndroidJavaClass unity = newAndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject unityActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
using(AndroidJavaClass andClass = new AndroidJavaClass("PUT YOUR JAVA CLASS HERE"))
{
int allowAutorotation = andClass.CallStatic<int>("GetAutorotateSetting", unityActivity);
if(allowAutorotation == 0)
{
doAutorotation = false;
}
else
{
doAutorotation = true;
}
}
#endif
return doAutorotation;
}
The person who suggested this solution also put a .unitypackage file on github.
I'm developing a cross platform app using cordova with an angular material front end.
I use HTML5 video tags in a list of md-cards to play videos with external urls. When inline the videos play correctly, and display the native controls as expected.
<video class="project-video" video-directive item="$ctrl.project" ng-src="{{$ctrl.project.videoUrl | trustUrl}}" preload="auto"
controls poster="{{$ctrl.project.video.thumbnail_url}}">
Your browser does not support the video tag.
</video>
However when I click the "toggle full-screen" button the video does go to full-screen, but the default controls disappear. I cannot get back to the app after this - the native android back button does not close the full screen - instead it closes the whole app.
The solution I am looking for will make the controls always appear even in full screen mode; this works out the box running the same code on iOS.
Therefore I do not want to spend time developing my own custom video controls just for android, if I can help it! So please do not post answers about how to do that (plenty already available on SO and elsewhere).
I am using a Meizu m2 note android device.
Thanks!
EDIT:
The controls are still there but are showing up in the shadow DOM tree in css as being of size 0 x 0px. Even when I change their size in chrome dev tools using the !important flag, they do not show up.
Any ideas?
This is an issue with Flyme OS which is used by Meizu devices. Since the controls are available and not visible, this should be resolved by ugrading the Flyme OS.
Please update Flyme OS to resolve this as the older versions of Flyme seems to have quiet some problems with fullscreen video mode. Hope it helps. Cheers
set the values which then allow to exit fullscreen.
var videoElement = document.getElementById("myvideo");
function toggleFullScreen() {
if (!document.mozFullScreen && !document.webkitFullScreen) {
if (videoElement.mozRequestFullScreen) {
videoElement.mozRequestFullScreen();
} else {
videoElement.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
}
document.mozFullScreen = true;
document.webkitFullScreen = true;
} else {
if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else {
document.webkitCancelFullScreen();
}
}
}
document.addEventListener("keydown", function(e) {
if (e.keyCode == 13) {
toggleFullScreen();
}
}, false);
add these two lines ..
document.mozFullScreen = true;
document.webkitFullScreen = true;
if you want something better
fullScreenButton.addEventListener("click", function() {
if (!document.fullscreenElement && // alternative standard method
!document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement ) { // current working methods
if (video.requestFullscreen) {
video.requestFullscreen();
} else if (video.msRequestFullscreen) {
video.msRequestFullscreen();
} else if (video.mozRequestFullScreen) {
video.mozRequestFullScreen();
} else if (video.webkitRequestFullscreen) {
video.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
}
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
}
});
How about this in your code your not mentioned controls attribute fully may be that could cause the problem
<video id="myvideo">
<source src="path/to/movie.mp4" />
</video>
<p onclick="toggleControls();">Toggle</p>
<script>
var video = document.getElementById("myvideo");
function toggleControls() {
if (video.hasAttribute("controls")) {
video.removeAttribute("controls")
} else {
video.setAttribute("controls","controls")
}
}
</script>
I'm trying to determine when a screen is rotated (in Android) using the XLabs method detailed here How to handle screen rotation/orientation in Xamarin Forms? and I'm having trouble with it.
I override the OnConfigurationChanged method in MainActivity
public override void OnConfigurationChanged (Android.Content.Res.Configuration newConfig)
{
base.OnConfigurationChanged (newConfig);
var xapp = Resolver.Resolve<IXFormsApp> ();
if (xapp == null)
return;
switch (newConfig.Orientation) {
case Android.Content.Res.Orientation.Landscape:
xapp.Orientation = XLabs.Enums.Orientation.Landscape;
break;
case Android.Content.Res.Orientation.Portrait:
//xapp.Orientation = XLabs.Enums.Orientation.Portrait;
break;
default:
break;
}
}
I'm having trouble with the Orientation variable in IXFormsApp i.e. xapp.Orientation. The XLabs documentation lists this as 'protected set', as does the compiler:
MainActivity.cs(109,5,109,21): error CS0200: Property or indexer 'XLabs.Platform.Mvvm.IXFormsApp.Orientation' cannot be assigned to -- it is read only
and it doesn't get set automagically (when I check where it is used, it is always set to 'None'), so I was wondering how to make use of it, and indeed, how to use the XLabs/IXFormsApp to determine rotation?
On a related note, I was also trying to set the Rotation handler (not sure why, but I thought I'd give it a go) with unusual results.
xapp.Rotation += (sender, args) =>
{
switch (args.Value)
{
case XLabs.Enums.Orientation.Landscape:
//xapp.Orientation = XLabs.Enums.Orientation.Landscape;
...
break;
case XLabs.Enums.Orientation.Portrait:
...
break;
default:
break;
}
};
If I try in the Android code I get the following error:
MainActivity.cs(60,4,60,22): error CS0019: Operator '+=' cannot be applied to operands of type 'System.EventHandler<XLabs.EventArgs<XLabs.Enums.Orientation>>' and 'lambda expression'
however, if I set it in the Forms code (where the results are used), it is fine (altho the handler never seems to actually be called). Does anyone know whay this would be the case?
There are two different solutions I have used in the past.
The first is by making a PageBase class which all my pages inherit from, and PageBase inherits from a regular Page.
My PageBase has two abstract methods (so the children of it have to fill it in), which are UpdateLandscape and UpdatePortait. Children will fill these methods in for how to layout the page depending on whether it is being laid out in landscape or portrait mode.
Pages have a method OnSizeAllocated, as Daniel said. I made PageBase override it and make it call UpdateLandscape and UpdatePortait accordingly.
If, as you said, you are only looking to check when it has rotated, the above works just fine, as OnSizeAllocated gets called for a page whenever you rotate your phone.
If you are checking for landscape vs portait because you want your code to be able to check at any time, then the second solution below works too.
The second way I solved it is to use dependency services to fill in an IDeviceInfo interface, and write all dynamic things by checking if DeviceInfo.IsPortait() is true or false (and this way I also let DeviceInfo have a Width and Height, so I can request the screen dimensions at any point).
On Android, I filled in my Android code as so:
[assembly: Dependency (typeof(Namespace.DeviceInfoProvider))]
namespace Namespace
{
public class DeviceInfoProvider : IDeviceInfoProvider
{
public bool IsPortait () { return DeviceInfoManager.Width < DeviceInfoManager.Height; }
public int GetWidth () { return DeviceInfoManager.Width; }
public int GetHeight () { return DeviceInfoManager.Height; }
}
public static class DeviceInfoManager
{
public static MainActivity MainActivity { get; set; }
public static int Width { get { return MainActivity.GetWidth (); } }
public static int Height { get { return MainActivity.GetHeight (); } }
}
}
Then in MainActivity I gave it these methods:
public int GetWidth() {
return (int)(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density);
}
public int GetHeight() {
return (int)(Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density);
}
And on the iOS side, I filled it in as so:
[assembly: Dependency (typeof(Namespace.DeviceInfoProvider))]
namespace Namespace {
public class DeviceInfoProvider : IDeviceInfoProvider {
public bool IsPortait() { return UIScreen.MainScreen.Bounds.Width < UIScreen.MainScreen.Bounds.Height; }
public int GetWidth() { return (int)UIScreen.MainScreen.Bounds.Width; }
public int GetHeight() { return (int)UIScreen.MainScreen.Bounds.Height; }
}
}
Personally, I am more of a fan of writing it the second way and making it check "if we are in portait mode, here are the differences". That way those things that are not different between portait and landscape only have to be written once, only the differences are written twice.
You can use OnSizeAllocated method override to detect orientation change;
double previousWidth;
double previousHeight;
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
if (previousWidth != width || previousHeight != height)
{
previousWidth = width;
previousHeight = height;
if (width > height)
{
// landscape mode
}
else
{
// portrait mode
}
}
}
In my AIR AS3 app I'm trying to override Back Button like this:
NativeApplication.nativeApplication.addEventListener( KeyboardEvent.KEY_DOWN, onKey );
private function onKey(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.BACK)
{
//stage.addChild(new MainMenuScreen());
//stage.removeChild(this);
//removeEventListener(KeyboardEvent.KEY_DOWN, onKey);
}
}
It seems to me that my code is getting done but the default Android behaviour (App is closed) is executed as well.
Have someone faced this problem?
If you want to stop the app closing, you should intercept the EXITING event:
NativeApplication.nativeApplication.addEventListener(Event.EXITING, exitHandler);
function exitHandler(event:Event):void
{
event.preventDefault();
}
I guess i should add that you can manually close the app with:
NativeApplication.nativeApplication.exit();
Just prevent the default action (closing the application) but make sure you still allow the application to close if there's nothing else to do (by using a readyToClose variable, for example):
private function onKey(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.BACK)
{
if(!readyToClose)
{
e.preventDefault();
//stage.addChild(new MainMenuScreen());
//stage.removeChild(this);
//removeEventListener(KeyboardEvent.KEY_DOWN, onKey);
}
}
}
Thank you all guys for your help!
The problem was due to my FlashDevelop IDE. I used PackageApp.bat instead of Run.bat, so wrong versions of my .apk were deployed onto device. Hope this post will help others who may face this problem
Here is another way to do it - if you use multiple stages you can put this in frame 1 of your first action panel
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, CheckKeypress, false, 0, true)
function CheckKeypress(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.BACK:
event.preventDefault();
gotoAndPlay (1);
break;
case Keyboard.MENU:
trace("Menu key is pressed.");
break;
case Keyboard.SEARCH:
trace("Search key is pressed.");
break;
}
}
I'm simply trying to toggle auto brightness on and off.
I started with this code (inside the onCreate method)
final ToggleButton autoBrightToggle = (ToggleButton) findViewById(R.id.brightToggle);
// display auto brightness state
final ToggleButton autoBrightToggle = (ToggleButton) findViewById(R.id.autoToggle);
autoOnOrOff.setText(String.valueOf(getAutoBrightnessMode()));
autoBrightToggle.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (autoBrightToggle.isChecked()) {
setAutoBright(true);
} else {
setAutoBright(false);
}
}
}); // end anonymous OnClickListener function
// toggle the brightness mode
private void setAutoBright(boolean mode) {
if (mode) {
Settings.System.putInt(cr, SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
autoOnOrOff.setText(String.valueOf(getAutoBrightnessMode()));
} else {
Settings.System.putInt(cr, SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
autoOnOrOff.setText(String.valueOf(getAutoBrightnessMode()));
}
}
Which doesn't seem to work. The setAutoBrightnessMode() method is also called again in onResume() but with the same non-results.
Anyway, I'm sorry if someone feels this question is redundant but the other posts did not get me where I need to go!
(FWIW - I'm testing this on my old Droid X and my Galaxy Nexus, not the Emulator)
EDITED - UPDATE ON THIS:
I'm 99% sure now that I am not seeing any changes to the Auto-Brightness mode reflected in the Settings panel and desktop widgets - even though I may actually be changing it's value.
part of the problem is that I don't know how exactly to determine if Auto-Brightness is on or not!
For instance, does the screen quickly and visibly change? I've been expecting immediate visible changes in brightness according to environment - but perhaps the changes are subtle? and over a longer period? or perhaps it takes 30 seconds or more of environment change before brightness changes?
Can someone suggest how I can track this? I've tried querying the Settings.System.SCREEN_BRIGHTNESS_MODE constant - hooking this method up to a textfield:
private int getAutoBrightnessMode() {
try {
int brightnessMode = Settings.System.getInt(cr, SCREEN_BRIGHTNESS_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
int brightnessMode = -10000;
}
return brightnessMode;
}
But it always reads 0, even after an onResume(). :-((
I know this is a simple procedure, but I'm trying to learn this stuff on my own, and have had almost no formal CS training... So all I can say is I'm very frustrated by this and feel like I've worked myself into a corner and at this point I'm so annoyed I can't think straight anymore.
So help would be great.
I use following approach in my application. Tested on HTC Desire HD and pair of noname chinese tablets.
Add to manifest permission:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
And use below code to toggle auto brightness. There is one trick in the code: we need to "refresh" brightness of app manually, because it doesn't changes automatically. May be it is the problem in your case.
void setAutoBrightness(boolean value) {
if (value) {
Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
} else {
Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
}
// After brightness change we need to "refresh" current app brightness
if (isChecked) {
refreshBrightness(-1);
} else {
refreshBrightness(getBrightnessLevel());
}
}
private void refreshBrightness(float brightness) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
if (brightness < 0) {
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
} else {
lp.screenBrightness = brightness;
}
getWindow().setAttributes(lp);
}
int getBrightnessLevel() {
try {
int value = Settings.System.getInt(getContentResolver(), SCREEN_BRIGHTNESS);
// convert brightness level to range 0..1
value = value / 255;
return value;
} catch (SettingNotFoundException e) {
return 0;
}
}