TextView textView = findViewById(R.id.textview); if (inputText.isEmpty()) { textView.setError("Please enter a valid text"); } else { // do something }
EditText editText = findViewById(R.id.edittext); Pattern pattern = Pattern.compile("^[\\w\\.-]+@[\\w\\.-]+\\.\\w{2,4}$"); Matcher matcher = pattern.matcher(email); if (!matcher.matches()) { editText.setError("Please enter a valid email address"); } else { // do something }In this example, we use a regular expression to validate an email address entered by the user. If the email address is not valid, we call setError on the EditText and display a message to the user. The android.widget.TextView class is part of the Android SDK, which is a package library provided by Google for developing Android applications.