BlackPearl
BlackPearl

Reputation: 2775

How to check to empty entry in EditText Android

I am having problem with this, I have tried codes that work in other part of my program but this won't work correctly. I am coding to make sure a user does not submit ab empty form.

Codes

                 <EditText
                    android:id="@+id/msage"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/edittext"
                    android:hint="Write Something...."
                    android:maxLines="4"
                    android:elevation="8dp"
                    android:textSize="14dp"
                    android:text=""
                    android:gravity="top"
                    android:textColor="@color/colorAccent"
                    android:textColorHint="@color/colorAccent"
                   android:layout_margin="40dp"/>

Then on my Activity:

 EditText msg;
 msg = (EditText)findViewById(R.id.msage);

 String msgContent =  msg.getText().toString().trim();
    if (msgContent.equals("") || msgContent.length() <= 15) {
        msg.setError("Enter valid Message");
        return;
    }

   //---below: code to submit form to server

I even tried TextUtils.isEmpty

Edit

So, after more than 12 hours of hair pulling, I finally found where the logic is wrong.

My code was encased in

public void btnSendFeedBack(View view) {
    // Code
}

using the android:clickable="true" and android:onClick="btnSendFeedBack", but this was so wrong as I found out.

After been chased around by @Charuz, I changed to

findViewById(R.id.sendFeedback).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            msgContent = msg.getText().toString().trim();
            //email
            if (msgContent.equals("") || msgContent == null || msgContent.length() < 15) {
                msg.setError("Enter a valid Feedback");
                return;
            }

            SendFeedback();
        }
    });

And it worked.

Thanks for your time...cheers!!!

Upvotes: 0

Views: 124

Answers (6)

Nikhil Bose
Nikhil Bose

Reputation: 39

try with addTextChangedListener in your code.

    msg.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            //here you will get the state of edittext before a change took place
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            // here is the best place to check the length of the valu form edit text on the go.
            // check whether the length is greater than 15
            // and if yes send to server.
            //[ MAY BE A BOOLEAN FLAG CAN MAKE YOUR JOB MUCH EASIER]
            // Else you can show the error message

            if(msg.getText().length()>15){
                VALID_MESSAGE = true;
            }
            else{
                msg.setError("Enter valid Message");
                VALID_MESSAGE = false;
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {


        }
    });

Here i have used a boolean VALID_MESSAGE. It becomes true when user input text with length greater than 15. Also make it false when he erase the entered text. So while sending to server you just need check whether the flag is true.

The same can also be done inside afterTextChanged. There is not much difference.

Upvotes: 0

W4R10CK
W4R10CK

Reputation: 5550

Try to get the condition true at first, on if block and send data to server:

EditText msg;
msg = (EditText)findViewById(R.id.msage);

String msgContent =  msg.getText().toString().trim();
if (!TextUtils.isEmpty(msgContent) || msgContent.length() >= 15) {
    //Send data to server

}else{
   msg.setError("Enter valid Message");
   //return;
}

Order of Evaluation of Logic Operators

Upvotes: 1

fijili
fijili

Reputation: 39

Remove line in EditText:

 android:text=""

And try:

 if(TextUtils.isEmpty(msg.getText().toString()))

Upvotes: 1

Lovekush Vishwakarma
Lovekush Vishwakarma

Reputation: 3169

Simply before sending form to server validate EditText value.

String val=EditText.getText().toString();

And inside your button click use logic,

if(val.length()>15){

  // Your logic for submit form to server
}else{
  // Error message as per your requirement
}

Upvotes: 2

rafsanahmad007
rafsanahmad007

Reputation: 23881

Here is a simple logic for checking if the Edittext is empty or not

private boolean isEmpty(EditText etText) {
    return etText.getText().toString().trim().length() == 0;
}

in your code try this:

String msgContent =  msg.getText().toString().trim();
if ((msgContent.length() == 0) || (msgContent.length() <= 15)) {
    msg.setError("Enter valid Message");
    return;
}

Upvotes: 1

santosh kumar
santosh kumar

Reputation: 2962

you have not added else condition for your code it checks for empty string and sends same to server.

   if (msgContent.equals("") || msgContent.length() <= 15) {
        msg.setError("Enter valid Message");
    }
    else
    {
//submit form to server
    }

Upvotes: 2

Related Questions