Advertisement
minafaw3

MainActivity

Nov 28th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. package com.example.mina.speechrecognition;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.speech.RecognizerIntent;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.support.v7.widget.Toolbar;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.TextView;
  13.  
  14. import java.util.ArrayList;
  15.  
  16. public class MainActivity extends AppCompatActivity {
  17.  
  18. private static final int RESULT_SPEECH = 100;
  19. TextView t1, t2, t3, t4, t5;
  20. Button b1;
  21. int count = 0;
  22. TextView[] arrayOftv;
  23. private Toolbar _toolbar;
  24. private Button fromWeb;
  25.  
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_main);
  30. _toolbar = (Toolbar) findViewById(R.id.toolbar);
  31. setSupportActionBar(_toolbar);
  32. t1 = (TextView) findViewById(R.id.textView);
  33. t2 = (TextView) findViewById(R.id.textView2);
  34. t3 = (TextView) findViewById(R.id.textView3);
  35. t4 = (TextView) findViewById(R.id.textView4);
  36. t5 = (TextView) findViewById(R.id.textView5);
  37.  
  38. fromWeb= (Button)findViewById(R.id.btn);
  39. fromWeb.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. Intent intent = new Intent(MainActivity.this , WebActivity.class);
  43. startActivity(intent);
  44. }
  45. });
  46. arrayOftv = new TextView[]{t1, t2, t3, t4, t5};
  47.  
  48. b1 = (Button) findViewById(R.id.button);
  49. b1.setOnClickListener(new View.OnClickListener() {
  50. @Override
  51. public void onClick(View v) {
  52. count = 0;
  53. startReadInput();
  54. }
  55. });
  56.  
  57.  
  58.  
  59. }
  60.  
  61.  
  62. @Override
  63. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  64. if (requestCode == RESULT_SPEECH && requestCode == RESULT_OK) ;
  65. {
  66. ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
  67. if (results.size() > 0 && count<arrayOftv.length) {
  68. arrayOftv[count].setText(results.get(0));
  69. count++;
  70. if(arrayOftv.length != count)
  71. startReadInput();
  72.  
  73. }
  74.  
  75. }
  76.  
  77. super.onActivityResult(requestCode, resultCode, data);
  78. }
  79.  
  80.  
  81. private void startReadInput() {
  82. Intent voicerecogize = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  83. voicerecogize.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
  84. voicerecogize.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
  85. RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
  86. voicerecogize.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ar-SA");
  87. startActivityForResult(voicerecogize, RESULT_SPEECH);
  88. }
  89.  
  90.  
  91. @Override
  92. public boolean onCreateOptionsMenu(Menu menu) {
  93. // Inflate the menu; this adds items to the action bar if it is present.
  94. getMenuInflater().inflate(R.menu.menu_main, menu);
  95. return true;
  96. }
  97.  
  98. @Override
  99. public boolean onOptionsItemSelected(MenuItem item) {
  100. // Handle action bar item clicks here. The action bar will
  101. // automatically handle clicks on the Home/Up button, so long
  102. // as you specify a parent activity in AndroidManifest.xml.
  103. int id = item.getItemId();
  104.  
  105. //noinspection SimplifiableIfStatement
  106. if (id == R.id.action_settings) {
  107. return true;
  108. }
  109.  
  110. return super.onOptionsItemSelected(item);
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement