how can i show a total score to the player screen not using a Toast

Tags: ,



I made a higher lower game . if the player guess the number he get a specific number of points depending on how many tryes he had I wrote the code, i have the total but i dont know how to display it in a textview or plain text , anything but no toast. Here is the code:

package com.markusappcompany.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    int total =0;

    int score = 20;

    int nr= 1;

    int randomNumber;

    public void generateRandomNumber(){

     Random rand = new Random();

      randomNumber = rand.nextInt(20)+1;
 }
public void clickFunction(View view){

     EditText editText = (EditText) findViewById(R.id.editText);


    int guessValue = Integer.parseInt(editText.getText().toString());



    String message;

    if(guessValue > randomNumber)
    {
        message = "Mai mic!";

        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

        nr= nr +1;

        score = score - 2;

    } else if( guessValue < randomNumber) {

        message = "Mai mare!";

        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

        nr = nr +1;

    } else {

        total = total+score;

        if (nr == 1) {

            message = "YAY! Ai ghicit din prima! Incearca din nou" ;

            Toast.makeText(this, message + "+" + score, Toast.LENGTH_LONG).show();

            generateRandomNumber();

            score = 20;

        } else {

        message = "YAY! Incearca din nou! Ai ghicit din " ;

        Toast.makeText(this, message + " " + nr +" incercari" + "+" +  score  + " " + total, Toast.LENGTH_LONG).show();

here the total is showed in a Toast. I want it to be showed on the screen as a text permanently.

       generateRandomNumber();

        nr = 1;

        score = 20;
        }

    }
    Log.i("Entered value", editText.getText().toString());

    Log.i("info", Integer.toString(randomNumber));


    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       generateRandomNumber();

    }
}

Answer

In your activity_main.xml add this TextView

<TextView
android:id = "@+id/text_view"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"

/>

In your MainActivity class in onCreate() method

  class MainActivity extends......
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         //find textview
         textView = findViewById(R.id.text_view);

         generateRandomNumber();

      }
    }

Now set the total score to the textView

.........
} else {
message = "YAY! Incearca din nou! Ai ghicit din " ;
String text = message + " " + nr +" incercari" + "+" +  String.valueOf(score)  + " " + String.valueOf(total);


textView.setText(text);
...........


Source: stackoverflow