How do you make a Button visible only after an animated Textview has been fully written?

Asked

Viewed 197 times

1

In the protected void onCreate(Bundle savedInstanceState) of Mainactivity eu I serve the button so that it is invisible and also so that it 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 slowly (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 wish that only after the entire text had been written, did Button become visible.

I have no idea where to put in the code so that the button becomes visible again after Textview has been fully written, but I believe it is in Mainactivity.

I’ve thought about counting the number of characters to then compare, but it didn’t work (at least not the way I tried).

1 answer

2


Hello,

I don’t know if this is the best way....

Create a Runnable class attribute responsible for action when finishing the animation

private Runnable postAction = null;

Then create a Setter stops it

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

Then edit the characterAdder run method 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 on 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");
    }
  • Thank you very much!!! I have a question that involves this same class (Typewriter), but this is in relation to an audio that I would like to be played while Textview is running, do you think you could give me a help on it too? https://answall.com/questions/194084/howto reproduce um-som-somente-enquanto-um-textview-animado-est%C3%a1-being-reproduced

Browser other questions tagged

You are not signed in. Login or sign up in order to post.