I am having a problem with setError()
on EditText
. When an activity is opened, it checks if certain fields are empty and sets error message on them if true. However, the exclamation mark icon is only displayed in case I write some text in field and then delete it. If I lose focus on that field, the icon will disappear again. Both fields Naam
and Telefonnumer
have this validation.
I use Android 2.2.2 SDK and the application is run on Nexus 7 with latest updates.
I have Util class:
public class Util { private static String TAG = "Util Class"; public static boolean editTextIsEmpty(EditText edittext) { if (edittext.getText().toString().trim().length() < 1) return true; else return false; } public void editTextListener(final EditText editText) { editText.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { if (editTextIsEmpty(editText) && editText.isEnabled()) editText.setError("Nodig"); else editText.setError(null); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (editTextIsEmpty(editText) && editText.isEnabled()) editText.setError("Nodig"); else editText.setError(null); } }); } }
and then I have method validateInput()
in my activity:
public class DeliveryActivity extends BaseActivity { private ImageButton btnSetDate; private Button btnToSummary; private Button btnSearchAddress; private EditText txtPostcode; private EditText txtHouseNumber; private EditText txtHouseNumberSuffix; private EditText txtStreet; private EditText txtCity; private EditText txtDeliveryDate; private EditText txtName; private EditText txtPhone; private EditText txtEmail; private EditText txtRemark; private TextView lblExtraDeliveryInfo; private Spinner spinnerDelivery; private Spinner spinnerDeliveryPeriod; private Spinner spinnerContact; private Spinner spinnerDeliveryAddress; private Spinner spinnerExtraDeliveryInfo; private RelativeLayout rlDeliveryAddressDetails; private DevRestHelper additionalDeliveryInfo; private DevRestHelper searchClientAddress; private Util util = new Util(); private int year; private int month; private int day; public static final int DIALOG_DATEPICKER = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_delivery); initControls(); validateInput(); } private void initControls() { btnSetDate = (ImageButton) findViewById(R.id.activity_delivery_btnCalendar); btnToSummary = (Button) findViewById(R.id.activity_delivery_btnSummary); btnSearchAddress = (Button) findViewById(R.id.activity_delivery_btnSearchAddress); spinnerDelivery = (Spinner) findViewById(R.id.activity_delivery_spinnerDeliveryMethod); spinnerDeliveryPeriod = (Spinner) findViewById(R.id.activity_delivery_spinnerDeliveryPeriod); spinnerContact = (Spinner) findViewById(R.id.activity_delivery_spinnerContactperson); spinnerDeliveryAddress = (Spinner) findViewById(R.id.activity_delivery_spinnerDeliveryAddress); spinnerExtraDeliveryInfo = (Spinner) findViewById(R.id.activity_delivery_spinnerExtraDeliveryInformation); txtPostcode = (EditText) findViewById(R.id.activity_delivery_txtPostcode); txtHouseNumber = (EditText) findViewById(R.id.activity_delivery_txtHousenumber); txtHouseNumberSuffix = (EditText) findViewById(R.id.activity_delivery_txtHousenumberSuffix); txtStreet = (EditText) findViewById(R.id.activity_delivery_txtStreet); txtCity = (EditText) findViewById(R.id.activity_delivery_txtCity); txtDeliveryDate = (EditText) findViewById(R.id.activity_delivery_txtDeliveryDate); txtName = (EditText) findViewById(R.id.activity_delivery_txtName); txtPhone = (EditText) findViewById(R.id.activity_delivery_txtPhone); txtEmail = (EditText) findViewById(R.id.activity_delivery_txtEmail); txtRemark = (EditText) findViewById(R.id.activity_delivery_txtRemark); lblExtraDeliveryInfo = (TextView) findViewById(R.id.activity_delivery_lblExtraDetailInformation); rlDeliveryAddressDetails = (RelativeLayout) findViewById(R.id.activity_delivery_rlDeliveryAddressDetails); } private void validateInput() { util.editTextListener(txtPostcode); util.editTextListener(txtHouseNumber); util.editTextListener(txtDeliveryDate); } }
Let me just say that code work on BlueStacks emulator.
