0

i have this code for creating a marquee, can i change the speed of it and its start position when activated? I have these codes written and when i click the button it starts the marquee on the center.

<TextView
    android:id="@+id/mywidget"
    android:layout_width="fill_parent"
    android:textSize="20dp"
    android:layout_height="wrap_content"
    android:text="This is a test of marquee on the text view in android."
    android:ellipsize="marquee"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:focusable="false"
    android:marqueeRepeatLimit="marquee_forever"
    android:textStyle="bold"
    android:shadowColor="#f5067e"
    android:textColor="#ff000d"
    android:shadowDx="0.0"
    android:shadowDy="0.0"
    android:shadowRadius="8"
    />

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.mywidget);
    textView.setVisibility(View.INVISIBLE);


    anim = (Button) findViewById(R.id.anim);
    anim.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textView.setVisibility(View.VISIBLE);
            textView.setSelected(true);
        }
    });
}
Ralph
  • 550
  • 3
  • 10
  • 25

1 Answers1

2

I'm also looking to change the marquee speed and start position. I found the solution for speed. Original post - Marquee Set Speed

Make sure that you call this method only after tv.setText() and tv.setSelected(true). Otherwise it will not work.

public static void setMarqueeSpeed(TextView tv, float speed) {
    if (tv != null) {
        try {
            Field f = null;
            if (tv instanceof AppCompatTextView) {
                f = tv.getClass().getSuperclass().getDeclaredField("mMarquee");
            } else {
                f = tv.getClass().getDeclaredField("mMarquee");
            }
            if (f != null) {
                f.setAccessible(true);
                Object marquee = f.get(tv);
                if (marquee != null) {
                    String scrollSpeedFieldName = "mScrollUnit";
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        scrollSpeedFieldName = "mPixelsPerSecond";
                    }
                    Field mf = marquee.getClass().getDeclaredField(scrollSpeedFieldName);
                    mf.setAccessible(true);
                    mf.setFloat(marquee, speed);
                }
            } else {
                Logger.e("Marquee", "mMarquee object is null.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Community
  • 1
  • 1
Hitesh Gupta
  • 1,091
  • 9
  • 14