How do I make a Button visible only after an animated TextView has been completely typed?

1

Protected void onCreate (Bundle savedInstanceState) from MainActivity I set the button so that it is invisible and also does not occupy space with the following code: button.setVisibility(View.GONE);

MainActivity :

package genesysgeneration.stackx;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private Button button;

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

        button=(Button)findViewById(R.id.button);
        button.setVisibility(View.GONE);

        TypeWriter textView=(TypeWriter)findViewById(R.id.textView);
        textView.setCharacterDelay(1);
        textView.animatedText("lansdfçaksjfçkajsçfljkasçlfkaçslkgçlaksgçlaksglçakçlgmnasçkdnbklanslkjbnlkasnbklanslkbnalksbnlaksn");

    }
}

I use the following class (TypeWriter) so that TextView is written bit by bit (hence the term Animated TextView I used).

TypeWriter :

public class TypeWriter extends TextView{

    private CharSequence mText;
    private int mIndex;
    private long mDelay = 1;

    public TypeWriter(Context context){

        super(context);

    }

    public TypeWriter(Context context, AttributeSet attrs){

        super(context, attrs);

    }

    private Handler mHandler = new Handler();
    private Runnable characterAdder = new Runnable() {
        @Override
        public void run() {
            setText(mText.subSequence(0, mIndex++));
            if (mIndex<=mText.length()){

                mHandler.postDelayed(characterAdder, mDelay);

            }
        }
    };

    public void animatedText(CharSequence text){

        mText=text;
        mIndex=0;

        setText("");
        mHandler.removeCallbacks(characterAdder);
        mHandler.postDelayed(characterAdder, mDelay);

    }

    public void setCharacterDelay(long millis){

        mDelay=millis;

    }

}

XML :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="genesysgeneration.stackx.MainActivity">

    <genesysgeneration.stackx.TypeWriter
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView"
        android:text="Button" />
</RelativeLayout>

I would like it to be visible only after all the text has been written .

I have no idea where to put it in the code so that the button becomes visible again after TextView has been fully written, but I think it's in MainActivity .

I've thought about counting the number of characters to compare, but it did not work (at least not the way I tried).

    
asked by anonymous 15.05.2017 / 21:01

1 answer

2

Hello,

I do not know if this is the best way to do it.

Create a Runnable class attribute responsible for the action at the end of the animation

private Runnable postAction = null;

Then create a setter for it

public void setPostAction(Runnable postAction) {
    this.postAction = postAction;
}

Then edit the run method of the characterAdder to:

public void run() {
            setText(mText.subSequence(0, mIndex++));
            if (mIndex<=mText.length()){

                mHandler.postDelayed(characterAdder, mDelay);

            }else{
                if (postAction != null){
                    postAction.run();
                }
            }
        }

Finally edit in mainActivity:

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

        button=(Button)findViewById(R.id.button);
        button.setVisibility(View.GONE);

        TypeWriter textView=(TypeWriter)findViewById(R.id.textView);
        textView.setCharacterDelay(1);
        textView.setPostAction(new Runnable(){
            @Override
            public void run() {
                button.setVisibility(View.VISIBLE);
            }
        });
        textView.animatedText("lansdfçaksjfçkajsçfljkasçlfkaçslkgçlaksgçlaksglçakçlgmnasçkdnbklanslkjbnlkasnbklanslkbnalksbnlaksn");
    }
    
15.05.2017 / 22:57