ActivityMain.xml
----------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.2"
/>
<Button
android:id="@+id/actionEng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="English"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.6"
/>
<Button
android:id="@+id/actionHi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hindi"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.8"/>
</android.support.constraint.ConstraintLayout>
----------------------------------------------------------------------------------------
MainActivity.java
-----------------------------------------------------------------------------------------
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView tvMessage;
Button actionEng, actionHi;
AppPrefs prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = new AppPrefs(this);
tvMessage = (TextView) findViewById(R.id.tvMessage);
actionEng = (Button)findViewById(R.id.actionEng);
actionHi = (Button)findViewById(R.id.actionHi);
actionEng.setOnClickListener(this);
actionHi.setOnClickListener(this);
tvMessage.setText(getResources().getString(R.string.txt_1));
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.actionEng:
{
setEnglish();
}
break;
case R.id.actionHi:
{
setHindi();
}
break;
}
}
private void setEnglish(){
String languageToLoad = "en"; // your language
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
recreate();
}
private void setHindi(){
String languageToLoad = "hi"; // your language
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
recreate();
}
}
No comments:
Post a Comment