Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seterror signal is not disappearing and Search is working

I have implemented the code of set error on two edit text.

  • First to get any string without null or empty value.
  • Second to get any string without null or empty value and must have some Double value.

Code is as follows

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SearchPlaces extends Activity {
    EditText place;
    EditText distance;
    Button search;
    static String _place;
    static String _distance;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        place = (EditText) findViewById(R.id.place);
        distance = (EditText) findViewById(R.id.distance);
        search = (Button) findViewById(R.id.search);
        search.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Map<String, String> validate = new HashMap<String, String>();
                _place = place.getText().toString();
                _distance = distance.getText().toString();
                validate.put("placename", _place);
                validate.put("distancenumeric", _distance);
                if (!validateInputInformation(validate)) {
                    return;
                }
                if ((_place == "" && _distance == "")) {
                    Toast.makeText(getApplicationContext(),
                            "Please fill all the information",
                            Toast.LENGTH_SHORT).show();
                } else {
                    _place = place.getText().toString();
                    _distance = distance.getText().toString();
                    Intent placedist = new Intent(getApplicationContext(),
                            MainActivity.class);
                    Bundle bundle = new Bundle();

                    bundle.putString("place", _place);
                    bundle.putString("distance", _distance);

                    placedist.putExtras(bundle);
                    startActivity(placedist);
                }
            }

            private boolean validateInputInformation(
                    Map<String, String> validate) {

                String l_place = validate.get("placename");
                if (l_place == null || l_place.equals("")) {
                    place.setError("Please enter the Place First");
                    return false;
                }

                String l_distance = validate.get("distancenumeric");
                if (l_distance == null || l_distance.equals("")) {
                    distance.setError("Please enter the Distance First");
                    return false;
                }
                if (!isDoubleNumber(l_distance)) {
                    distance.setError("Please enter Numeric Value");
                    return false;
                }

                return true;

            }
            private boolean isDoubleNumber (String num) {
                try {
                    Double.parseDouble(num);
                } catch (NumberFormatException nfe) {
                    return false;
                }
                return true;
            }
        });
    }
}

after implementing that all working are going good but when I doesn't enter and click search button the error pops up in first edit-box after that when I reenter the correct values as input needed then the error notification doesn't disappear but search button works good.

Please help me so that the set-error notification can be removed from the first editbox.

like image 907
Varun Vishnoi Avatar asked Nov 23 '12 08:11

Varun Vishnoi


2 Answers

Write below code line instead of if ((_place == "" && _distance == "")), it will solve your problem.

if (_place.equals("") && _distance.equals(""))
like image 198
Dipak Keshariya Avatar answered Sep 24 '22 16:09

Dipak Keshariya


I stumble on your post because I'm facing the same problem: "When I leave empty a field, my setError shows up but when I start typing in, the icon and text are not removed"

This behaviour is strange and I'm pretty sure that this is a bug on Android because if I set my field's inputType to "textAddressEmail" or "textPassword" it works but it doesn't when my input are "text", "textCapWords", "textPersonName"...

The solution is to programmatically set the inputType

EditText registrationName = (EditText)findViewById(R.id.registrationName);

registrationName.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS | InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
like image 38
Mathieu Castets Avatar answered Sep 22 '22 16:09

Mathieu Castets