Kotlin TextView Blink (KTB)

Hi guys, just sharing some fun, at that time I was practicing textview and just wanted to know if the way to make textview blink on Android is as easy as doing it for the web? OMG it turns out not guys, it's quite complicated, even though the case is very simple when viewed from a web developer's perspective. Okay, how complicated? see the explanation below.

<TextView
   android:id="@+id/usage"
   android:layout_marginTop="220dip"
   android:layout_marginLeft="45dip"
   android:layout_marginRight="15dip"
   android:typeface="serif"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Google "
   android:textColor="#030900"/>

Then I want to make it look like it's blinking, so we just hide the TextView component and then display it again in a certain time span, now to do this routine we use a service called "Handler", then assisted by "Thread" to create the interval. More or less like this.

package teste.blink;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

public class TesteBlinkActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        blink();
}

private void blink(){
    final Handler handler = new Handler();
    new Thread(new Runnable() {
        @Override
        public void run() {
        int timeToBlink = 1000;    //in milissegunds
        try{Thread.sleep(timeToBlink);}catch (Exception e) {}
            handler.post(new Runnable() {
                @Override
                    public void run() {
                    TextView txt = (TextView) findViewById(R.id.usage);
                    if(txt.getVisibility() == View.VISIBLE){
                        txt.setVisibility(View.INVISIBLE);
                    }else{
                        txt.setVisibility(View.VISIBLE);
                    }
                    blink();
                }
                });
            }
        }).start();
    }

Please compile and see the results, good luck!


Post a Comment

Previous Next

نموذج الاتصال