How can I change letters' color in a custom TextView control well in android?

Gasta87

I'm following an e-book of android first developing; in a certain point the goal was to change color of each letter cyclically.

the example is written in pieces and it's not clear for me how to glue them well.

NOTE: I inserted the definition of selfchange, in the book it wasn't put.

So where are the errors? in this way my app crash. Should I put @Override over the onTextChanged method?

I wrote like this:

public class CustomTextView extends TextView
{
    public CustomTextView(Context context,AttributeSet attributeSet,int defSytle)
    {
        super(context, attributeSet, defSytle);
    }

    public CustomTextView(Context context,AttributeSet attributeSet)
    {
        super(context, attributeSet);
    }

    public CustomTextView(Context context) 
    {
        super(context);
        // TODO Auto-generated constructor stub
    }

    protected void onTextChanged(CharSequence text,
    int start, int lengthBefore, int lengthAfter)
    {
        if(selfChange)
        {
            selfChange = false;
            return;
        }
        selfChange=true;

        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        builder.clearSpans();
        ForegroundColorSpan colorSpan;
        int color;
        for(int i=0; i < text.length(); i++)
        {
            //pick the next color
            color = colorArray[i%colorArray.length];
            //Create the color span
            colorSpan = new ForegroundColorSpan(color);
            //Add the color span for this one char
            builder.setSpan(colorSpan,
            i, i,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        setText(builder);
    }

    boolean selfChange = false;

    int colorArray[] = new int[]
    {
        Color.WHITE,
        Color.RED,
        Color.YELLOW,
        Color.GREEN,
        Color.BLUE,
        Color.MAGENTA,
        Color.CYAN,
        Color.DKGRAY
    };
}

LogCat:

01-04 00:17:34.560: D/AndroidRuntime(3149): Shutting down VM
01-04 00:17:34.560: W/dalvikvm(3149): threadid=1: thread exiting with uncaught exception (group=0x40fbe2a0)
01-04 00:17:34.575: E/AndroidRuntime(3149): FATAL EXCEPTION: main
01-04 00:17:34.575: E/AndroidRuntime(3149): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lux.myproject/com.lux.myproject.MainActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class com.lux.myproject.CustomTextView
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.os.Looper.loop(Looper.java:137)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.ActivityThread.main(ActivityThread.java:4898)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at java.lang.reflect.Method.invokeNative(Native Method)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at java.lang.reflect.Method.invoke(Method.java:511)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at dalvik.system.NativeStart.main(Native Method)
01-04 00:17:34.575: E/AndroidRuntime(3149): Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class com.lux.myproject.CustomTextView
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.view.LayoutInflater.createView(LayoutInflater.java:613)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.Activity.setContentView(Activity.java:1924)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at com.lux.myproject.MainActivity.onCreate(MainActivity.java:53)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.Activity.performCreate(Activity.java:5206)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
01-04 00:17:34.575: E/AndroidRuntime(3149):     ... 11 more
01-04 00:17:34.575: E/AndroidRuntime(3149): Caused by: java.lang.reflect.InvocationTargetException
01-04 00:17:34.575: E/AndroidRuntime(3149):     at java.lang.reflect.Constructor.constructNative(Native Method)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.view.LayoutInflater.createView(LayoutInflater.java:587)
01-04 00:17:34.575: E/AndroidRuntime(3149):     ... 22 more
01-04 00:17:34.575: E/AndroidRuntime(3149): Caused by: java.lang.NullPointerException
01-04 00:17:34.575: E/AndroidRuntime(3149):     at com.lux.myproject.CustomTextView.onTextChanged(CustomTextView.java:46)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.widget.TextView.setText(TextView.java:3723)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.widget.TextView.setText(TextView.java:3577)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.widget.TextView.<init>(TextView.java:1224)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.widget.TextView.<init>(TextView.java:566)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at com.lux.myproject.CustomTextView.<init>(CustomTextView.java:20)
01-04 00:17:34.575: E/AndroidRuntime(3149):     ... 25 more
Carlo Moretto
protected void onTextChanged(CharSequence text,
    int start, int lengthBefore, int lengthAfter)
{
    if (text.lenght > 0)
    //YOUR CODE
}

Try to check the lenght of the text

screen.setText is breaking on use. uncaught expection (group=0xb3a539@0) (Android)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to change color of textView of custom griditem in android

How can I change letters’ color?

How can I change the bootstrap's well color?

custom TextView that can change background color

Android: How can I change AlertDialog Title Text Color and Background Color without using custom layout?

How can I change color of bootstrap progress bar with custom color

How can I change the background color of the Android tab with a renderer, while using a custom renderer to add some padding

How can i achieve this custom Shape in android for a textview?

How can I identify the color of the letters in these images?

Android: how can change the text color in custom view?

How to change TextView color with ViewBinding? Android/Kotlin

Android - Can I change the reference of a TextView with a button?

How can I change the color of android:homeAsUpIndicator in theme

How can I change color text of status bar in Android?

How can I change the color of the android progressbar(horizontal) to different sections?

How can i change the background color in a BadgeDrawable Android

How can I change Material DateRangePicker color in android?

How can I change default elements color android layout?

how can i change highlight color in mupdf library android

Can I change the color of Angular Material checkbox with some custom color and how?

How can I change the mouse over color of a Button having a control template applied?

How can i change asp.net chart control series color?

How can I color specific letters in html element text?

How do I change color of a certain part of my textview?

How to control color of letters in geom_text?

how to change color of textview hyperlink?

how can i change the EditText cursor knob color android (not the cursor color the knob)

How can I center a TextView on Android?

How can I change property control by code?