4

I am animating fragment transitions. I want to provide a slide in from ride animation, but I can't find it programmatically accessing android.R.anim
Only a few animations are present in there, while exploring the source code I found this folder frameworks/base/core/res/res/anim that has dozens of animations, including slide_in_right.xml

How can I access it from code? Is there a better way than copy/paste it in my res/anim folder?

TeamBanana
  • 179
  • 1
  • 10
  • 1
    If you can't access it via `android.R.something` just copy the xml and use it locally – zapl Dec 08 '12 at 20:33
  • You should be able to reference it with android.R.anim.slide_in_right. What sdk version are you targeting? – Chuck D Dec 08 '12 at 20:50
  • i'm targeting 17, with min sdk version 14 – TeamBanana Dec 08 '12 at 21:49
  • Per "just copy the XML file" -- see http://stackoverflow.com/a/14295285/3063884 for instructions. – CJBS Sep 03 '14 at 21:15
  • Explanation as to why they don't work here: https://stackoverflow.com/questions/62923422/missing-slide-in-right-and-slide-out-left-animations-in-android/75881117#75881117 – phazei Mar 29 '23 at 18:57

3 Answers3

3

That's not the answer to the question.android.R.anim.slide_out_right is defined elsewhere and is accessible from R.anim. Slide_in_right is not accessible from this package, hence the need to use the xml file definition

I managed to access the animation by copying the files in frameworks/base/core/res/res/anim/slide_in_right.xml to res/anim/slide_in_right.xml and used the code:

AnimationUtils.loadAnimation(this, R.anim.slide_in_right);

to load the animation.

The full code to achieve an onTouchListener that allows the user to slide an image left to veiw next and slide an image right to view previous is shown below

    /**
 * @return
 */
protected OnTouchListener createOnTouch() {
    return new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if ((event.getAction() == MotionEvent.ACTION_DOWN) || (event.getAction() == MotionEvent.ACTION_POINTER_DOWN)) {
                downX = (int) event.getX();
                Log.i("event.getX()", " downX " + downX);
                return true;
            } else if ((event.getAction() == MotionEvent.ACTION_UP) || (event.getAction() == MotionEvent.ACTION_POINTER_UP)) {
                upX = (int) event.getX();
                Log.i("event.getX()", " upX " + upX);
                Log.d("event.getX()", " upX-downX " + (upX - downX));
                Log.d("event.getX()", " downX-upX " + (downX - upX));
                if (upX - downX > 100) {
                    imageSwitcher1.setInAnimation(AnimationUtils.loadAnimation(ImageDisplayActivity.this, android.R.anim.slide_in_left));
                    imageSwitcher1.setOutAnimation(AnimationUtils.loadAnimation(ImageDisplayActivity.this, android.R.anim.slide_out_right));

                    int newPosition = selected - 1;
                    showDetails(newPosition, gallery.getChildAt(newPosition));
                    // ImageDisplayActivity.this.switchTitle(curIndex);
                } else if (downX - upX > 100) {
                    imageSwitcher1.setInAnimation(AnimationUtils.loadAnimation(ImageDisplayActivity.this, R.anim.slide_in_right));
                    imageSwitcher1.setOutAnimation(AnimationUtils.loadAnimation(ImageDisplayActivity.this, R.anim.slide_out_left));

                    int newPosition = selected + 1;
                    showDetails(newPosition, gallery.getChildAt(newPosition));

                } else {
                    return false;
                }
                return true;
            }
            return false;
        }
    };
}
Lettings Mall
  • 300
  • 3
  • 10
0

You can get predefined animation xml using code below,

Animation animation;

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   animation = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);
Talha
  • 12,673
  • 5
  • 49
  • 68
0

Also this works too

 <action
        android:id="@+id/action_fragment_main_to_favoriteFragment"
        app:destination="@id/favoriteFragment"
        app:enterAnim="@android:anim/fade_in"
        app:exitAnim="@android:anim/fade_out"
        app:popEnterAnim="@android:anim/slide_in_left"
        app:popExitAnim="@android:anim/slide_out_right" />
harun karaca
  • 134
  • 5