1. MainActivity.Java
package com.example.Speek Text;
import java.util.ArrayList;
import java.util.Locale;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.database.CursorJoiner;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.provider.MediaStore;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
protected static final int RESULT_SPEECH = 1;
private boolean hello = true;
private ImageButton btnSpeak;
private TextView Text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Text = (TextView) findViewById(R.id.Text);
btnSpeak = (ImageButton) findViewById(R.id.mic);
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, Locale.getDefault());
try {
startActivityForResult(intent, RESULT_SPEECH);
Text.setText("");
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
"Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
boolean setText = true;
if(setText){
Text.setText(text.get(0));
}
else
{
Text.setText("hi");
Toast.makeText(this, "Speek Word ", Toast.LENGTH_SHORT).show();
}
}
}
break;
}
}
}
2. Activity Main.xml
<?xml version="1.0" encoding="utf-8"?>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/background">
<TextView
android:text="hi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview1"
android:layout_margin="30dp"
android:textSize="22dp"
android:layout_centerHorizontal="true"
android:textColor="#ffffff"
android:background="#000000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Text"
android:layout_below="@+id/textview1"
android:layout_marginTop="20dp"
android:text="Tap on mic to speak"
android:textColor="#ffffff"
android:textSize="25dp"
android:layout_centerInParent="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/mic"
android:id="@+id/mic"
android:layout_below="@+id/Text"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true"
android:background="@null"/>
</RelativeLayout>
3. Activity Mainifests.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.Speek">
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
4. String.xml
<resources>
<string name="app_name">Calling</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
<< PREVIEW >>
<< NEXT >>
No comments:
Post a Comment