Disable default animation from Portrait to Landscape - android

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

Related

Dim screen upon idle time. Wake up upon screen input

So I am fairly new to the world of coding and I am doodling with a little private learning project.
I have made a simple web browser based on WebView for a embedded android 7.1 ELOtouch device.
I have found plenty of articles online on how to turn the screen on/off etc but never really managed to make it work.
What I am trying to do is that the screen dims down to the lowest level after xx amount of time, let’s say 5min. And only dims back up to a defined level upon user touch/screen input.
The unit is always on and don’t have any form for advanced screen adjustments in settings, so as I see it, it has to be done programmatically.
Thankful for and advice or guidance.
Attached one of my sources:
How to change screen timeout programmatically?
To change screen brightness by user touch, you could do like this:
In AndroidManifest.xml file, add this line:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
And in some place of activity class file:
Settings.System.putInt(this.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, 80);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness =0.8f;
getWindow().setAttributes(lp);
startActivity(new Intent(this,DummyActivity.class));
Note: when setting up brightness,the modification doesn't take effect immediately, to solve this problem,just start another blank dummy activity and finish it in Oncreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finish();
}
Don' forget to include DummyActivity in AndroidManifest.xml.
Edited:
To dim screen after some time, the basic logic is same, and maybe you should create a Timer(TimerTask).
Hope this is helpful!

Android: Black Screen between Activity

When I go one activity to another activity , between the transaction a Black screen is come for some seconds. I properly finish the activity before calling startActvity().
Am using android:theme="#android:style/Theme.Translucent" theme for my activity. Even though between the activity transaction a black screen is coming
Can any one please tell me how to resolve this
Thanks in advance :)
There is no need to finish activity before calling startActivity().
Make sure that you have set content view in the onCreate of called Activity and that you are not blocking UI thread (check onCreate, onStart and onResume if you have override them).
You don't need to manage finshing your activity, this will be managed automatically when the activity is no longer in view. Just use:
startActivity(new Intent(this, MyNextActivity.class));
And use this code in whatever method you are using to navigate the activity changes.
If you make sure your window is the background of your activities you can set the window background to a color other than black:
<item name="android:windowBackground">#drawable/window_background</item>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/window_background"/>
</shape>
windowBackground in Android 6 (Marshmallow)
The other option is to manage transitions, so there is no gap between the end of the first transition and the beginning of the second. However, you have not mentioned transitions.
How to remove the delay when opening an Activity with a DrawerLayout?
for disable this default animation create one style:
<style name="noAnimTheme" parent="android:Theme">
<item name="android:windowAnimationStyle">#null</item>
</style>
and set it as theme for your activity in the manifest:
<activity android:name=".ui.ArticlesActivity" android:theme="#style/noAnimTheme">
</activity>
Assumption :-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xyz);
// comment code here
}
If you go from activity A to B then try to comment code in OnCreate , OnResume in Activity B Like this and check what happen still black screen is coming or not.If coming then try to change theme.
If you have a finish() or FLAG_ACTIVITY_CLEAR_TASK - a blank screen may show up on pre ICS devices
To avoid this black screen you have to add one line in intent
overridePendingTransition (0, 0);
Example(kotlin):
val intent = Intent(applicationContext, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
overridePendingTransition (0, 0)
Example(Java):
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition (0, 0);

How to lock screen rotation in the code on Android 4.2 and lower?

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!

Creating custom LockScreen in android [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am developing custom lockscreen app.its working fine in below 4.0 but above 4.0,when we press home button the app stops.is there any solution for this no apps will stop when pressing home button untill unlocking the screen.(like go locker app)
Another way to develop a LockScreen App is by using Views, let me explain it.
First of all you can "disable" in some devices the System lock screen by disabling the KEYGUARD:
((KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock("IN").disableKeyguard();
You should put this line of code in your Service.
After that you can launch an activity every time the screen goes off:
public class AutoStart extends BroadcastReceiver {
public void onReceive(Context arg0, Intent arg1) {
if(arg1.getAction().equals("android.intent.action.SCREEN_OFF")) {
Intent localIntent = new Intent(arg0, LockScreen.class);
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
localIntent.addFlags(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
arg0.startActivity(localIntent);
}
}
}
If you read the documentation for WindowManager.LayoutParams.TYPE_SYSTEM_ERROR it explains that is a type of internal system error windows, appear on top of everything they can. In multiuser systems shows only on the owning user's window.
So now you have an activity on top of everything, but a press in HOME button will exit the activity.
Here is where the Views make their appearance. You can inflate a view from a layout resource and add it to the WindowManager as a TYPE_SYSTEM_ERROR, so will be on top of everything. And since you can control when to remove this View, the best place is in onDestroy of your Activity, because pressing the HOME button will only pause your activity, and the view will still be visible.
public WindowManager winManager;
public RelativeLayout wrapperView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
this.winManager = ((WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE));
this.wrapperView = new RelativeLayout(getBaseContext());
getWindow().setAttributes(localLayoutParams);
View.inflate(this, R.layout.lock_screen, this.wrapperView);
this.winManager.addView(this.wrapperView, localLayoutParams);
}
public void onDestroy()
{
this.winManager.removeView(this.wrapperView);
this.wrapperView.removeAllViews();
super.onDestroy();
}
To avoid the notification bar of showing I added the flags FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL | FLAG_LAYOUT_IN_SCREEN to consume all pointer events.
Not forget to add these two lines to your manifest:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
From here you just need to add the logic of your Lock Screen app to let the user use his smartphone :)
A custom launcher is basically an app (you can make it behave like a grid, list, implement your own drag and drop etc) then, you only need to add these lines to the intent filter of the main activity, with this done, after you install your app and press the home button your app will appear in the list of available homescreens.
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
What i cant find is a way to replace the lock screen, and hacks like disabling the lock screen on the phone and using an activity in a custom launcher isn't actually replacing the lockscreen ^^
You can use the below method to disable the Home key in android :
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
I am developing on a Samsung Galaxy S4 5.0 and what worked for me was simply changing getWindow().setFlags(..) to getWindow().addFlags(..)
I think first of all you should ask yourself if you really want to hijack the home key. Sometimes you may want it. But I think placing the app on the Android lock screen, letting the home key act normally and letting the underlying Android lock screen take care of password-protecting the device is what you actually want in a lot of cases (unless you want to change the way this is done by default).
Bottom line, letting an app be displayed on the Android lock screen comes pretty close to writing your own custom lock screen. And is decidedly easier since you don't have to manage passwords yourself. Not to mention it's safer and more reliable since you don't hijack the home key.
I did it like this and it works very well. You can see the details here:
show web site on Android lock screen
The question is about displaying a website on the lock screen, since that's what I was interested in, but the answer is more general, it works with any app.
You can see here an app that's on Google Play and has been written like this:
https://play.google.com/store/apps/details?id=com.a50webs.intelnav.worldtime

how to prevent game restart when change orientation in AndEngine

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>

Categories

Resources