Change Language of Android App programmatically using Java.

Seerat Abbas Khan
2 min readJan 11, 2021

So here is from my side. it works perfectly for me.

**Follow these steps to change Language.**

1.Save your strings in folder like values-ur(URDU)

language strings

then convert your all strings to that language you have to convert.

2.Make a BaseActivity class and extend all your rest Activities to that BaseActivity class.

public class BaseActivity extends AppCompatActivity
{
public BaseActivity context;
public MySharePreference mySharePreference;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
context = BaseActivity.this;
mySharePreference = MySharePreference.getInstance(context);
LocaleHelper.setLocale(context, mySharePreference.getLanguage() );
}

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

}

3.I have ExampleActivity that i want to translate its language.

public class ExampleActivity extends BaseActivity {

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

simply extend your application to your BaseActivity class.

3.Make a LocalHelper class in which it change the layout-direction and strings.

public class LocaleHelper
{

public static Context setLocale(Context context, String language)
{
Locale locale = new Locale(language);
Locale.setDefault(locale);

Resources resources = context.getResources();

Configuration configuration = new Configuration(resources.getConfiguration());
configuration.setLayoutDirection(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
configuration.setLocale(locale);
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
}
else
{
configuration.locale = locale;
configuration.setLocale(locale);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1)
{
return context.createConfigurationContext(configuration);
}
else
{
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
}

public static Context onAttach(Context context)
{
return setLocale(context, MySharePreference.getInstance(context).getLanguage());
}
}

4.In your App class attach base context.

public class AppClass extends MultiDexApplication
{

private static AppClass appClass;
public static AppClass getintance()
{
return appClass;
}


@Override
public void onCreate()
{
super.onCreate();
appClass = this;
MySharePreference.getInstance(this);
}


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

}

5.In your build.gradle file add latest androidx SharedPreferences dependency.

implementation 'androidx.preference:preference:1.1.1'

6.In my caseI make my Own SharedPreferences class.

public class MySharePreference
{
private static MySharePreference instance;
private static SharedPreferences pref;

private MySharePreference(Context context)
{
if (context != null)
{
pref = PreferenceManager.getDefaultSharedPreferences(context);
}
else
{
pref = PreferenceManager.getDefaultSharedPreferences(App.getintance());
}
}

public static MySharePreference getInstance(Context context)
{
if (instance == null || pref == null)
{
instance = new MySharePreference(context);
}
return instance;
}


public String getLanguage()
{
return pref.getString("appLanguage", "en");
}

public void setLanguage(String b)
{
pref.edit().putString("appLanguage", b).apply();
}
}

7.At last save the abbreviation of that language to SharedPreferences in my case Ihave spinner.

spinner_lang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if (mySharePreference.getLanguagePosition() != position)
{
mySharePreference.setLanguage(Constants.COUNTRY_LIST.get(position).countryAbbr);
mySharePreference.setLanguagePosition(position);
startActivity(new Intent(mActivity, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
}

@Override
public void onNothingSelected(AdapterView<?> parent)
{

}
});

now all done……

--

--