2

I am making a reminder app in which the user needs to set date and time from date and time pickers. So I did the following:

private TextView mDateDisplay;
private TextView mTimeDisplay;
...

// Set reminder button
setButton = (Button) findViewById(R.id.set_reminder_button);
setButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
    if (!validate()) {
        onSetDateTimeFailed();
        return;
       }
       // creating new product in background thread
        db.addReminder(id, mEditMessageText.getText().toString(), mDateDisplay.getText().toString(), mTimeDisplay.getText().toString());
        finish();
    }
});
...

//    Validation
public boolean validate() {
    boolean valid = true;

    String date = mDateDisplay.getText().toString();
    String time = mTimeDisplay.getText().toString();

    if (date.isEmpty() ) {
        mDateDisplay.setError("Set date");
        valid = false;
    } else {
        mDateDisplay.setError(null);
    }

    if (time.isEmpty()) {
        mTimeDisplay.setError("Set time");
        valid = false;
    } else {
        mTimeDisplay.setError(null);
    }

    return valid;
}

public void onSetDateTimeFailed() {
    Toast.makeText(getBaseContext(), "Setting date and time failed", Toast.LENGTH_LONG).show();

    setButton.setEnabled(true);
}

I get is the error symbols on the textviews (!), but the only toast message I get is the "Setting date and time failed" and not the "Set date" or "Set time".

The other thing I wanted to do is add a reminder message to date and time:

private EditText mEditMessageText;
...
//    Validation
public boolean validate() {
    boolean valid = true;

    String message = mEditMessageText.getText().toString();

    if (message.isEmpty() || time.length() < 1 || time.length() > 140) {
        mEditMessageText.setError("Enter between 1 and 140 characters");
        valid = false;
    } else {
        mEditMessageText.setError(null);
    }

But now I also get the "Enter between 1 and 140 characters" message when there is error in any validated field. For example, if I entered the needed number of letters in editText filed and did not select date or time I get this error message "Enter between 1 and 140 characters" and the error symbol (!) in editText as well.

Banana
  • 2,435
  • 7
  • 34
  • 60
  • Can you try to catch exceptions in validation? Might be an error you don't see – user6547359 Jul 08 '16 at 09:33
  • Try using if variable.equals("") instead of isEmpty() – Tosin Onikute Jul 08 '16 at 09:35
  • @Html Tosin Tried it but I get the same result. No toast messages for "Set Date" or "Set Time" is shown, and "Enter between 1 and 140 characters" is always shown if any TextView field is empty, even if the message field itself is not empty and meets the criteria. – Banana Jul 08 '16 at 09:43
  • Use `TextUtils.isEmpty()` instead – Vucko Jul 08 '16 at 09:46
  • @Vucko Like this: if (TextUtils.isEmpty(date) )? – Banana Jul 08 '16 at 09:49
  • I guess, yes. The method takes a parameter type `CharSequence` which is any String, StringBuffer, StringBuilder etc. – Vucko Jul 08 '16 at 09:50
  • @Vucko Still not working :) I get the same result. – Banana Jul 08 '16 at 09:51
  • I think the problem is in the other part of your code. Please show where you're calling `validate()` and `onSetDateTimeFailed()` – Vucko Jul 08 '16 at 10:01
  • @Vucko I added it in my first code block (// Set reminder button) – Banana Jul 08 '16 at 10:11
  • 2
    I think I've found a very similar question [here](http://stackoverflow.com/questions/13508270/android-seterrorerror-not-working-in-textview) check and see if it works. Setting the Error on the textview is somewhat different. – Vucko Jul 08 '16 at 10:15
  • 1
    @Vucko Will try this tonight. It looks promising :) TY – Banana Jul 08 '16 at 10:16
  • @Vucko I tried adding the following: style="@android:style/Widget.EditText" to textviews in xml, and it worked (without adding a toast message), but I still get the "Enter between 1 and 140 characters" and (!) symbol if there is any empty field (either date or time field). – Banana Jul 08 '16 at 21:10
  • This is the way it should be right? – Vucko Jul 08 '16 at 21:14
  • @Vucko It shows the Toast message in messageEditText even if I entered the required number of letters. It should not show (!) or the error msg if the messageEditText field is not empty. And when I add date and time the error message then dissapears from messageEditText – Banana Jul 08 '16 at 21:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116839/discussion-between-vucko-and-kemo). – Vucko Jul 08 '16 at 21:23

3 Answers3

1

Try this:

public boolean validate() {
        String date = mDateDisplay.getText().toString();
        String time = mTimeDisplay.getText().toString();
        String message = mEditMessageText.getText().toString();

        if (date.isEmpty()) {
            Toast.makeText(getBaseContext(), "Set date", Toast.LENGTH_SHORT).show();
            return false;
        } else if (time.isEmpty()) {
            Toast.makeText(getBaseContext(), "Set time", Toast.LENGTH_SHORT).show();
            return false;
        } else if (message.isEmpty() || time.length() < 1 || time.length() > 140) {
            Toast.makeText(getBaseContext(), "Enter between 1 and 140 characters", Toast.LENGTH_SHORT).show();
            return false;
        } else {
            return true;
        }
    }
Dang Nguyen
  • 354
  • 2
  • 9
  • I tried it and it shows messages by showing the first toast message, if I fix it it will show the second... but it does not show the (!) symbol. – Banana Jul 08 '16 at 20:20
1

Referring to this question helped solve setting the errors on the TextViews.

As for the other part, it was replacing time.length() with message.length().

Just moved my most helpful comments to the answer section.

Community
  • 1
  • 1
Vucko
  • 7,371
  • 2
  • 27
  • 45
0

@Vucko got the answer to first part of my question - Why does it always show "Enter between 1 and 140 characters", by paying more attention to my code then I did. It was a syntax error. Instead of message.length() I wrote time.length().

For the second part of my question - how to show the messages for TextViews I found a solution on a link Vucko posted - Android setError("error") not working in Textview by adding

    style="@android:style/Widget.EditText"

or

    android:focusable="true"
    android:focusableInTouchMode="true"

to my xml file.

TY Vucko

Community
  • 1
  • 1
Banana
  • 2,435
  • 7
  • 34
  • 60
  • 1
    No problem bro. I would have liked to get some credit like accepted answer :D Can I write my own answer combining these two parts? I seek to be rewarded, it was quite a ride to find all the errors :) – Vucko Jul 08 '16 at 21:56
  • @Vucko Sure thing :) Thanks again – Banana Jul 08 '16 at 22:43