Skip to content

Instantly share code, notes, and snippets.

@VladSumtsov
Last active August 29, 2015 14:20
Show Gist options
  • Save VladSumtsov/459fa8e8dbe48706fc8b to your computer and use it in GitHub Desktop.
Save VladSumtsov/459fa8e8dbe48706fc8b to your computer and use it in GitHub Desktop.
package com.corewillsoft.usetool.ui.fragments;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.corewillsoft.usetool.R;
import com.google.inject.Inject;
import java.util.Locale;
import de.greenrobot.event.EventBus;
import roboguice.RoboGuice;
/**
* Represents supported by the app languages
*
* @see com.corewillsoft.usetool.utils.LanguageSwitcher
*/
public class LanguagesFragment extends PreferenceFragment {
public static final String KEY_SELECTED_LANGUAGE = "KEY_SELECTED_LANGUAGE";
/**
* Supported by the app languages
*/
public enum SupportedLanguage {
DEFAULT(Locale.getDefault()),
ENGLISH(Locale.ENGLISH),
GERMAN(Locale.GERMAN),
SPANISH(new Locale("es")),
RUSSIAN(new Locale("ru")),
CHINESE(Locale.SIMPLIFIED_CHINESE),
POLISH(new Locale("pl")),
ITALIAN(Locale.ITALIAN),
PORTUGUESE(new Locale("pt"));
public final Locale locale;
SupportedLanguage(Locale locale) {
this.locale = locale;
}
/**
* @param context
* @return language names in it's specific locale
*/
public String getDisplayName(Context context) {
switch (this) {
case DEFAULT:
return context.getString(R.string.settings_default_language_title, getCapitalizedLanguageName());
default:
return getCapitalizedLanguageName();
}
}
private String getCapitalizedLanguageName() {
StringBuilder sb = new StringBuilder(locale.getDisplayLanguage(locale));
if (!TextUtils.isEmpty(sb)) {
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
}
return sb.toString();
}
}
@Inject
private SharedPreferences preferences;
@Inject
private Resources resources;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RoboGuice.getInjector(getActivity()).injectMembersWithoutViews(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_languages, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ListView listView = (ListView) view;
listView.setAdapter(new ArrayAdapter<SupportedLanguage>(getActivity(), R.layout.preference_list_item, SupportedLanguage.values()) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setText(getItem(position).getDisplayName(getActivity()));
return view;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
SupportedLanguage language = (SupportedLanguage) adapterView.getItemAtPosition(i);
Locale.setDefault(language.locale);
Configuration c = new Configuration();
c.locale = language.locale;
resources.updateConfiguration(c, resources.getDisplayMetrics());
preferences.edit().putInt(KEY_SELECTED_LANGUAGE, language.ordinal()).apply();
getActivity().finish();
}
});
((View)view.getParent().getParent()).setBackgroundResource(R.color.settings_background);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment