Here is my DECISION
To make the long text inside the text view not allowed by the parent view or screen, I did two things.
First, let textview inside scroolview as below code
<ScrollView android:layout_width="wrap_content" android:layout_height="50dp" android:layout_centerHorizontal="true"> <TextView android:id="@+id/marquee_text" android:layout_width="wrap_content" android:layout_height="50dp" android:maxLines="1" android:textColor="@android:color/black" android:textSize="30sp"/> </ScrollView>
Then I measure my text size and then refine the textview parameter by doing this.
marqueeText.setText("my long text"); Paint textPaint = marqueeText.getPaint(); String text = marqueeText.getText().toString();//get text int width = Math.round(textPaint.measureText(text));//measure the text size ViewGroup.LayoutParams params = marqueeText.getLayoutParams(); params.width = width; marqueeText.setLayoutParams(params); //refine DisplayMetrics displaymetrics = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics); int screenWidth = displaymetrics.widthPixels; //this is optional. do not scroll if text is shorter than screen width //remove this won't effect the scroll if (width <= screenWidth) { //All text can fit in screen. return; } //set the animation TranslateAnimation slide = new TranslateAnimation(0, -width, 0, 0); slide.setDuration(20000); slide.setRepeatCount(Animation.INFINITE); slide.setRepeatMode(Animation.RESTART); slide.setInterpolator(new LinearInterpolator()); marqueeText.startAnimation(slide);
I hope this solution, which took me half a day to research, can help others who may encounter the same problem as me.
source share