2

I have searched the numerous questions that look like this one, but haven't found my answer in any of them. i want set Local Default to English from fragment , i used this code to onStart() and onAttach() and onCreateView() but didn't work

My Fragment Code :

package com.demo.tomcatfire.taskmanagerui;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.Locale;

public class MainFragment2 extends Fragment implements View.OnClickListener {
    private LinearLayout ll_display,ll_add,ll_about,ll_about2,ll_learnig,ll_learnig2;


    @Override
    public void onAttach(Context context) {

        Locale locale = new Locale("en");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        context.getResources().updateConfiguration(config,
                context.getResources().getDisplayMetrics());
    }

    @Override
    public void onStart() {

        Locale locale = new Locale("en");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getContext().getResources().updateConfiguration(config,
                getContext().getResources().getDisplayMetrics());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main2, container, false);

        ll_about=(LinearLayout) rootView.findViewById(R.id.LL_aboute);
        ll_about2=(LinearLayout) rootView.findViewById(R.id.LL_aboute2);
        ll_display=(LinearLayout)rootView.findViewById(R.id.LL_display);
        ll_learnig=(LinearLayout) rootView.findViewById(R.id.LL_learning);
        ll_learnig2=(LinearLayout) rootView.findViewById(R.id.LL_learning2);
        ll_add=(LinearLayout)rootView.findViewById(R.id.LL_add);
        //------------------------------------------------------------------------------------------
        ll_add.setOnClickListener(this);
        ll_display.setOnClickListener(this);
        ll_learnig.setOnClickListener(this);
        ll_learnig2.setOnClickListener(this);
        ll_about.setOnClickListener(this);
        ll_about2.setOnClickListener(this);



        return rootView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.LL_aboute:
                break;
            case R.id.LL_aboute2:
                break;
            case R.id.LL_add:
                startActivity(new Intent(getContext(),AddTaskActivity.class));
                break;
            case R.id.LL_learning:
                break;
            case R.id.LL_learning2:
                break;
            case R.id.LL_display:
                startActivity(new Intent(getContext(),DisplayFinalActivity.class));

                break;

        }

    }

    @Override
    public void onResume() {
        Locale locale = new Locale("en");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getActivity().getBaseContext().getResources().updateConfiguration(config,
                getActivity().getBaseContext().getResources().getDisplayMetrics());
    }
}
Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
EhSAn
  • 111
  • 3
  • 12

1 Answers1

7

check if onAttach(Context context) is getting called?

Solution: Need to use Support library fragment as you are using because this method has been added in API 23 . I tested with following code. It is working.

 @Override
    public void onAttach(Context context) {
        super.onAttach(context); // this statement is missing
        Locale locale = new Locale("en");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        context.getResources().updateConfiguration(config,
                context.getResources().getDisplayMetrics());
    }

In addition You only need to change locale in the onAttach() method.

Hope this helps you.

Community
  • 1
  • 1
Sandeep Sharma
  • 1,855
  • 3
  • 19
  • 34