Android studio: How do I show an updating variable at all times using a Textview?

Viewed 65

enter image description here
The android studio code works as intended. However, points are made in the background since it's an idle game. An on click method only shows the points that were made as of the last click. I need the points to be shown at all times.

I have been testing this new added bit of code but the points are not updating when the button is clicked. Example in photo. Evolution Points = 0; The on click method is updating but there are also points made in the background that need to be shown without a button click. Any suggestions?

visibletotals.setText("Evolutions Points: " + clicks);

full code for reference

package com.example.idleclicker;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import java.util.Timer;
    import java.util.TimerTask;
    
    
    public class MainActivity extends AppCompatActivity {
    
        TextView points;
    TextView visibletotals;
        Button click;
        Button upgradebtn;
        TextView Leveltext;
    int clicks = 0;
    int clickcost = 10;
    int upgradelevel = 1;
        Timer myTimer = new Timer();
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            click = (Button) findViewById(R.id.click);
            points = (TextView) findViewById(R.id.points);
            upgradebtn = (Button) findViewById(R.id.upgradebtn);
            Leveltext = (TextView) findViewById(R.id.leveltext);
            visibletotals = (TextView) findViewById(R.id.visibletotals);
    
    
            visibletotals.setText("Evolutions Points: " + clicks);
    
    
            click.setEnabled(true);
    
            upgradebtn.setEnabled(true);
    
            myTimer.schedule(new TimerTask() {
                                 @Override
                                 public void run() {
                                     clicks+= upgradelevel;
    
                                 }
                             }, 0, 1000);
    
    
    
            click.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            clicks++;
                            points.setText(getResources().getString(R.string.evol) + clicks);
    
                        }
    
    
                    });
    
            upgradebtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (clicks >= clickcost) {
    
                        clicks -= clickcost;
    
                        upgradelevel += 1;
    
                        clickcost *= 2;
                        upgradebtn.setText(getResources().getString(R.string.Upgrade) +
                                clickcost +
                                getString(R.string.LevelText)
                                + upgradelevel);
    
                    };
                }
    
                });
    
    
        }}
1 Answers

I can see that your clicks is changing(dynamic) while your set text is static that means it is only replying at the start and it is not triggered from the next time you do it. one way to tackle this is put the settext value in the trigger block(i.e onclicklistener) and make it set the fresh text as per triggered.

Related