0

I have found and implemented a function in my project whereby the use can enter an address in an EditText field and press a button "SHOW ON MAP", it will pass the text that the user typed in and pass that address to the Google Maps.

As show below:

enter image description here

However, there is a slight problem, even when the user did not enter anything in my app "EditText field, it will still lead the user to Google Maps(however with nothing in the search bar).

How can I change the codes so that if there is no text in the EditText field and the user click "SHOW ON MAP", my app will prompt a toast saying "Please enter address" ?

I have been messing around and cannot seem to get it working.

Below are my codes:

   

 

  /* -------------------- Methods to use Maps -------------------- */

       

     public void onMaps(View v){

        

                // Checking if Network is available

                if (!Utilities.isNetworkAvailable(this)){

                    showDialogFragment(NETWORK_ERR_DIALOG, "network_err_dialog");

                }

                else{

                    // Obtaining text shown by the TextEdit (it could be different from the recognized one cause user can modify it)

                    textToUse = companyAddress.getText().toString();

                    textToUse = textToUse.replace(' ', '+');

                    try {

                        Intent geoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + textToUse));

                        startActivity(geoIntent);

                    } catch (Exception e){

                        Toast.makeText(this, getString(R.string.maps_error), Toast.LENGTH_SHORT).show();

                    }

                }

            }
Hello World
  • 2,764
  • 1
  • 11
  • 23
Donovan Tan
  • 326
  • 1
  • 5
  • 16
  • Check for the length of you textToUse variable. If the length is 0 or less then a certain factor then you can use a toast and return by getting into the intent block. – Anshul Feb 25 '16 at 04:11
  • Couldn't you just put your entire try-catch in an if statement like `if(!textToUse.equals("")){//try-catch}` and then `else{//make toast here}` – Calvin P. Feb 25 '16 at 04:15

6 Answers6

1

Try adding

if (textToUse.length() == 0) {
    Toast.makeText(this, "Please Enter Address", Toast.LENGTH_SHORT).show();
    return;
}

before your try/catch block, like this.

...
textToUse = companyAddress.getText().toString();
textToUse = textToUse.replace(' ', '+');
// begin inserted code
if (textToUse.length() == 0) {
    Toast.makeText(this, "Please Enter Address", Toast.LENGTH_SHORT).show();
    return;
}
// end inserted code
try {
    Intent geoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + textToUse));
    startActivity(geoIntent);
} catch (Exception e){
    Toast.makeText(this, getString(R.string.maps_error), Toast.LENGTH_SHORT).show();
}
...
William Rosenbloom
  • 2,506
  • 1
  • 14
  • 37
0

Try replaceing this with getActivity()

Toast.makeText(this, getString(R.string.maps_error), Toast.LENGTH_SHORT).show();
Zanilen
  • 37
  • 10
0

Check for the length of you textToUse variable. If the length is 0 or less then a certain factor then you can use a toast and return by getting into the intent block. Alternatively you can add a onTextChangedListener to your edit text and disable the button if the length of text is below a certain factor.

Anshul
  • 387
  • 3
  • 15
0

put following condition on your code before start activity

        textToUse = companyAddress.getText().toString();
        textToUse = textToUse.replace(' ', '+');
        if (textToUse.trim().equals("") && textToUse.trim().equals("+")) {
            Toast.makeText(this, getString(R.string.maps_error), Toast.LENGTH_SHORT).show();
        } else {
            try {
                Intent geoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + textToUse));
                startActivity(geoIntent);
            } catch (Exception e) {
                Toast.makeText(this, getString(R.string.maps_error), Toast.LENGTH_SHORT).show();
            }
        }
Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39
0

you should always check whether your searchbox has any text or not.

if (companyAddress.getText().length() > 0) {

            textToUse = companyAddress.getText().toString();

            textToUse = textToUse.replace(' ', '+');

            try {
                Intent geoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + textToUse));
                startActivity(geoIntent);
            } catch (Exception e) {
                // Handle error
            }
        } else {
            Toast toast = Toast.makeText(context, "Enter Text in Search Field", Toast.LENGTH_SHORT).show();
        }
ErShani
  • 392
  • 2
  • 9
0

Try usin isEmpty() like this

EditText myedittext = (EditText) findViewById(R.id.youredittext);
    String edit = myedittext .getText().toString();

                if(edit.isEmpty()){
                           Toast.makeText(youractivity.this, "please enter adress" , Toast.LENGTH_SHORT).show();
                          }else{
                                     // what you want like search the map
                                    }
user5894647
  • 544
  • 6
  • 15