Skip to content

Instantly share code, notes, and snippets.

@Ebaneck
Last active May 5, 2017 10:58
Show Gist options
  • Select an option

  • Save Ebaneck/b874b408371ff2121a99bd8cf32c82e9 to your computer and use it in GitHub Desktop.

Select an option

Save Ebaneck/b874b408371ff2121a99bd8cf32c82e9 to your computer and use it in GitHub Desktop.
Android how to change the application language at runtime and make it persistent using preference manager
package com.nkdroid.firstaid.translate_app;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Locale;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.AdapterView.OnItemSelectedListener;
import com.nkdroid.firstaid.R;
public class HelloActivity extends AppCompatActivity {
private Spinner countrySpinner;
private Spinner genderSpinner;
private String key= "Help Yourself";
private boolean isitShowed;
private View firstView;
private Button startButton;
private TextView ageTextView;
private TextView termsTextView;
Locale myLocale;
@Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
boolean ciao_1=preferences.getBoolean(key,isitShowed);
//Log.d("intro",ciao+"");
if(ciao_1){
loadMainActivity();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_localize);
// Prevent SoftKeyboard to pop up on start
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
countrySpinner = (Spinner) findViewById(R.id.activity_hello_spinner_country);
// genderSpinner = (Spinner) findViewById(R.id.activity_hello_spinner_gender);
startButton = (Button) findViewById(R.id.activity_hello_button_start);
termsTextView = (TextView) findViewById(R.id.helloactivity_textview_terms);
// ageTextView = (TextView) findViewById(R.id.activity_hello_age);
countrySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
if (pos == 1) {
Toast.makeText(parent.getContext(),
"You have selected English", Toast.LENGTH_SHORT)
.show();
setLocale("en");
} else if (pos == 2) {
Toast.makeText(parent.getContext(),
"Vous avez choisi le français", Toast.LENGTH_SHORT)
.show();
setLocale("fr");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
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);
startActivity(new Intent(HelloActivity.this, com.nkdroid.firstaid.doctorfinder.activity.access.SplashActivity.class));
finish();
}
private void loadMainActivity() {;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
isitShowed=true;
editor.putBoolean(key, isitShowed);
editor.apply();
Intent intent = new Intent(this,com.nkdroid.firstaid.doctorfinder.activity.access.SplashActivity.class);
// Intent intent = new Intent(this,com.nkdroid.firstaid.HomeActivity.class);
startActivity(intent);
finish();
}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/helloactivity_mainframe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:context="com.nkdroid.firstaid.translate_app.HelloActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="32dp"
android:paddingLeft="32dp"
android:paddingRight="32dp"
android:paddingTop="32dp">
<TextView
android:id="@+id/activity_hello_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="32dp"
android:text="@string/helloactivity_headline"
android:textColor="#000000"
android:textSize="@dimen/abc_text_size_display_1_material" />
<TextView
android:id="@+id/activity_hello_subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:text="@string/helloactivity_subhead"
android:textColor="#323232"
android:textSize="@dimen/abc_text_size_subhead_material" />
<Spinner
android:id="@+id/activity_hello_spinner_country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:entries="@array/languages"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/helloactivity_hint_settings"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="@string/helloactivity_textview_tos"
android:textColor="#323232"
android:textStyle="bold" />
<TextView
android:id="@+id/helloactivity_textview_terms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/helloactivity_terms"
android:textColor="#07A7CE"
android:textStyle="bold" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/activity_hello_button_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="24dp"
android:background="?android:attr/selectableItemBackground"
android:drawableEnd="@drawable/ic_navigate_next_pink_24px"
android:drawableRight="@drawable/ic_navigate_next_pink_24px"
android:padding="8dp"
android:text="@string/helloactivity_button_start"
android:textColor="#07A7CE"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment