2

I am developing an android application in which i am using fragments. And in that I am change language from English to Arabic after click on list item and its work fine.

But What i want that the language which is selected is remain in whole application even after we force close application from device.... But it doesn't happen with me....

If i am using with Activity then i have call

setContentView(R.layout.main); 

in onResume() Method and its work fine.

But my question is that how can we set a view with fragment or call onCreateView method on onResume() method because onCreateView() method returns itself view.. So have tried a lot to solve it but i couldn't.. So please is there any way to solve it??

Thanks in Advance.

This is my Code:

package com.example.okazz.Settings;

import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.okazz.R;

public class Languages extends Fragment {
// private int current = -1;
View view;
Context ct;
SharedPreferences sp1, sp;
Boolean Ischeck = false;
String User_id, countryid;
Button settings;
String[] arrLanguages = new String[] { "English", "Arabic" };
String req;
ListView lv;
private Locale myLocale;

public Languages(Context c) {

    this.ct = c;
}

/** Called when the activity is first created. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    view = inflater.inflate(R.layout.languagechange, container, false);

    SharedPreferences sp = getActivity().getSharedPreferences("key", 0);
    User_id = sp.getString("userid=", User_id);
    countryid = sp.getString("countryid", countryid);
    settings = (Button) view.findViewById(R.id.btnsett);
    lv = (ListView) view.findViewById(android.R.id.list);
    lv.setAdapter(new MobileArrayAdapter(ct, arrLanguages));
    settings.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            FragmentManager manager = getFragmentManager();
            manager.popBackStack();
            Log.d("Back to Home", "back button pressed");
        }
    });

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            // get selected items
            String selectedValue = (String) lv.getAdapter().getItem(
                    position);
            Toast.makeText(ct, selectedValue, Toast.LENGTH_SHORT).show();
            // setResult(RESULT_OK, new Intent().putExtra("language",
            // arrLanguages[position]));
            // Configuration config = new Configuration();
            String lang = null;
            switch (position) {
            case 0:
                lang = "en";
                break;
            case 1:
                lang = "ar";
                break;

            default:

                break;
            }

            changeLang(lang);

            // SharedPreferences sp = getActivity().getSharedPreferences(
            // "check", 0);
            //
            // boolean checked = sp.getBoolean("Ischeck", false);
            //
            // if (checked == true) {
            lv.setItemChecked(position, true);
            // }

        }

    });
    return view;
}

public void loadLocale() {
    String langPref = "Language";
    SharedPreferences prefs = ct.getSharedPreferences("CommonPrefs",
            Activity.MODE_PRIVATE);
    String language = prefs.getString(langPref, "");
    changeLang(language);

}

public void saveLocale(String lang) {
    String langPref = "Language";
    SharedPreferences prefs = ct.getSharedPreferences("CommonPrefs",
            Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(langPref, lang);
    editor.commit();
}

public void changeLang(String lang) {
    if (lang.equalsIgnoreCase(""))
        return;
    myLocale = new Locale(lang);
    Log.v("My Language", myLocale + "");
    saveLocale(lang);
    Locale.setDefault(myLocale);
    android.content.res.Configuration config = new android.content.res.Configuration();
    config.locale = myLocale;
    ct.getResources().updateConfiguration(config,
            ct.getResources().getDisplayMetrics());

}

class MobileArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MobileArrayAdapter(Context context, String[] values) {
        super(context, R.layout.list_changelanguage, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_changelanguage,
                parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.textView1);
        ImageView imageView = (ImageView) rowView
                .findViewById(R.id.imageView1);
        textView.setText(values[position]);

        String s = values[position];

        System.out.println("**** Position ****" + s);
        if (lv.isItemChecked(position)) {
            imageView.setVisibility(View.VISIBLE);
            Ischeck = true;
            SharedPreferences sp = getActivity().getSharedPreferences(
                    "check", 0);
            SharedPreferences.Editor sedt = sp.edit();
            sedt.putBoolean("Ischeck", Ischeck);
            sedt.clear();
            sedt.commit();
        } else {
            imageView.setVisibility(View.GONE);

        }

        return rowView;
    }
}

@Override
public void onConfigurationChanged(
        android.content.res.Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (myLocale != null) {
        newConfig.locale = myLocale;
        Locale.setDefault(myLocale);
        getActivity().getResources().updateConfiguration(newConfig,
                getActivity().getResources().getDisplayMetrics());
    }
}

@Override
public void onResume() {
    // TODO Auto-generated method stub
    loadLocale();
    super.onResume();
}}
Shani Goriwal
  • 2,111
  • 1
  • 18
  • 30
  • I understand what you're asking. You want whatever you do in `onCreateView` to happen in `onResume` but `onResume` doesn't return a view. – CodyBugstein Oct 01 '14 at 14:36

2 Answers2

0

Your question is a bit hard to read, but I think I got it : you want to re-layout a fragment as the language changes.

I guess the best for fragements could be to remove the fragment and then re-add a new instance to your activity.

An other option is to use the onConfigurationChange methods of both the activity and the fragments inside it : Changing locale: Force activity to reload resources?.

You could fire this event yourself by doing something like :

int lang = //the result of your click inside the list item
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();

switch(lang){
    case ARABIC:
        conf.locale = Locale.WHATEVER;
        break;

    case ENGLISH:
        conf.locale = Locale.ENGLISH;
        break;
}
res.updateConfiguration(conf, dm);
Community
  • 1
  • 1
Snicolas
  • 37,840
  • 15
  • 114
  • 173
0

You can save your preference of language by using SharedPreferences and recreate the view with the preferred language in onCreate

glo
  • 1,408
  • 3
  • 25
  • 54