skip to Main Content

I’m trying to make a TextView blink while the user is in the Main Activity.

I created a new thread but what happens is that the TextView’s visibility turns on, than off and then the app closes itself with Android saying that the app stopped working.
Seems like the for loop executes ones and then the whole application is interrupted.

Here’s a code snippet

    Thread blinkThread = new Thread(){
        @Override
        public void run() {
            for(int i = 0; i < 10; i++){
                lastletter.setVisibility(VISIBLE);
                try {
                    sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                lastletter.setVisibility(View.INVISIBLE);
                try {
                    sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    };

The code above code is inside the MainActivity class.
The thread starts in the onCreate() method, I basically wrote blinkThread.start()

2

Answers


  1. Try to use below code:

    Code:

    private void blinkCall() {
        final Handler handler = new Handler();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int timeToBlink = 1000;
                    try {
                        Thread.sleep(timeToBlink);
                    } catch (Exception e) {
                    }
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
    
                            if (textView.getText().toString().equals("")) {
                                textView.setText("Abhishek");
                            } else {
                                textView.setText("");
                            }
                            blinkCall();
                        }
                    });
                }
            }).start();
    }
    
    Login or Signup to reply.
  2. In android, manipulating views from other threads than the main thread throws an exception.

    You need to make a handler for the main thread.

    private final Handler mainHandler = new Handler(getMainLooper());
    

    and post runnables to it.

        Thread blinkThread = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    // the main thread will handle this
                    mainHandler.post(() -> lastletter.setVisibility(View.VISIBLE));
                    
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
    
                    mainHandler.post(() -> lastletter.setVisibility(View.INVISIBLE));
                    
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search