Search code examples
javaandroidtimernumbersseconds

Android Studio: Add number every Second


I want to display the income of a person in a certain time. For this I ask for the income per month (=gehalt) and working hours per week (stunden) in another activity. Then in the second activity I want to increase the TextView showGehaltproSekunde (id = textViewZahl) every second by the income per second. I am a beginner, so I don't know what exactly I have to write in public void run(). Or is there another possibility to increase the number every second? I hope someone can help me. Thank you!

public class SecondScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.secondlayout);
    Timer t, timer;

    Intent getGehalt = getIntent();
    float gehalt = getGehalt.getFloatExtra("Gehalt", 0);

    Intent getStunden = getIntent();
    float stunden = getStunden.getFloatExtra("Stunden", 0);

    double gehaltProSekunde = gehalt/4/stunden/3600;
    double gehaltProSekundeRounded = Math.round(gehaltProSekunde*1000)/1000.0;

    TextView showGehaltProSekunde = (TextView)findViewById(R.id.textViewZahl);
    showGehaltProSekunde.setText(gehaltProSekundeRounded+" €");

    t = new Timer();
    t.scheduleAtFixedRate( new TimerTask() {
        @Override
        public void run() {

        }
    });
}

}


Solution

  • For example analysis this:

    private Handler mHandler = new Handler();
    private boolean wasRun = true;
    mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        if(wasRun){               
           //whatever you want to do if run
           //you can add you want to increase variable here
        }
        mHandler.postDelayed(this, 1000);
    }
    }, 1000); // 1 seconds