My application uses Speech To Text recognition, if I change the language in the settings to Russian, it stops working correctly, as the recognition language also changes to Russian. How can I make English use my application regardless of the system language?
I tried this option, but it does not work:
Locale myLocale; myLocale = new Locale("English (US)", "en_US"); Locale.setDefault(myLocale); android.content.res.Configuration config = new android.content.res.Configuration(); config.locale = myLocale; getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button recognizeButton = (Button)findViewById(R.id.button1); recognizeButton.setOnClickListener(this); } @Override public void setContentView(View context) { final DisplayMetrics dm = context.getResources().getDisplayMetrics(); final Configuration conf = context.getResources().getConfiguration(); conf.locale = new Locale("en"); context.getResources().updateConfiguration(conf, dm); } @Override public void onClick(View v) { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "You may speak!"); intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH); startActivityForResult(intent, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1 && resultCode == RESULT_OK) { ArrayList<String> results; results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); TextView speechText = (TextView) findViewById(R.id.textView1); String str=""; for(int i=0;i<results.size();i++){ str+= results.get(i); } if (str.equals("Mercedes")) { speechText.setText(str); }else{ speechText.setText("It's not: " + str); } } }