-1

i have array string at res->value->strings like below :

<string-array name="language">
    <item>France</item>
    <item>English</item>
</string-array>

and now, inside onItemSelected i want to make if select france then Toast fance and change language to france, when i select english then Toast english and change language to english.

public class SPinnerActivity extends Activity implements 
AdapterView.OnItemSelectedListener{
    public void onItemSelected(AdapterView<?> parent,View view,int pos,long id){


    }
    public void onNothingSelected(AdapterView<?> parent){

    }
}

here is method for change language :

 protected void setLanguage (String language){
    mylocale = new Locale(language);
    Resources resources = getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration conf = resources.getConfiguration();
    conf.locale = mylocale;
    resources.updateConfiguration(conf,dm);

}
asqa
  • 199
  • 3
  • 17

3 Answers3

1

if you want to use the switch case then do the following

public class SPinnerActivity extends Activity implements 
AdapterView.OnItemSelectedListener{
    public void onItemSelected(AdapterView<?> parent,View view,int pos,long id){

          switch (pos){
                    case 0:
                        Toast.makeText(context,"France",Toast.LENGTH_LONG).show();
                        break;
                    case 1:
                        Toast.makeText(context,"English",Toast.LENGTH_LONG).show();
                        break;

                }
    }
    public void onNothingSelected(AdapterView<?> parent){

    }
}

Note: This is not the good way to do that , Since it is not the direct way to do it.

What I will suggest is to show something like this

Load your array as arrayList

ArrayList yourLanguageList = load from your strig xml as array list;

then do this in onItemSelected

AdapterView.OnItemSelectedListener{
public void onItemSelected(AdapterView<?> parent,View view,int pos,long id){

Toast.makeText(context,yourLanguageList.get(pos).toString,Toast.LENGTH_LONG)
.show();

}
public void onNothingSelected(AdapterView<?> parent){

}

}

A.s.ALI
  • 1,992
  • 3
  • 22
  • 54
  • Second way **is also not the good way** .. as ArrayAdapter may be filtered - so obviously `yourLanguageList.get(pos)` will return wrong item – Selvin Jan 16 '18 at 11:43
  • then he can get it from the view returned from the spinner – A.s.ALI Jan 17 '18 at 07:58
1

Try this

 public class SPinnerActivity extends Activity implements

     String language = ""; 
    AdapterView.OnItemSelectedListener{
        public void onItemSelected(AdapterView<?> parent,View view,int pos,long id){
        switch(position){

        case 0:
        language ="France";
       break;  

     case 0:
        language ="English";
         break;            
      }
        setLanguage (language )
        }
        public void onNothingSelected(AdapterView<?> parent){

        }
    }

 protected void setLanguage (String language){
    mylocale = new Locale(language);
    Resources resources = getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration conf = resources.getConfiguration();
    conf.locale = mylocale;
    resources.updateConfiguration(conf,dm);
    Toast.makeText(this,language,Toast.LENGTH_SHORT).show();  

}
Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
0

You can load the array items in xml itself, refer the below code

<Spinner 
   android:id="@+id/spinner"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:drawSelectorOnTop="true"
   android:entries="@array/languages"/>  //your xml arrayhere


public class MyActivity extends Activity {

private String[] string_array;

@Override
public void onCreate() {
    super.onCreate();
    //string_array = getResources().getStringArray(R.array.languages); 

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            String items = spinner.getSelectedItem().toString();
            Log.i("Selected item : ", items);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }

    });
}

}
SaravInfern
  • 3,338
  • 1
  • 20
  • 44