@Override
            public void onClick(View v) {
                if (recognizing) {
                    handler.stopListening();
                } else {
                    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getContext().getPackageName());
                    handler.startListening(intent);
                }
                btnRecognize.setEnabled(false);
            }
Fragment with visualization of speech recognition.
app/src/main/java/com/cleveroad/example/SpeechRecognitionFragment.java