-7

I added a button to change text (in the array) when clicking on it.

When the current index reaches the length of the array, I need a toast message to pop up, and also not to update the current index.

But there are errors. Please help me!

This my code:

public void onClick(View v) {
    if (currentIndex == 22) {
        Toast.makeText(azka.this, "YOUR MESSAGE", Toast.LENGTH_SHORT).show();
        return;
    }

    currentIndex++;
    simpleTextSwitcher.setText(strings[currentIndex]);
}
Scott Smith
  • 3,900
  • 2
  • 31
  • 63

1 Answers1

0

Where do you initialize currentIndex? You're using currentIndex++; but you never initialize it unless it is your position.

And you're using return inside an if when you're using a void from onClick. If you want to do something like this try something like this:

And change first parameter of Toast to v.getContext()

public void onClick(View v) {
    if (currentIndex == 22) {
        Toast.makeText(v.getContext(), "YOUR MESSAGE", Toast.LENGTH_SHORT).show();
    } else {
        //Code if is not equals to 22
    }
}
Daniel Puiu
  • 962
  • 6
  • 21
  • 29
Dani Gee
  • 413
  • 4
  • 10