Monday, 31 July 2017

Android Change Language Programmatically

If You Want To Change Language Then You Want To Make Second Directory Of Value With Language Code And You Need To Diffine Your Hindi Text And Some Classes



#ref-menu

1) LocaleHelper.Java

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;

import java.util.Locale;

public class LocaleHelper {

    private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";

    public static Context onAttach(Context context) {
        String lang = getPersistedData(context, Locale.getDefault().getLanguage());
        return setLocale(context, lang);
    }

    public static Context onAttach(Context context, String defaultLanguage) {
        String lang = getPersistedData(context, defaultLanguage);
        return setLocale(context, lang);
    }

    public static String getLanguage(Context context) {
        return getPersistedData(context, Locale.getDefault().getLanguage());
    }

    public static Context setLocale(Context context, String language) {
        persist(context, language);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResources(context, language);
        }

        return updateResourcesLegacy(context, language);
    }

    private static String getPersistedData(Context context, String defaultLanguage) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
    }

    private static void persist(Context context, String language) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();

        editor.putString(SELECTED_LANGUAGE, language);
        editor.apply();
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Context updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Configuration configuration = context.getResources().getConfiguration();
        configuration.setLocale(locale);
        configuration.setLayoutDirection(locale);

        return context.createConfigurationContext(configuration);
    }

    @SuppressWarnings("deprecation")
    private static Context updateResourcesLegacy(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Resources resources = context.getResources();

        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLayoutDirection(locale);
        }

        resources.updateConfiguration(configuration, resources.getDisplayMetrics());

        return context;
    }

}


2) MainApplication.Java

import android.app.Application;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;

public class MainApplication extends AppCompatActivity {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(LocaleHelper.onAttach(base, "en"));
    }

}


3) MainActivity.Java

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

   
    TextView mTitleTextView;
  
    TextView mDescTextView;

    TextView mAboutTextView; 

    Button mToTRButton;
   
    Button mToENButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitleTextView = (TextView)findViewById(R.id.titleTextView);
        mDescTextView = (TextView)findViewById(R.id.descTextView);
        mAboutTextView = (TextView)findViewById(R.id.aboutTextView);
        mToTRButton = (Button) findViewById(R.id.toHIButton);
        mToENButton = (Button)findViewById(R.id.toENButton);

        mToHIButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateViews("hi");
            }
        });

        mToENButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateViews("en");
            }
        });

        setTitle(getString(R.string.main_activity_toolbar_title));

    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(LocaleHelper.onAttach(base));
    }



    private void updateViews(String languageCode) {
        Context context = LocaleHelper.setLocale(this, languageCode);
        Resources resources = context.getResources();

 mTitleTextView.setText(resources.getString(R.string.main_activity_title));
 mDescTextView.setText(resources.getString(R.string.main_activity_desc));
 mAboutTextView.setText(resources.getString(R.string.main_activity_about));
        mToHIButton.setText(resources.getString(R.string.main_activity_to_tr_button));
        mToENButton.setText(resources.getString(R.string.main_activity_to_en_button));

        setTitle(resources.getString(R.string.main_activity_toolbar_title));
    }

}


4) Second Directory Of Value-hi

value-hi Containt Only One File "String.xml"

Normal Sring.xml File In Value Folder

<resources>
    <string name="app_name">Change Language</string>

    <string name="main_activity_title">This title is in English</string>
    <string name="main_activity_desc">This is the description text in English.</string>
    <string name="main_activity_about">This is about text in English.</string>
    <string name="main_activity_to_tr_button">हिंदी</string>
    <string name="main_activity_to_en_button">English</string>
    <string name="main_activity_toolbar_title">Title in English</string>

</resources>

Our value-hi File In Value Folder

<resources>
    <string name="app_name">भाषा बदलो</string>
    <string name="main_activity_title">यह शीर्षक हिंदी में है</string>
    <string name="main_activity_desc">यह हिंदी में वर्णन टेक्स्ट है</string>
    <string name="main_activity_about">यह हिंदी में पाठ के बारे में है</string>
    <string name="main_activity_to_tr_button">Turkish</string>
    <string name="main_activity_to_en_button">English</string>
    <string name="main_activity_toolbar_title">हिंदी में शीर्षक</string>

</resources>


Note : This Is Hindi Title You Can Change Your Language As Per Need


Download Source Code : 
https://drive.google.com/open?id=0B7p5TzrAq_QgNXVRNXZGdmdqbkk

1 comment:

Hayata Dair Her Şey said...

Hello,

thank you. But the language doesn't change for raw folder.

How can I change the language for raw files?

I need your help.

Android Studio - Get Current Latitude And Longitude

1. Add this dependencies in your gradle file     implementation 'com.google.android.gms:play-services-location:18.0.0' 2. Add this p...