Search code examples
androidspinnerandroid-spinner

Android Spinner validation


I need to validate selected item of Spinner in Android.

I tried the following code, but it's not working.

if (Spinner1.getSelectedItem().toString().trim() == "Pick one") {
    Toast.makeText(CallWs.this, "Error", Toast.LENGTH_SHORT).show();
}

What is wrong with the code, and how can i fix it?


Solution

  • Use .equals or .equalsIgnoreCase to compare two strings in java/android instead of ==.

    Try this

    if (Spinner1.getSelectedItem().toString().trim().equals("Pick one")) {
        Toast.makeText(CallWs.this, "Error", Toast.LENGTH_SHORT).show();
    }