2

In my project I have to implement multi language option. For this I wrote the following code.

Locale locale = new Locale(Language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());

Image for reference: enter image description here

Here I want to update button text in selected language. For this I try the following way.

In my activity.

My fragment code is:

public class LanguageFragment extends ListFragment {

String[] language_id = new String[] { "", "tn", "ja" };
ListView lv;
SharedPreferences settings;
SharedPreferences.Editor editor;
onLanguageSelectedListener mListener;
int pos;

public interface onLanguageSelectedListener {
    void setonLanguage();
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    lv = getListView();
    System.out.println("getlistview");

    String english = getActivity().getString(R.string.english) + "";
    String tamil = getActivity().getText(R.string.tamil) + "";
    String japanse = getActivity().getText(R.string.japanese) + "";
    String[] language_list = new String[] { english, tamil, japanse };

    settings = getActivity().getSharedPreferences("LANGUAGE_TYPE", 0);
    ListAdapter adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, language_list) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (convertView == null) {
                LayoutInflater li = (LayoutInflater) getActivity()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = li.inflate(android.R.layout.simple_list_item_1, null);
            }
            if (lv.isItemChecked(position)) {
                // v.setActivated(true);
                editor = settings.edit();
                editor.putString("Position", lv.getCheckedItemPosition() + "");
                editor.putString("Language",
                        language_id[lv.getCheckedItemPosition()].toString());
                editor.commit();
                mListener = (onLanguageSelectedListener) getActivity();
                mListener.setonLanguage();
                v.setBackgroundResource(R.drawable.blue_top1);
            } else {
                v.setBackgroundResource(0);
            }
            return super.getView(position, v, parent);
        }
    };
    String s = settings.getString("Position", 0 + "");
    pos = Integer.parseInt(s);
    setListAdapter(adapter);
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    lv.setItemChecked(pos, true);
    lv.setSelector(R.drawable.blue_top1);
}

}

And my activity is:

public class SettingsActivity extends Activity implements
    OnItemSelectedListener, onLanguageSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SharedPreferences settings = getSharedPreferences("LANGUAGE_TYPE", 0);
    String Language = settings.getString("Language", "");

    Locale locale = new Locale(Language);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());

    setContentView(R.layout.settings_activity);
}

public void onSettingsItemSelected(String link) {
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();

    Fragment language;
    if ("language".equals(link)) {
        language = new LanguageFragment();
        transaction.replace(R.id.detailFragment, language);
        transaction.commit();
    }
}

public void setonLanguage() {
    SharedPreferences settings = getSharedPreferences("LANGUAGE_TYPE", 0);
    String Language = settings.getString("Language", "");
    Locale locale = new Locale(Language);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());

    name = getText(R.string.hi) + " " + name;
//      Intent intent = getIntent();
//      overridePendingTransition(0, 0);
//      intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
//      finish();
//
//      overridePendingTransition(0, 0);
//      startActivity(intent);
    System.out.println("inside listener : " + Language);
}
}

But this works on after restart the activity only. I want to update on click of the list view. I know it is possible. But I don't know how to achieve it.

halfer
  • 19,824
  • 17
  • 99
  • 186
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • Can you be more clear on what you want to do? When do you want the text of the button to change? Once you have selected a different item from the list or only after you click the save button or something like that? – Amulya Khare Oct 04 '13 at 07:46
  • Once selected a different item from the list. – Gunaseelan Oct 04 '13 at 07:47

2 Answers2

1

There are two ways you could solve this problem:

Method 1:

You can override the onItemClick in the list fragment and in that method, update the text of the the button via code.

Update your listener:

public interface onLanguageSelectedListener {
    void setonLanguage();
    void onLanguageItemSelected(String language);
}

Invoke the callback on item click:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
   mListener = (onLanguageSelectedListener) getActivity();
   mListener.onLanguageItemSelected(language_id[position].toString());
}

Then inside the activity implement the onLanguageSelected() and update the button text.

This way has its limitations because if there are more views (other than the button) which you want to update, then you would have to update each of them one by one.

Method 2:

The more generic solution to this problem is to reload the content view after the configuration changes. THis method is described here: https://stackoverflow.com/a/11610910/827110

However, since you want to update the text every time a different item is selected, method 2 might not be so suitable.

Community
  • 1
  • 1
Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
0

First, in AdroidManifest.xml, declare that you will handle locale change for all activities:

<activity android:name="YourActivity" android:configChanges="locale" />

Secondly, you need to handle onConfigurationChanged() callback in all activity classes:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Change text for all views
    textView.setText(getString(R.string.text));
    ...
}
K T
  • 81
  • 1
  • 5