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); } } } 

    1 answer 1

     final DisplayMetrics dm = context.getResources().getDisplayMetrics(); final Configuration conf = context.getResources().getConfiguration(); conf.locale = new Locale("en"); context.getResources().updateConfiguration(conf, dm); 

    This code is guaranteed to work, I use it myself. After that, you need to recreate all the View (in the case of the Activity call setContentView() ), so that they setContentView() up new resources. You can of course manually record new resources in the View .

    • Could you inspect my code? I have your option does not work. - Nikolai
    • @ Nikolai is too much wrong to inspect. In the root of all wrong. - Vladyslav Matviienko
    • my code is correct, I took it from the book "Learning Android Intents in Android application development - 2014". Except for the setContentView method. Just your part, as I understand it, is suitable for a different approach, the question is how and where to insert it, so that it will work, if possible, could you share a link where your approach is implemented? - Nikolai