Search code examples
androidrunnable

Why method in Runnable works only one time?


As you can see method refreshProgressBars() works only one time, although Toast created without interruption. How it possible?!

 bREnemyRegeneration = new Runnable() {
            @Override
            public void run() {
                refreshProgressBars(); //method works only 1 time
                Toast.makeText( //works always
                      BattleActivity.this, 
                      "THIS TOAST WILL BE SHOWN", 
                      Toast.LENGTH_SHORT).show();
                bHandlerTimers.postDelayed(bREnemyRegeneration, 3000); //start new iteration of this Runnable
            }
        };

Just in case method refreshProgressBars():

public void refreshProgressBars(){
    checkDeath();
    if (bHealthsBar.getLayoutParams().width < 30) bHealthsBar.getLayoutParams().width = 0;
    else bHealthsBar.getLayoutParams().width = (int) (PB_EN_HP_WIDTH * bEnemy.getHealth(false) / bEnemy.getHealth(true));
}

Runnable calls as follows:

bHandlerTimers = new Handler();
bHandlerTimers.post(bREnemyRegeneration);

Solution

  • I edit method refreshProgressBar() as follows: Instead bHealthsBar.getLayoutParams().width = bWidth I used bHealthsBar.setLayoutParams(new RelativeLayout.LayoutParams(bWidth, bHealthsBar.getLayoutParams().height));

    And... all works! Question is closed.