Trying to add Multiple languages in my Android App

Viewed 61

I make a simple android App which have a Login activity and one fragment(BottomSheetDialogFragment) I am trying to add multiple languages through the fragment (which have multiple buttons) to main activity(Login Activity) but its not working here below the code can some one help me please?

Login Activity

public class LoginActivity extends AppCompatActivity {


    private ActivityLoginBinding binding;

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

        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomnav bottomnav=new bottomnav();
                bottomnav.show(getSupportFragmentManager(),bottomnav.getTag());

            }
        });


    }
}

Fragment

public class bottomnav extends BottomSheetDialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_bottomnav, container, false);
        Button urdu,hindi,arabic,eng;
        eng=view.findViewById(R.id.english);
        urdu=view.findViewById(R.id.urdu);
        hindi=view.findViewById(R.id.hindi);
        arabic=view.findViewById(R.id.arabic);
        eng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        return view;

        }

    private void setlocale(String language) {
        Locale locale= new Locale(language);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getContext().getResources().updateConfiguration(config,
                getContext().getResources().getDisplayMetrics());
    }
    public void onClick(View v) {

        switch(v.getId()) {
            case R.id.english:
                setlocale("");

                break;
            case R.id.urdu:
                setlocale("ur");

                break;
            case R.id.hindi:
                setlocale("hi");

                break;
            case R.id.arabic:
                setlocale("ar");

                break;

            default:
        }
}
}

Login activity Fragment

1 Answers

You need to refresh the activity for the language changes to apply

Related