android_guy
android_guy

Reputation: 162

any working way for vertical scrolling of text automatically in view android java

I know that this topic is asked many times on this website but all the responses so far haven't been very helpful to me.

what i am trying is to vertical scroll the text automatically from example i have to scroll the text from strings and the text should be directly on the relative layout from topmargin = 2 to bottommargin = 20 and then keeps looping once the all text is shown from the strings

any working way ?

you can see this video what i am looking for

https://www.youtube.com/watch?v=dL3lp5_N4VA

i didn't found where to start properly because none works now i am thinking if anybody have working code for this

thanks

Upvotes: 1

Views: 108

Answers (1)

Cory Charlton
Cory Charlton

Reputation: 8938

I did exactly this for a Christmas present for my daughter. My solution was to put the text in a ScrollView and programmatically scroll with a Runnable that reschedules itself if there is still more content to scroll.

Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/background"
        android:src="@drawable/background"
        android:scaleType="centerCrop"
        />

    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            <Space
                android:layout_width="match_parent"
                android:layout_height="@dimen/leading_space"
                tools:layout_height="1dp"
                />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="@dimen/activity_horizontal_margin"
                android:layout_marginStart="@dimen/activity_horizontal_margin"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                android:orientation="vertical"
                >

                <TextView
                    android:id="@+id/textview_a_long_time_ago"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/trailing_space"
                    android:layout_gravity="center_horizontal"
                    android:text="@string/a_long_time_ago"
                    android:textStyle="bold"
                    android:lineSpacingMultiplier="1.1"
                    style="@style/Text"
                    />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/episode_2015"
                    style="@style/Text.Episode"
                    />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/merry_christmas"
                    style="@style/Text.Title"
                    />

            </LinearLayout>

            <TextView
                android:id="@+id/textview_crawl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/activity_vertical_margin"
                android:layout_marginEnd="@dimen/activity_horizontal_margin"
                android:layout_marginStart="@dimen/activity_horizontal_margin"
                android:text="@string/opening_text"
                android:textStyle="bold"
                android:lineSpacingMultiplier="1.1"
                style="@style/Text"
                />

            <!--
            <Space
                android:layout_width="match_parent"
                android:layout_height="@dimen/trailing_space"
                tools:layout_height="1dp"
                />
                -->

            <RelativeLayout
                android:id="@+id/layout_next"
                android:layout_width="match_parent"
                android:layout_height="@dimen/leading_space"
                android:background="?attr/selectableItemBackground"
                >

                <ImageView
                    android:id="@+id/imageview_lightsabers"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:contentDescription="@string/lightsaber"
                    android:scaleType="centerInside"
                    android:src="@drawable/lightsabers"
                    />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:paddingBottom="@dimen/activity_vertical_margin"
                    android:paddingEnd="@dimen/activity_horizontal_margin"
                    android:paddingStart="@dimen/activity_horizontal_margin"
                    android:paddingTop="@dimen/activity_vertical_margin"
                    android:text="@string/click_to_continue"
                    android:textAllCaps="true"
                    style="@style/Text.Title"
                    />
            </RelativeLayout>
        </LinearLayout>

    </ScrollView>

</RelativeLayout>

Code snippet from my Activity:

private ScrollView _scrollView;

private Runnable _smoothScrollRunnable = new Runnable() {
    @Override
    public void run() {
        _scrollView.smoothScrollBy(2, 2);

        if (_scrollView.canScrollVertically(1)) {
            _scrollView.postDelayed(_smoothScrollRunnable, 75);
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    _scrollView = (ScrollView) findViewById(R.id.scrollview);
}

@Override
protected void onResume() {
    super.onResume();

    _scrollView.post(_smoothScrollRunnable);
}

Result (video quality sucks and it wasn't finished yet but you get the idea):

https://www.youtube.com/watch?v=TfKxGraLNLA

Edit:

Response to the comment:

hi sir i liked you way it works when the text is more than the screen ,can is this be implemented something like to scroll the text till some height and then back like up and down in loop till some width ?

Sure it could be modified to do that. Check out the ScrollView.canScrollVertically method:

public boolean canScrollVertically (int direction)

Check if this view can be scrolled vertically in a certain direction.

Parameters

direction - Negative to check scrolling up, positive to check scrolling down.

Returns

boolean - true if this view can be scrolled in the specified direction, false otherwise.

And the ScrollView.smoothScrollBy method:

public final void smoothScrollBy (int dx, int dy)

Like scrollBy(int, int), but scroll smoothly instead of immediately.

Parameters

dx - the number of pixels to scroll by on the X axis

dy - the number of pixels to scroll by on the Y axis

So if you modified the code snippet to remember which direction you were scrolling (up or down) and then flip it when you couldn't go any further you would achieve what you are looking for.

Edit:

Quick, incomplete, code snippet to explain what I'm talking about:

private boolean isScrollingDown = true;
private Runnable _smoothScrollRunnable = new Runnable() {
    @Override
    public void run() {
        if (isScrollingDown && !_scrollView.canScrollVertically(1)) {
            // We can't scroll down any further so start scrolling up ...
            isScrollingDown = false;
        } else if (!isScrollingDown && !_scrollView.canScrollVertically(-1) {
            // We can't scroll up any further so start scrolling down ...
            isScrollingDown = true;
        }

        if (isScrollingDown) {
            // Scroll down by 2
            _scrollView.smoothScrollBy(0, 2);
        } else {
            // Scroll up by 2
            _scrollView.smoothScrollBy(0, -2);          
        }

        // Just keep scrolling forever ...
        _scrollView.postDelayed(_smoothScrollRunnable, 75);
    }
};

Upvotes: 1

Related Questions