Repository: NativeScript/NativeScript
File: tns-core-modules/ui/core/view/view.android.ts (TypeScript)

Code before:
        this._refreshStateListAnimator();

    }

    _refreshStateListAnimator() {
        const nativeView: any = this.nativeViewProtected;
        // we have elevation or pressedZ
        if (this._originalStateListAnimator === undefined) {
            this._originalStateListAnimator = nativeView.getStateListAnimator();
        }
        const style = this.style;
        console.log("refreshing sla", this.elevation, this.androidPressedZ, this._originalStateListAnimator, elevationProperty.isSet(style), androidPressedZProperty.isSet(style));
        if ((!elevationProperty.isSet(style) && !androidPressedZProperty.isSet(style)) || // nothing to override
            (!androidPressedZProperty.isSet(style) && this._originalStateListAnimator === null)) { // we don't need pressedZ and there's no default
            // we have no reason to override anything, reset to default
            if (this._originalStateListAnimator !== nativeView.getStateListAnimator()) {
                nativeView.setStateListAnimator(this._originalStateListAnimator);
            }
            this._originalStateListAnimator = undefined;
            return;
        }
        const sla = new (android.animation as any).StateListAnimator();

        const ObjectAnimator = android.animation.ObjectAnimator;
        const AnimatorSet = android.animation.AnimatorSet;
        const shortAnimTime = android.R.integer.config_shortAnimTime;

        const buttonDuration =
            nativeView.getContext().getResources().getInteger(shortAnimTime) / 2;
        const pressedElevation = layout.toDevicePixels(this.elevation || 0);
        const pressedZ = layout.toDevicePixels(this.androidPressedZ || 0);
        const elevation = layout.toDevicePixels(this.elevation || 0);
        const z = layout.toDevicePixels(0);

        const pressedSet = new AnimatorSet();
        const notPressedSet = new AnimatorSet();
        const defaultSet = new AnimatorSet();

        pressedSet.playTogether(java.util.Arrays.asList([
            ObjectAnimator.ofFloat(nativeView, "translationZ", [pressedZ])
                .setDuration(buttonDuration),
            ObjectAnimator.ofFloat(nativeView, "elevation", [pressedElevation])
                .setDuration(0),
        ]));
        notPressedSet.playTogether(java.util.Arrays.asList([
            ObjectAnimator.ofFloat(nativeView, "translationZ", [z])
                .setDuration(buttonDuration),
            ObjectAnimator.ofFloat(nativeView, "elevation", [elevation])
                .setDuration(0),
        ]));
        defaultSet.playTogether(java.util.Arrays.asList([

Review comment (suggestion):
With respect to https://github.com/NativeScript/android-runtime/issues/1330 -- I don't think we really need to access android.R constants directly here -- we can store the necessary values as constants in TypeScript:
``` ts
const config_shortAnimTime = 17694720;
const state_pressed = 16842919;
const state_enabled = 16842910;
```

Code after:

    private refreshStateListAnimator() {
        const nativeView: any = this.nativeViewProtected;

        const ObjectAnimator = android.animation.ObjectAnimator;
        const AnimatorSet = android.animation.AnimatorSet;

        const duration = nativeView.getContext().getResources().getInteger(shortAnimTime) / 2;
        const elevation = layout.toDevicePixels(this.androidElevation || 0);
        const z = layout.toDevicePixels(0);
        const pressedZ = layout.toDevicePixels(this.androidDynamicElevationOffset || 0);

        const pressedSet = new AnimatorSet();
        pressedSet.playTogether(java.util.Arrays.asList([
            ObjectAnimator.ofFloat(nativeView, "translationZ", [pressedZ])
                .setDuration(duration),
            ObjectAnimator.ofFloat(nativeView, "elevation", [elevation])
                .setDuration(0),
        ]));

        const notPressedSet = new AnimatorSet();
        notPressedSet.playTogether(java.util.Arrays.asList([
            ObjectAnimator.ofFloat(nativeView, "translationZ", [z])
                .setDuration(duration),
            ObjectAnimator.ofFloat(nativeView, "elevation", [elevation])
                .setDuration(0),
        ]));

        const defaultSet = new AnimatorSet();
        defaultSet.playTogether(java.util.Arrays.asList([
            ObjectAnimator.ofFloat(nativeView, "translationZ", [0]).setDuration(0),
            ObjectAnimator.ofFloat(nativeView, "elevation", [0]).setDuration(0),
        ]));

        const stateListAnimator = new (<any>android.animation).StateListAnimator();
        stateListAnimator.addState([statePressed, stateEnabled], pressedSet);
        stateListAnimator.addState([stateEnabled], notPressedSet);
        stateListAnimator.addState([], defaultSet);
        nativeView.setStateListAnimator(stateListAnimator);
    }

    [horizontalAlignmentProperty.getDefault](): HorizontalAlignment {
        return <HorizontalAlignment>org.nativescript.widgets.ViewHelper.getHorizontalAlignment(this.nativeViewProtected);
    }
    [horizontalAlignmentProperty.setNative](value: HorizontalAlignment) {
        const nativeView = this.nativeViewProtected;
        const lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
        // Set only if params gravity exists.
        if (lp.gravity !== undefined) {
            switch (value) {
                case "left":