Search code examples
androidconcatenationtoast

variable/resource concatenation in toast not working


This works:

Toast.makeText(getApplicationContext(), attemptsRemainingCount.toString(), Toast.LENGTH_LONG).show();

This works:

Toast.makeText(getApplicationContext(), R.string.attemptsRemaining, Toast.LENGTH_LONG).show();

This however, does not work:

Toast.makeText(getApplicationContext(), attemptsRemainingCount.toString() + R.string.attemptsRemaining, Toast.LENGTH_LONG).show();

All it gives me is a long number. Could someone please tell me what I'm doing wrong with the concatenation here?


Solution

  • You're concatenating java variable with a Resource Identifier.

    Try using following:

    Toast.makeText(getApplicationContext(), attemptsRemainingCount.toString() + getResources().getString(R.string.attemptsRemaining), Toast.LENGTH_LONG).show();