Android speech to text – Android voice typing & Recognition

Android speech to text source code example 2017

Android speech to text source code example supported newer version of Android API 25. Below is
complete source code of android speech to text.

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    
    private TextView voiceToText;
    private static final int REQ_CODE_VOICE_IN = 143;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        voiceToText = (TextView) findViewById(R.id.voiceIn);
        ImageView speakPromt = (ImageView) findViewById(R.id.btnToSpeak);
        speakPromt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startVoiceToTextService();
            }
        });
    }

    private void startVoiceToTextService() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        //You can set here own local Language.
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Hiiii, Tell Me SomeThing");
        try {
            startActivityForResult(intent, REQ_CODE_VOICE_IN);
        }
        catch (ActivityNotFoundException a) {
            
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case REQ_CODE_VOICE_IN: {
                if (resultCode == RESULT_OK && null != data) {
                    ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    voiceToText.setText(result.get(0));
                }
                break;
            }

        }
    }
}

Android simple audio recorder voice recorder example with source code 2017 no deprecation 
in newer version of android.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

        <TextView
            android:id="@+id/voiceIn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:textColor="#1e9aff"
            android:textStyle="bold"
            android:text="hint"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_below="@+id/btnToSpeak"
            android:layout_alignParentStart="true"
            android:layout_marginTop="65dp" />

    <ImageView
        android:id="@+id/btnToSpeak"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@android:drawable/presence_audio_busy"
        />

</RelativeLayout>

Get all the new Android source code videos.

Add a Comment

Your email address will not be published. Required fields are marked *