Validation: Added Toast messages for empty or invalid input
This commit is contained in:
@@ -6,6 +6,7 @@ import android.view.View;
|
|||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.RadioButton;
|
import android.widget.RadioButton;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.activity.EdgeToEdge;
|
import androidx.activity.EdgeToEdge;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
@@ -43,6 +44,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
String userInput = tempInput.getEditText().getText().toString().trim();
|
String userInput = tempInput.getEditText().getText().toString().trim();
|
||||||
if(userInput.isEmpty()) {
|
if(userInput.isEmpty()) {
|
||||||
tempInput.setError("Please enter a temperature");
|
tempInput.setError("Please enter a temperature");
|
||||||
|
Toast.makeText(MainActivity.this, "Error: No value entered", Toast.LENGTH_SHORT).show();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
@@ -61,6 +63,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
tempInput.setError("Invalid number format");
|
tempInput.setError("Invalid number format");
|
||||||
|
Toast.makeText(MainActivity.this, "Error: Invalid number or special characters", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user