Spinner Values is not being selected
Asked Answered
C

3

0

I have very simple spinnser in which i am showing two value 1=> English 2=> Hebrew

and i restart the whole activity (To change UI) on selecting any value from the spinner but the problem is my activity is only restarting for case 1 only please help me to figure out the problem.

Here is the code that i am 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 in side it but it only shows for one time...

Spinner working exactly as i want but only on my device. all of the other devices doesn't show any toast for Hebrew language. They only show Toast for English language.

can anybody tell me what is the problem here? Thanks

Corse answered 21/12, 2015 at 10:9 Comment(0)
F
1

try this

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);
}
Fissi answered 21/12, 2015 at 10:30 Comment(0)
D
0

Problem is when activity load which gets first position of 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) {

                }
            });
Dael answered 21/12, 2015 at 10:33 Comment(2)
it is not getting any option on first launch. to avoid this i put a condition.. as you can check in my codeCorse
Not necessary. because Now first time you always get 0'th positionDael
C
0

In order to switch language for your app, I suggest you refer to my 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's Doc - Supporting Different Languages.

Hope this helps!

Creationism answered 23/12, 2015 at 16:17 Comment(5)
Bro do you have any idea how to refresh activity contents (layout and text) without restarting activity. and if there is no way to refresh the contents without restarting. then how can i refresh the contents of those activities which are currently in pause state. because when i'll restart them then i'll lost their data and their context. how to overcome this problem. ThanksCorse
IMO, you can keep data in public static variables.Creationism
i think that's not an efficient solution.Corse
Another option is using intent.putExtra :)Creationism
thats what i am doing right now :P but i think its not a good way tooCorse

© 2022 - 2024 — McMap. All rights reserved.