Search code examples
androidandroid-actionbarlocale

Change text after changing locale


Okay guys. I'm doing a project and I have put a button in the action bar which translates from Bulgarian to English and vice verse. I have make the change in locale but my problem is that after I click the button the language across the app changes but the text on the button in the action bar does not. I have made two folders values-bg and values-en and the string I'm taking for the button is declared in both string.xmls but the text does not change at all.

This is the code :

import java.util.Locale;

import com.pushbots.push.Pushbots;

import android.net.Uri;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    Button lBtn, sBtn, cBtn, aBtn;
    TextView hLink;

    private String loc = "loc";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActionBar bar = getActionBar();
        bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#118cec")));

        setContentView(R.layout.activity_main);
        this.lBtn = (Button) findViewById(R.id.button1);
        lBtn.setOnClickListener(this);
        this.sBtn = (Button) findViewById(R.id.button2);
        sBtn.setOnClickListener(this);
        this.cBtn = (Button) findViewById(R.id.button3);
        cBtn.setOnClickListener(this);
        this.aBtn = (Button) findViewById(R.id.button4);
        aBtn.setOnClickListener(this);
      this.hLink = (TextView) findViewById(R.id.hlink);
       hLink.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {

        if (v.getId() == lBtn.getId()) {

            startActivity(new Intent(this, lect_list.class));
            Log.d(loc, "pressed");

        } else if (v.getId() == sBtn.getId()) {
            Toast t = Toast.makeText(this, "Text",
                    Toast.LENGTH_LONG);
            t.show();
        } else if (v.getId() == cBtn.getId()) {

            Toast t = Toast.makeText(this,
                    "Text",
                    Toast.LENGTH_LONG);
            t.show();
        } else if (v.getId() == aBtn.getId()) {

            startActivity(new Intent(this, dlect.class));
            Log.d(loc, "pressed");
        }
        else if (v.getId() == hLink.getId()) {

            Toast t = Toast.makeText(this, "Text",
                    Toast.LENGTH_LONG);
            t.show();


            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
        }

    }

    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        if (item.getItemId() == R.id.trans) {

            Toast t = Toast.makeText(this, "Translation", Toast.LENGTH_LONG);
            t.show();

            Locale mLocale = new Locale("bg");
            Locale.setDefault(mLocale);
            Configuration config = getBaseContext().getResources()
                    .getConfiguration();
            if (!config.locale.equals(mLocale)) {
                config.locale = mLocale;
                getBaseContext().getResources().updateConfiguration(config,
                        null);
                setContentView(R.layout.activity_main);
                this.lBtn = (Button) findViewById(R.id.button1);
                lBtn.setOnClickListener(this);
                this.sBtn = (Button) findViewById(R.id.button2);
                sBtn.setOnClickListener(this);
                this.cBtn = (Button) findViewById(R.id.button3);
                cBtn.setOnClickListener(this);
                this.aBtn = (Button) findViewById(R.id.button4);
                aBtn.setOnClickListener(this);

            } else {
                mLocale = new Locale("en");
                config.locale = mLocale;
                getBaseContext().getResources().updateConfiguration(config,
                        null);
                setContentView(R.layout.activity_main);


                this.lBtn = (Button) findViewById(R.id.button1);
                lBtn.setOnClickListener(this);
                this.sBtn = (Button) findViewById(R.id.button2);
                sBtn.setOnClickListener(this);
                this.cBtn = (Button) findViewById(R.id.button3);
                cBtn.setOnClickListener(this);
                this.aBtn = (Button) findViewById(R.id.button4);
                aBtn.setOnClickListener(this);

            }
        }

        return super.onOptionsItemSelected(item);
    }


}

Is it possible somehow to inflate the menu one more time. After i change the locale i set the new activity but not the new menu. Can i call getMenuInflater().inflate(R.menu.main, menu); somehow in my If block ?


Solution

  • Call invalidateOptionsMenu() after you change locale… or simply restart your app :)