Search code examples
androidmarquee

How to set marquee behaviour in android to alternate programatically


This is the code I am using but the behaviour of the marquee is that is scrolls. How to make it to alternate i.e. it moves from right to left and then left to right.

public void setMarquee(TextView textView) {
        textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        textView.setMarqueeRepeatLimit(-1);
        textView.setHorizontallyScrolling(true);
        textView.setSelected(true);
        textView.requestFocus();

    }

Solution

  • Step1

    create folder called anim in res directory

    step 2

    create slide_left.xml

    slide_left.xml

        <translate
            android:duration="3000"
            android:fromXDelta="100%"
            android:toXDelta="0" />
    </set>
    

    step 3

    in main_activity.class

     LinearLayout mlayout =
         (LinearLayout)findViewById(R.id.mlayout); // the view u want to animate
         Animation slide_left_anim;
         slide_left_anim=
        AnimationUtils.loadAnimation(getActivity().getApplicationContext(),
                        R.anim.slide_left);
        slide_left_anim.setRepeatCount(Animation.INFINITE);
    
         mlayout.startAnimation(slide_left_anim);