3

I have a custom RelativeLayout and I even have set setLayoutTransition(null);. I add this custom view to the WindowManager with ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).updateViewLayout(this, layoutParams);

I change views in the custom view AND I change the LayoutParams for the WindowManager and afterwards call updateViewLayout...

I think, the chang of the LayoutParams for the WindowManager is animated, but I'm not sure...

How can I disable ALL animations?

prom85
  • 16,896
  • 17
  • 122
  • 242

1 Answers1

14

This one's a little annoying. Android animates all window changes, and they've made the flag to disable it private. You can disable window animations using reflection

    WindowManager.LayoutParams wp = new WindowManager.LayoutParams();
    String className = "android.view.WindowManager$LayoutParams";
    try {
        Class layoutParamsClass = Class.forName(className);

        Field privateFlags = layoutParamsClass.getField("privateFlags");
        Field noAnim = layoutParamsClass.getField("PRIVATE_FLAG_NO_MOVE_ANIMATION");

        int privateFlagsValue = privateFlags.getInt(wp);
        int noAnimFlag = noAnim.getInt(wp);
        privateFlagsValue |= noAnimFlag;

        privateFlags.setInt(wp, privateFlagsValue);

        // Dynamically do stuff with this class
        // List constructors, fields, methods, etc.

    } catch (ClassNotFoundException e) {
        Logger.l.e(e.toString());
        // Class not found!
    } catch (Exception e) {
        Logger.l.e(e.toString());
        // Unknown exception
    }

Now wp will not animate layout changes. Note you'll probably see flicker when you change the window size. I haven't found a way to work around that yet.

stewjacks
  • 471
  • 6
  • 12
  • That's what I'm doing currently. I just use `flag |= 0x00000040;` instead of the name and I don't call `Class.forName` but rather use the class directly. Thanks anyway, I'll mark your answer as correct – prom85 Aug 27 '15 at 13:58
  • You've got to use `Reflection` as this is a `privateFlag`. You can't just apply it as a regular flag. – stewjacks Aug 30 '15 at 20:08
  • As I said, I do the same thing you say, just with the above mentioned differences. Of course, I use reflection as well. I just meant, that I already had solved the problem myself... Still your answer is correct and therefore I marked it as answer. – prom85 Aug 30 '15 at 20:11
  • 1
    gotcha. always use flag names instead of constants. You never know when a flag constant might change and you're stuck with a problem you can't debug. – stewjacks Aug 30 '15 at 20:48
  • actually, that was the reason why I used the number. I thought, this is more save than the name as I assume in this case the number won't change... – prom85 Aug 30 '15 at 20:51
  • I only said it because I spent several hours debugging a reflections issue a few years ago where a flag value changed on another library. Generally the value is much more likely to change than the variable name. Variable names are deprecated. I'm overly cautious though. Happy coding! – stewjacks Aug 30 '15 at 20:57
  • I understand. Appreciate that hint. Thanks – prom85 Aug 30 '15 at 20:59
  • Thanks @stewjacks, this solves also the question that I posted on SO one month ago, namely almost two years after you had posted your answer.. Here is my question, I copy the link in case someone else is trying to solve the same problem (or maybe has some suggestions): http://stackoverflow.com/questions/44226196/android-view-resize-and-windowmanager-unwanted-transition-during-child-view-pla Thanks! – Christopher23 Jun 12 '17 at 11:22
  • 1
    I did this but still there is a glitch which is visible from naked eyes did any one got workaround for that.. – silentsudo Sep 20 '17 at 14:50
  • This works but my device is getting turned off immediately when the app set this flag. – Nikola Minoski Oct 13 '17 at 23:13
  • 1
    @sector11, one possible solution - to call `removeView` and `addView` instead of `updateViewLayout` – Alexey Andronov Apr 03 '18 at 02:00