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:
public class SettingsFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {
Spinner langspinner;
public SettingsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false);
Button settupdatebtn = (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;
}
@Override
public void onItemSelected(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);
} else if (pos == 2) {
Toast.makeText(parent.getContext(),
"Has Seleccionado Español!", Toast.LENGTH_SHORT)
.show();
setLocale("es");
langspinner.setSelection(2);
} else if (pos == 3) {
Toast.makeText(parent.getContext(),
"Vous Avez Sélectionné Le Français!", Toast.LENGTH_SHORT)
.show();
setLocale("fr");
langspinner.setSelection(3);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@Override
public void onClick(View v) {
SettingsFragment fragment = new SettingsFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
Toast.makeText(getActivity(), "Settings Updated!", Toast.LENGTH_SHORT).show();
}
Locale myLocale;
public void setLocale(String lang) {
myLocale = new Locale(lang);
Locale.setDefault(myLocale);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
refresh();
}
public void refresh() {
Fragment currentFragment = getFragmentManager().findFragmentByTag("fragment_tag_String");
FragmentTransaction fragTransaction = 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;
SettingsFragment fragmentA = new SettingsFragment();
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.
Fragment currentFragment = getFragmentManager().findFragmentByTag("FRAGMENT");
FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
fragTransaction.detach(currentFragment);
fragTransaction.attach(currentFragment);
fragTransaction.commit();
Set fragmentTag for SettingsFragment in MainActivity where you called this fragment. (ex: OnNavigationItemSelected() method).
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new YourSettingsFragment(),"Tag").commit();
Post a Comment for "How To Make A Spinner (In A Fragment) That Changes The App's Language?"