title: Google voice recognition without popup in android studio
body: <p>Job: I wanna an app like "When I call the method it'll start voice recognizer and  gives the result of voice recognition</p>
  
  <p>Problem: Voice recognizer is starting when I call the method but it's not giving the result of voice recognition instantly. It's giving the result of first calling in second calling.</p>
  
  <p>My codes:
  Voice Recognition Class:</p>
  
  <pre><code>package com.ooo.voicerecog;
  import java.util.ArrayList;
  import android.Manifest;
  import android.content.pm.PackageManager;
  import android.os.Build;
  import android.speech.RecognitionListener;
  import android.speech.RecognizerIntent;
  import android.speech.SpeechRecognizer;
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.v4.app.ActivityCompat;
  import android.support.v4.content.ContextCompat;
  import android.util.Log;
  public class VoiceRecognitionActivity extends Activity implements        
  RecognitionListener {
  private SpeechRecognizer speech = null;
  private Intent recognizerIntent;
  public ArrayList&lt;String&gt; matches;
  public String newtext=null;
  public String text =null;
  public void starto(){
  int MyVersion = Build.VERSION.SDK_INT;
  if (MyVersion &gt; Build.VERSION_CODES.LOLLIPOP_MR1) {
      if (!checkIfAlreadyhavePermission()) {
          requestForSpecificPermission();
      }
  }
  speech = SpeechRecognizer.createSpeechRecognizer(this);
  speech.setRecognitionListener(this);
  recognizerIntent = new 
  Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
          "en");
  recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
          this.getPackageName());
  recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
          RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
  recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
  speech.startListening(recognizerIntent);
  }
  public String getvoice(){
  starto();
  speech.stopListening();
  return text;
  }
  private boolean checkIfAlreadyhavePermission() {
  int result = ContextCompat.checkSelfPermission(this, 
  Manifest.permission.RECORD_AUDIO);
  if (result == PackageManager.PERMISSION_GRANTED) {
  return true;
  } else {
  return false;}
  }
  
  @Override
  public void onRequestPermissionsResult(int requestCode, String[] 
  permissions, int[] grantResults) {
  switch (requestCode) {
  case 101:
  if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  //granted
  } else {
                  //not granted
  }
  break;
  default:
  super.onRequestPermissionsResult(requestCode, permissions, 
  grantResults);
      }
  }
  private void requestForSpecificPermission() {
  ActivityCompat.requestPermissions(this, new String[]
  {Manifest.permission.RECORD_AUDIO}, 101);
  }
  @Override
  public void onBeginningOfSpeech() { }
  
  @Override
  public void onBufferReceived(byte[] buffer) { }
  
  @Override
  public void onEndOfSpeech() {}
  
  @Override
  public void onError(int errorCode) {
      String errorMessage = getErrorText(errorCode);
  }
  
  @Override
  public void onEvent(int arg0, Bundle arg1) {}
  
  @Override
  public void onPartialResults(Bundle arg0) {}
  
  @Override
  public void onReadyForSpeech(Bundle arg0) { }
  
  @Override
  public void onResults(Bundle results) {
  matches = 
  results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
      for (String result : matches)
          text += result + "\n";}
  
  @Override
  public void onRmsChanged(float rmsdB) {}
  
  public static String getErrorText(int errorCode) {
      String message;
      switch (errorCode) {
          case SpeechRecognizer.ERROR_AUDIO:
              message = "Audio recording error";
              break;
          case SpeechRecognizer.ERROR_CLIENT:
              message = "Client side error";
              break;
          case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
              message = "Insufficient permissions";
              break;
          case SpeechRecognizer.ERROR_NETWORK:
              message = "Network error";
              break;
          case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
              message = "Network timeout";
              break;
          case SpeechRecognizer.ERROR_NO_MATCH:
              message = "No match";
              break;
          case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
              message = "RecognitionService busy";
              break;
          case SpeechRecognizer.ERROR_SERVER:
              message = "error from server";
              break;
          case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
              message = "No speech input";
              break;
          default:
              message = "Didn't understand, please try again.";
              break;
      }
      return message;
    }
  
     }
  </code></pre>
  
  <p>MainActivity:</p>
  
  <pre><code>package com.ooo.voicerecog;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  
  public class MainActivity extends VoiceRecognitionActivity{
  public Button button;
  public Button button1;
  public TextView textView;
  public String texo;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      button = (Button) findViewById(R.id.button);
      textView = (TextView) findViewById(R.id.textview);
      button.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View view) {
  
              textView.setText(getvoice());
  
  
  
          }
      });
  
  
  
  
    }}
  </code></pre>