Spinner values ​​not selectable

I have a very simple spinner in which I show two values ​​1 => English 2 => Hebrew

and I restart all activity (to change the interface) when I select any value from the counter, but the problem is that my activity restarts only for case 1, only help me figure out the problem.

Here is the code I'm using

languageSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if (!isFistLaunch) { String email = mEmailEditText.getText().toString(); String pass = mPasswordEditText.getText().toString(); Intent intent = new Intent(MainActivity.this, MainActivity.class); intent.putExtra("typed_email", email); intent.putExtra("typed_pass", pass); mUserSession.setUserLanguage(lang[position]); Toast.makeText(MainActivity.this, "Spinner position = " + position, Toast.LENGTH_SHORT).show(); startActivity(intent); MainActivity.this.finish(); } else { isFistLaunch = false; } } @Override public void onNothingSelected(AdapterView<?> parent) { } }); 

I also put a toast on its side, but it only shows once ...

Spinner works the way I want, but only on my device. all other devices do not show Hebrew toasts. They only show a toast for English.

Can anyone tell me what the problem is? Thanks

0
android android spinner
Dec 21 '15 at 10:09
source share
3 answers

try it

 ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, states); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spTimeName.setAdapter(dataAdapter); if (!compareValue.equals(null)) { int spinnerPosition = dataAdapter.getPosition(compareValue); spTimeName.setSelection(spinnerPosition); } 
+1
Dec 21 '15 at 10:30
source share

The problem is that the active load that gets the first position of the array is lang1

  Spinner languageSpinner = (Spinner) findViewById(R.id.spinner); String lang[] = {"Select lang","lang1", "lang2"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(JobCardActivity.this, android.R.layout.simple_list_item_1, lang); languageSpinner.setAdapter(adapter); languageSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if(position != 0 || (!lang[position].equals("Select lang"))){ Toast.makeText(JobCardActivity.this, "Spinner position = " + position, Toast.LENGTH_SHORT).show(); } } @Override public void onNothingSelected(AdapterView<?> parent) { } }); 
0
Dec 21 '15 at 10:33
source share

To switch the language for your application, I suggest you refer to the following code:

  protected static void setLocale(Context context, String language) { // if not exist, set EN as default Locale locale = new Locale(language); final Locale[] availableLocales=Locale.getAvailableLocales(); if (!(Arrays.asList(availableLocales).contains(locale))) { locale = new Locale("en"); } Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; ((Activity) context).getBaseContext().getResources().updateConfiguration(config, ((Activity) context).getBaseContext().getResources().getDisplayMetrics()); // refresh activity to reload resources Intent refresh = ((Activity) context).getIntent(); ((Activity) context).overridePendingTransition(0, 0); refresh.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); ((Activity) context).finish(); ((Activity) context).overridePendingTransition(0, 0); context.startActivity(refresh); } protected static void switchLocale(Context context) { Locale current = context.getResources().getConfiguration().locale; if (current.getLanguage().equals("he")){ setLocale(context, "en"); } else { setLocale(context, "he"); } } 

Then inside onClick of Button or onItemSelected of Spinner ..., for example, you can call switchLocale(mContext);

P / S: English resources will be stored in \app\src\main\res\values , Hebrew resources will be stored in \app\src\main\res\values-he

You can read more at Google Doc - Multilingual Support .

Hope this helps!

0
Dec 23 '15 at 16:17
source share



All Articles