Djengiss Djengiss - 3 months ago 15
Android Question

I need my counter to stop counting and display a toast when it reaches 510

This is the math part at the moment, i have one of these for each text view element, and it counts fine, all i need is for the numbers in all of the text views to stop counting when the total reaches 510 and all the others to stop counting when they reach 252 individually

when the total reaches 510 and/or when the individual values reach 252 i want a toast dislaying "Highest number reached" or something like that

This is Java if anyone doesn't know, and it's in android studio

private View.OnClickListener hpListener = new View.OnClickListener() {
public void onClick(View view) {

// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.evval);
// TextView element
TextView HPtx = (TextView) findViewById(R.id.hpval);
TextView atttx = (TextView) findViewById(R.id.attval);
TextView deftx = (TextView) findViewById(R.id.defval);
TextView satx = (TextView) findViewById(R.id.saval);
TextView sdtx = (TextView) findViewById(R.id.sdval);
TextView spdtx = (TextView) findViewById(R.id.spdval);
TextView totaltx = (TextView) findViewById(R.id.totalval);

// Ev values
int add = Integer.parseInt(spinner.getSelectedItem().toString());

// Calculation
int HP = Integer.parseInt(HPtx.getText().toString());
int att = Integer.parseInt(atttx.getText().toString());
int def = Integer.parseInt(deftx.getText().toString());
int sa = Integer.parseInt(satx.getText().toString());
int sd = Integer.parseInt(sdtx.getText().toString());
int spd = Integer.parseInt(spdtx.getText().toString());
int hptotal = HP + add;
int total = hptotal + att + def + sa + sd + spd;

// Value calculations
DecimalFormat addHPFormat = new DecimalFormat("000");
DecimalFormat addtotalFormat = new DecimalFormat("000");
HPtx.setText(addHPFormat.format(hptotal));
totaltx.setText(addtotalFormat.format(total));
}
};

Answer
private View.OnClickListener hpListener = new View.OnClickListener() {
public void onClick(View view) {

    // Spinner element
    Spinner spinner = (Spinner) findViewById(R.id.evval);
    // TextView element
    TextView HPtx = (TextView) findViewById(R.id.hpval);
    TextView atttx = (TextView) findViewById(R.id.attval);
    TextView deftx = (TextView) findViewById(R.id.defval);
    TextView satx = (TextView) findViewById(R.id.saval);
    TextView sdtx = (TextView) findViewById(R.id.sdval);
    TextView spdtx = (TextView) findViewById(R.id.spdval);
    TextView totaltx = (TextView) findViewById(R.id.totalval);

    // Ev values
    int add = Integer.parseInt(spinner.getSelectedItem().toString());

    // Calculation
    int HP = Integer.parseInt(HPtx.getText().toString());
    int att = Integer.parseInt(atttx.getText().toString());
    int def = Integer.parseInt(deftx.getText().toString());
    int sa = Integer.parseInt(satx.getText().toString());
    int sd = Integer.parseInt(sdtx.getText().toString());
    int spd = Integer.parseInt(spdtx.getText().toString());
    int hptotal = HP + add;
    int total = hptotal + att + def + sa + sd + spd;

    if (total >= 510 || att >= 252 || def >= 252 || sa >= 252 || sf >= 252 || spd >= 252) {
         // Replace MainActivity.this with your context
         Toast.makeText(MainActivity.this, "Highest number reached", Toast.LENGTH_SHORT).show();
         return;
    }

    // Value calculations
    DecimalFormat addHPFormat = new DecimalFormat("000");
    DecimalFormat addtotalFormat = new DecimalFormat("000");
    HPtx.setText(addHPFormat.format(hptotal));
    totaltx.setText(addtotalFormat.format(total));
    }
};
Comments