ANIMATEDTEXT eliminating all other Activity Components and how to make it run in Textviews?

Asked

Viewed 33 times

0

I can’t get this Animation to run in my Textviews, and the same (animation) runs without any Textview or anything like that in my Activity:

inserir a descrição da imagem aqui

See, even if I add Components to my Activity, they disappear in execution:

inserir a descrição da imagem aqui

Typewriter.class:

package genesysgeneration.animatedtext;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.TextView;

public class TypeWriter extends TextView {

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

    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 animateText(CharSequence text){

        mText=text;
        mIndex=0;

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

    }

    public void setCharacterDelay(long millis){

        mDelay=millis;

    }

}

An error appears on the following line => public class TypeWriter extends TextView {, but nothing to prevent the implementation of the.

Mainactivity:

package genesysgeneration.animatedtext;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

        TypeWriter writer = new TypeWriter(this);
        setContentView(writer);

        writer.setCharacterDelay(150);
        writer.animateText("(nova execução) Pirulito ki bate bate, pirulito ki jah bateu!!!");

    }
}

I would like to know how to make sure that other Components are not deleted and that animation is executed in all Textviews that are added to Activity.

1 answer

1


You used the method setContentView() passing as parameter your animated Textview. You have to pass only the layout through the method, this way: setContentView(R.layout.activity_main); and instead of instantiating the animated Textview the way you are doing, you should do so:

Typewriter txtAnimado = (Typewriter) findViewById(R.id.meu_txt_animado);

In xml you must type <Typewriter that Android Studio completes the rest for you:

<pacote.animatedtext.TypeWriter
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />

One way I think easier to do this, is to add Textview normally and then go in your xml and exchange <TextView for <Typewriter, because if you already try to do with Typewriter will only appear in the beginning android:layout_width="wrap_content" and android:layout_height="wrap_content" just like that:

<pacote.animatedtext.TypeWriter
        android:layout_width=""
        android:layout_height="" />

This way you may have difficulty putting Textview in the desired position, even more if you are using Relativelayout. Adding with normal Textview and then switching is much easier, as you only need to switch from Textview to Typewriter, having already made all its positioning and reference in relation to other Textview.

  • fixed setContext, but an error occurred while inserting textView. it did not accept my textView as Typewriter TypeWriter writer = (TypeWriter)findViewById(R.id.tv); accused a mistake.

  • So in xml this view whose id is "tv" is not type Typewriter...

  • just type <Typewriter ... that Android studio will auto-complete for you with the correct import packages.

Browser other questions tagged

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