Reputation: 139
I am trying to change the locale
of application when selecting options from the onOptionsItemSelected
.
When I try to do so, the language gets successfully changed in the same menu.
However, the text
in the TextView
present in the layout doesn't change.
Any help is appreciated.
My MainActivity is:
public class MainActivity extends AppCompatActivity {
TextView textView;
Locale myLocale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
textView = (TextView) findViewById(R.id.textView1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_english) {
Toast.makeText(getApplicationContext(),
"You have selected English", Toast.LENGTH_SHORT)
.show();
setLocale("en");
return true;
} else if (id == R.id.action_nepal) {
Toast.makeText(getApplicationContext(),
"You have selected Nepali", Toast.LENGTH_SHORT)
.show();
setLocale("ne");
return true;
}
return super.onOptionsItemSelected(item);
}
public void setLocale(String lang) {
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
getBaseContext().getResources().updateConfiguration(conf,
getBaseContext().getResources().getDisplayMetrics());
invalidateOptionsMenu();
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// refresh your views here
super.onConfigurationChanged(newConfig);
}
}
My Manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.languagesupport">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:configChanges="locale"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And both my strings file :
For English:
<resources>
<string name="app_name">languageChange</string>
<string name="sample_text">Hello World</string>
<string name="action_settings">Settings</string>
<string name="english">English</string>
<string name="nepal">Nepal</string>
</resources>
For Nepali:
<resources>
<string name="app_name">भीमदत्त नगरपालिका</string>
<string name="action_settings">गृहपृष्ठ</string>
<string name="sample_text">" विकास क्षेत्रमा पर्ने महाकाली अञ्चल), कञ्चनपुर जिल्लाको स"</string>
<string name="english">अंग्रेजी</string>
<string name="nepal">नेपाल</string>
</resources>
Upvotes: 1
Views: 2885
Reputation: 24907
There are two different approaches:
1. Without Activity restart:
Set android:configChanges="locale"
in your <activity>
in manifest
Change your setLocale as follows:
private void setLocale(String lang) {
Locale myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
getBaseContext().getResources().updateConfiguration(conf, getBaseContext().getResources().getDisplayMetrics());
invalidateOptionsMenu();
onConfigurationChanged(conf);//Add this line
}
Override onConfigurationChanged()
:
@Override
public void onConfigurationChanged(final Configuration newConfig) {
super.onConfigurationChanged(newConfig);
textView.setText(<your-text>);
//Any other UI text to change
}
This will ensure your locale is changed correctly.
2. Recreate the activity:
Instead of
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
finish();
CALL
recreate();
This will cause this Activity to be recreated with a new instance.
Upvotes: 2
Reputation: 69689
Try this use finish();
after restarting your activity
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
finish();
Upvotes: 1