ANIMATEDTEXT deleting all other components of the activity and how to do it in TextViews?

0

I can not get this animation to run in my TextViews, other than the same animation (animation) without any TextView or anything like that in my activity:

Look,eventhoughIaddcomponentstomyactivity,theyaddupatexecution:

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 in the following line = > public class TypeWriter extends TextView { , but nothing that prevents the application from running.

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 do so that the other components were not deleted and this animation was executed on all TextViews that were added to activity.

    
asked by anonymous 26.03.2017 / 20:13

1 answer

1

You used the setContentView() method by passing your animated TextView as parameter. You have to pass only the layout through the method, like this: 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 that I think is easiest to do this is to add the TextView normally and then go into your xml and change <TextView to <Typewriter , since if you already try to do with TypeWriter will only appear from the beginning android:layout_width="wrap_content" and android:layout_height="wrap_content" exactly like this:

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

This way you may have a hard time putting the TextView in the desired position, even more if you are using RelativeLayout. Adding with normal TextView and then swapping is much easier since you only need to switch from TextView for TypeWriter, having already done all its positioning and reference in relation to the other components.

    
26.03.2017 / 23:28