Skip to main content

Vertical AutoScrolling TextView in Android

In android by default we can scroll the text in horizontal using marquee in layout, but if we want to scroll the text in vertical its not possible by default.
So here we will learn to create a custom TextView which will auto-scroll in vertical direction.
Source Code: VerticalScrollingTextView-Android

Create a AutoScrollingTextView.class which extends TextView:


@SuppressLint("AppCompatCustomView")
public class AutoScrollingTextView extends TextView {

    private static final float DEFAULT_SPEED = 65.0f;
    public Scroller scroller;    
    public float speed = DEFAULT_SPEED;    
    public boolean continuousScrolling = true;
    public AutoScrollingTextView(Context context) {
        super(context);        
        init(null, 0);        
        scrollerInstance(context);    
    }

    public AutoScrollingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);        
        init(attrs, 0);        
        scrollerInstance(context);    
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public AutoScrollingTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, 
                                                              int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);        
        init(attrs, defStyleAttr);        
        scrollerInstance(context);    
    }

    private void init(AttributeSet attrs, int defStyleAttr) {
        TypedArray attrArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyTextView, 
                                                               defStyleAttr, 0);        
        initAttributes(attrArray);    
    }

    protected void initAttributes(TypedArray attrArray) {
        String textStyle = attrArray.getString(R.styleable.MyTextView_myTextStyle);
        if (textStyle == null || textStyle.equals(null) || textStyle.equals("")) {

        } else {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), textStyle);            
            setTypeface(tf);        
        }

    }

    public void scrollerInstance(Context context) {
        scroller = new Scroller(context, new LinearInterpolator());        
        setScroller(scroller);    
    }

    @Override    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (scroller.isFinished()) {
            scroll();
        }
    }

    public void scroll() {
        int viewHeight = getHeight();
        int visibleHeight = viewHeight - getPaddingBottom() - getPaddingTop();        
        int lineHeight = getLineHeight();
        int offset = -1 * visibleHeight;
        int distance = visibleHeight + getLineCount() * lineHeight;        
        int duration = (int) (distance * speed);
        scroller.startScroll(0, offset, 0, distance, duration);
    }

    @Override    
    public void computeScroll() {
        super.computeScroll();
        if (null == scroller)
            return;
        if (scroller.isFinished() && continuousScrolling) {
            scroll();        
        }
    }

    @Override    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (null == scroller)
            return;
        if (scroller.isFinished() && continuousScrolling) {
            scroll();        
        }
    }

    public void setSpeed(float speed) {
        this.speed = speed;    
    }

    public float getSpeed() {
        return speed;    
    }

    public void setContinuousScrolling(boolean continuousScrolling) {
        this.continuousScrolling = continuousScrolling;    
    }

    public boolean isContinuousScrolling() {
        return continuousScrolling;
    }
}


For detail on how to add attrs which is used in above class follow this link Custom FontTextView in Android.

Now add the AutoScrollingTextView in layout:

<com.AutoScrollingTextView    
    android:id="@+id/tvContent"    
    android:layout_width="match_parent"    
    android:layout_height="wrap_content"    
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"    
    android:maxLines="3"    
    android:scrollHorizontally="false"
    android:scrollbars="vertical"    
    android:text="@string/lipsum"    
    android:textAlignment="inherit"    
    android:textColor="@android:color/white"    
    android:textSize="@dimen/twenty" />

In MyActivity.class:

AutoScrollingTextView tvContent = (AutoScrollingTextView) findViewById(R.id.tvContent);
tvContent.setMovementMethod(new ScrollingMovementMethod());
tvContent.scroll();

Happy coding!!!

 

Comments

  1. Great i was searching for this Thanks

    ReplyDelete
  2. can you please provide me the full code of this...i am searching it from past 15 days . my id is sethashim49@gmail.com

    ReplyDelete
    Replies
    1. Hi, i have added code in github you can download from here https://github.com/yuvaraj119/VerticalScrollingTextView-Android

      Delete
  3. Replies
    1. In this code you cant make it scrollable because marqueeRepeatLimit is used, but you can use normal scrollview with textview inside it to make it scrollable.

      Delete
  4. My google is not working mobile nee updation how to update an app

    ReplyDelete

Post a Comment

Popular posts from this blog

Flexbox inside the RecyclerView as a LayoutManager (FlexboxLayoutManager).

Currently google has release the Flexbox which can be used for building flexible layouts using FlexboxLayout, it can be interpreted as an advanced LinearLayout because both layouts align their child views sequentially. For more detail on this flexbox-layout But here we are gonna work on Flexbox with RecyclerView. Flexbox with a large number of items in a scrollable container! Let's first see what are the Supported attributes / features comparison Due to some characteristics of the RecyclerView, some Flexbox attributes are not available/not implemented to the FlexboxLayoutManager. Here is a quick overview of the attributes/features comparison between the two containers. Attribute / Feature FlexboxLayout                FlexboxLayoutManager (RecyclerView) flexDirection flexWrap (except wrap_reverse ) justifyContent alignItems alignContent - layout_order - layout_fle...

Android Customized-FloatSeekBar

A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged. SeekBar support only int values for progress: void setMax(int max) Set the upper range of the progress bar max. void setMin(int min) Set the lower range of the progress bar to min. void setProgress(int progress) Sets the current progress to the specified value. int getMax() Return the upper limit of this progress bar's range. int getMin() Return the lower limit of this progress bar's range. int getProgress() Get the progress bar's current level of progress. In some scenario we would need floating point seekbar progress, which is not supported by default in android. Here the floating seekbar which supports floating point value: You can download the full sample and library from https://github.com/yuvaraj119/Cus...