Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.mina.speechrecognition;
- import android.content.Intent;
- import android.os.Bundle;
- import android.speech.RecognizerIntent;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- private static final int RESULT_SPEECH = 100;
- TextView t1, t2, t3, t4, t5;
- Button b1;
- int count = 0;
- TextView[] arrayOftv;
- private Toolbar _toolbar;
- private Button fromWeb;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- _toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(_toolbar);
- t1 = (TextView) findViewById(R.id.textView);
- t2 = (TextView) findViewById(R.id.textView2);
- t3 = (TextView) findViewById(R.id.textView3);
- t4 = (TextView) findViewById(R.id.textView4);
- t5 = (TextView) findViewById(R.id.textView5);
- fromWeb= (Button)findViewById(R.id.btn);
- fromWeb.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(MainActivity.this , WebActivity.class);
- startActivity(intent);
- }
- });
- arrayOftv = new TextView[]{t1, t2, t3, t4, t5};
- b1 = (Button) findViewById(R.id.button);
- b1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- count = 0;
- startReadInput();
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == RESULT_SPEECH && requestCode == RESULT_OK) ;
- {
- ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
- if (results.size() > 0 && count<arrayOftv.length) {
- arrayOftv[count].setText(results.get(0));
- count++;
- if(arrayOftv.length != count)
- startReadInput();
- }
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
- private void startReadInput() {
- Intent voicerecogize = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- voicerecogize.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
- voicerecogize.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
- RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
- voicerecogize.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ar-SA");
- startActivityForResult(voicerecogize, RESULT_SPEECH);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement