Skip to content Skip to sidebar Skip to footer

How To Make A Spinner (in A Fragment) That Changes The App's Language?

My app was almost fully complete when I realized I needed to use fragments instead of activities in order to share the same Navigation Menu across all of the screens, so now I'm in

Solution 1:

try this:

publicclassSettingsFragmentextendsFragmentimplementsView.OnClickListener, AdapterView.OnItemSelectedListener {

    Spinner langspinner;

    publicSettingsFragment() {
        // Required empty public constructor
    }

    @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Viewview= inflater.inflate(R.layout.fragment_settings, container, false);
        Buttonsettupdatebtn= (Button) view.findViewById(R.id.setting_update_btn);
        settupdatebtn.setOnClickListener(this);

        langspinner = (Spinner) view.findViewById(R.id.settings_language_spinner);
        langspinner.setOnItemSelectedListener(this);
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.lang_array, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        langspinner.setAdapter(adapter);
        langspinner.setSelection(0, false);
        return view;
    }

    @OverridepublicvoidonItemSelected(AdapterView<?> parent, View view, int pos, long id) {

        if (pos == 1) {

            Toast.makeText(parent.getContext(),
                    "You Have Selected English!", Toast.LENGTH_SHORT)
                    .show();
            setLocale("en");
            langspinner.setSelection(1);

        } elseif (pos == 2) {

            Toast.makeText(parent.getContext(),
                    "Has Seleccionado Español!", Toast.LENGTH_SHORT)
                    .show();
            setLocale("es");
            langspinner.setSelection(2);

        } elseif (pos == 3) {

            Toast.makeText(parent.getContext(),
                    "Vous Avez Sélectionné Le Français!", Toast.LENGTH_SHORT)
                    .show();
            setLocale("fr");
            langspinner.setSelection(3);
        }
    }

    publicvoidonNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }


    @OverridepublicvoidonClick(View v) {
        SettingsFragmentfragment=newSettingsFragment();
        android.support.v4.app.FragmentTransactionfragmentTransaction=
                getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
        Toast.makeText(getActivity(), "Settings Updated!", Toast.LENGTH_SHORT).show();
    }


    Locale myLocale;

    publicvoidsetLocale(String lang) {
        myLocale = newLocale(lang);
        Locale.setDefault(myLocale);
        Resourcesres= getResources();
        DisplayMetricsdm= res.getDisplayMetrics();
        Configurationconf= res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        refresh();


    }

    publicvoidrefresh() {
        FragmentcurrentFragment= getFragmentManager().findFragmentByTag("fragment_tag_String");
        FragmentTransactionfragTransaction= getFragmentManager().beginTransaction();
        fragTransaction.detach(currentFragment);
        fragTransaction.attach(currentFragment);
        fragTransaction.commit();

    }

}

Make sure when you load the SettingsFragment from your activity u add the Fragment tag

Like this;

SettingsFragmentfragmentA=newSettingsFragment();
getSupportFragmentManager().beginTransaction()
    .replace(R.id.MainFrameLayout,fragmentA,"fragment_tag_String")
    .addToBackStack(null).commit(); 

Solution 2:

You may just detach and attach your fragments. Then it will refresh the views and the local will be changed.

FragmentcurrentFragment= getFragmentManager().findFragmentByTag("FRAGMENT");
    FragmentTransactionfragTransaction= getFragmentManager().beginTransaction();
    fragTransaction.detach(currentFragment);
    fragTransaction.attach(currentFragment);
    fragTransaction.commit();

Set fragmentTag for SettingsFragment in MainActivity where you called this fragment. (ex: OnNavigationItemSelected() method).

FragmentManagerfm= getFragmentManager();
    fm.beginTransaction().replace(R.id.content_frame, newYourSettingsFragment(),"Tag").commit();

Post a Comment for "How To Make A Spinner (in A Fragment) That Changes The App's Language?"