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:
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();
}
}
}