Jack Shultz Jack Shultz - 26 days ago 12
Android Question

Trouble with Android character by character display text animation

I'm following this solution
Android character by character display text animation

I get unresolved method for

setContentView


Here is the Typewriter Class

public class Typewriter extends TextView {


private CharSequence mText;
private int mIndex;
private long mDelay = 500; //Default 500ms delay


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;
}
}


And then I try to use it

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

//Add a character every 150ms
writer.setCharacterDelay(150);
writer.animateText("Sample String");


enter image description here

Also I tried adding this custom layout

<com.mayday.md.common.Typewriter
android:id="@+id/contact_popup"
style="@style/wizard_intro_style"
android:background="#FFFFFF"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.24"
android:text=""
android:padding="1dp"
android:paddingLeft="1dp"
android:paddingTop="1dp"
android:paddingRight="1dp"
android:paddingBottom="1dp"
android:lines="2"
android:layout_above="@+id/ll_contact_1"
android:layout_centerHorizontal="true"
android:width="150dp" />

Answer

You are using it in a wrong way. To make your textview like in example do this.

Typewriter contact_popup = (Typewriter) view.findViewById(R.id.contact_popup);
contact_popup.setCharacterDelay(150);
contact_popup.animateText("Sample String");

return view;