title: Null Validation on EditText box in Alert Dialog - Android
body: <p>I am trying to add some text validation to an edit text field located within an alert dialog box. It prompts a user to enter in a name. </p>
  
  <p>I want to add some validation so that if what they have entered is blank or null, it does not do anything apart from creating a Toast saying error.</p>
  
  <p>So far I have:</p>
  
  <pre><code>    AlertDialog.Builder alert = new AlertDialog.Builder(this);
      alert.setTitle("Record New Track");
      alert.setMessage("Please Name Your Track:");
      // Set an EditText view to get user input
      final EditText trackName = new EditText(this);
      alert.setView(trackName);
      alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
  
              String textString = trackName.getText().toString(); // Converts the value of getText to a string.
              if (textString != null &amp;&amp; textString.trim().length() ==0)
              {   
  
                  Context context = getApplicationContext();
                  CharSequence error = "Please enter a track name" + textString;
                  int duration = Toast.LENGTH_LONG;
  
                  Toast toast = Toast.makeText(context, error, duration);
                  toast.show();
  
  
              }
              else 
              {
  
                  SQLiteDatabase db = waypoints.getWritableDatabase();
                  ContentValues trackvalues = new ContentValues();
                  trackvalues.put(TRACK_NAME, textString);
                  trackvalues.put(TRACK_START_TIME,tracktimeidentifier );
                  insertid=db.insertOrThrow(TRACK_TABLE_NAME, null, trackvalues);
  
              }
  </code></pre>
  
  <p>But this just closes the Alert Dialog and then displays the Toast. I want the Alert Dialog to still be on the screen.</p>
  
  <p>Thanks</p>