How to print a string in a textview letter by letter in java?

1

I want to print a string that I go through with a for loop but I do not know how to pause and go back through the loop keeping what is already written, I want to give the effect as if the message was being written at the moment, I have tried with the following code but waits for the total time and prints it at once. Greetings and thanks!

    private void bucle() {
        String saludo = "Hola chavales";
        int pausa = 100;

        for (int i = 1; i <= saludo.length(); i++) {
            try {
            tpantalla.setText(modelSplash.toString().substring(0, i));
                Thread.sleep(pausa);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
    
asked by Alex 20.06.2018 в 17:56
source

3 answers

0

You can use the following library also

link

Among its settings you can modify the time, the "sound" (as a typewriter) plus any attribute of a normal textview.

<in.codeshuffle.typewriterview.TypeWriterView
   android:id="@+id/typeWriterView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textStyle="bold" />     

TypeWriterView typeWriterView=(TypeWriterView)findViewById(R.id.typeWriterView);
typeWriterView.setDelay(int);
typeWriterView.setWithMusic(boolean);
typeWriterView.animateText(string);     
    
answered by 25.06.2018 в 16:58
0

One option is to use a Handler to write each letter of the string at a defined time, in this case as a half second example (500 ms):

    final long DELAY = 500;
    final TextView textView = (TextView) findViewById(R.id.textView);

    final Handler handler = new Handler();
    Timer timer = new Timer();

    TimerTask task = new TimerTask() {
        int counter = 0;

        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    try {

                        //Write every letter in CharSequence
                        textView.setText(myPoem.subSequence(0, counter));
                        counter++;

                    } catch (Exception e) {

                    }
                }
            });
        }
    };
    timer.schedule(task, 0, DELAY);  //DELAY every n milliseconds

Another option is if you prefer to use a custom TextView ,

import android.support.v7.widget.AppCompatTextView;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;

public class TypeWriter extends AppCompatTextView {

    private CharSequence mText;
    private CharSequence mOriginalText;
    private long delay = 1000; // 1 second
    private int mIndex;

    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, delay);
            }else{
                mIndex = 0;
                mText = mOriginalText;
                setText("");
            }
        }
    };

    public void animateText(CharSequence text) {
        setText("");
        mText = text;
        mOriginalText = text;
        mIndex = 0;
        mHandler.removeCallbacks(characterAdder);
        mHandler.postDelayed(characterAdder, delay);
    }

    //*Overrides delay value, default is 1 second.
    public void setCharacterDelay(long millis) {
        delay = millis;
    }
}

for this in the layout you would change the definition of TextView of

<TextView

a

 <[Paquete de aplicación].TypeWriter

With both options you can create something like this:

The% custom TextView is what is done in libraries such as TypeWriter

    
answered by 25.06.2018 в 15:58
-1

One of the ways to do this is with a thread inside the for loop, where the thread with the .sleep method, of the Thread class allows you to give it the time you want to delay doing the action, in this case iterating the in the loop, the time received by the .sleep () method is given in milliseconds, so if you want it to last a second, it places 1000 milliseconds here, leaving the code as it would be

package views;

import java.awt.BorderLayout;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class MainWindow extends JDialog{

    private static final long serialVersionUID = 1L;
    private JLabel lbText;

    public MainWindow() {
        lbText = new JLabel();
        setSize(400, 500);

        add(lbText, BorderLayout.CENTER);
        setVisible(true);
    }

    public void setText(String text) {
        try {
            for (int i = 1; i <= text.length(); i++) {
                Thread.sleep(1000);
                lbText.setText(text.substring(0, i));
                repaint();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        MainWindow mainWindow = new MainWindow();
        mainWindow.setText(JOptionPane.showInputDialog("ingrese el texto"));
    }
}
    
answered by 22.06.2018 в 03:17