2

I want to change the language as the spinner drop down changes .I have the following code but are not able to change the language of even simple "Hello World".I have values defined in values,values-es and values-fr folder for "hello_world" and "hello" string for each language .What i am doing wrong here.Here is my code below for MainActivity.java

package com.example.changelanguage;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.util.*;

public class MainActivity extends Activity {

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

        ArrayList<String> languages = new ArrayList<String>();
        languages.add("English");
        languages.add("French");
        languages.add("Spanish");

        Spinner spinner = (Spinner)findViewById(R.id.spinner1);
        spinner.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, languages));


        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                 Log.v("Value", "Language Value: " + position);
                    if(position == 1){
                         //Change text to Spanish
                        setLocale("es");
                    }
                    else{
                        //Change text to French
                         setLocale("fr");
                    }
                    Toast.makeText(
                            parentView.getContext(), "Selected Lang : " + 
                    parentView.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }

            });


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



    public void setLocale(String localeCode){
        Locale locale = new Locale(localeCode);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    }           

}

Code for the AndroidMainfest.xml is below :-

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.changelanguage"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppBaseTheme" >
        <activity
            android:name="com.example.changelanguage.MainActivity"
            android:label="@string/app_name" 
            android:configChanges="locale">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />                
            </intent-filter>

        </activity>
    </application>

</manifest>

Code for RelativeLayout is :-

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="83dp"
         />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinner1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="66dp"
        android:text="@string/hello" />

</RelativeLayout>
user2718771
  • 125
  • 2
  • 2
  • 8
  • for one, your spanish item is at position 2 – njzk2 Sep 04 '13 at 16:25
  • define 'are not able to change' – njzk2 Sep 04 '13 at 16:26
  • Hi, i change the code now and item positions .But still its behavior is same as item position does't matter.Please elobrate what you mean "define 'are not able to change' – user2718771 Sep 04 '13 at 16:29
  • Check this: http://stackoverflow.com/questions/2264874/changing-locale-within-the-app-itself and http://stackoverflow.com/questions/15303140/android-changing-activity-language?rq=1 and http://stackoverflow.com/questions/7171517/setting-locale-programmatically-not-working – Chintan Soni Sep 04 '13 at 16:34
  • I tried to implement what is said on the "http://stackoverflow.com/questions/7171517/setting-locale-programmatically-not-working".But Code Exception on "Resources standardResources = activity.getResources();" Can't resolved activity ? – user2718771 Sep 04 '13 at 16:50
  • Any Resource or link will be helpful her e. – user2718771 Sep 04 '13 at 20:09
  • Ok i am able to implement it with the link below.But i am not able to refresh the views? i have to restart the application to implement the changes. How can i refresh the changes immediately .Though i tried the below code but it refreshes the whole main Intent and after that i am not able to select the Spinner Item:-private void refresh() { finish(); //setContentView(R.layout.activity_main); Intent myIntent = new Intent(MainActivity.this, MainActivity.class); startActivity(myIntent); } – user2718771 Sep 04 '13 at 21:50

0 Answers0