0

After I translated my app, I made it possible for users to change their locale. Doing so made dialogues I show look too small. Why? How can I fix it?

Code for localizing:

    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
          getBaseContext().getResources().getDisplayMetrics());

Code for showing a dialog:

    final Dialog dialog = new Dialog(mContext);
    dialog.setTitle(context.getResources().getString(R.string.rate) + APP_TITLE + "!");

    LinearLayout ll = new LinearLayout(mContext);
    ll.setOrientation(LinearLayout.VERTICAL);

    TextView tv = new TextView(mContext);
    tv.setText(context.getResources().getString(R.string.if_you_enjoy_using)
            + APP_TITLE
            + context.getResources().getString(
                    R.string.please_take_a_moment_to_rate_it_thanks_for_your_support));
    tv.setWidth(240);
    tv.setPadding(4, 0, 4, 10);

    if (Locale.getDefault().getLanguage().equals("ja"))
    {
        tv.setTextSize(13);
    }

    ll.addView(tv);

    Button b1 = new Button(mContext);
    b1.setText(context.getResources().getString(R.string.rate) + APP_TITLE);
    b1.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            <code>
        }
    });

    ll.addView(b1);

    Button b2 = new Button(mContext);
    b2.setText(R.string.remind_me_later);
    b2.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            <code>
        }
    });

    ll.addView(b2);

    Button b3 = new Button(mContext);
    b3.setText(R.string.no_thanks);
    b3.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            <code>
        }
    });

    ll.addView(b3);

    dialog.setContentView(ll);
    dialog.show();

The dialogues: Before:

enter image description here

After:

enter image description here

After debugging the problem is with this:

getBaseContext().getResources().updateConfiguration(config,
          getBaseContext().getResources().getDisplayMetrics());

If I drop it, the dialogues return to normal, but the app is not localized...

Ran
  • 657
  • 2
  • 13
  • 29
  • Have you tried just removing getDisplayMetrics() from your code? As the localization resources are updated just with getResources. – KBorja Nov 13 '14 at 09:39
  • So what should I do? getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources()); doesn't compile... If I remove the whole getBaseContext the app is not translated correctly. – Ran Nov 13 '14 at 16:45
  • My bad, maybe instead of create a new Configuration object you can try this answer http://stackoverflow.com/questions/2900023/change-language-programatically-in-android where they get the context configuration, change the locale and update it. – KBorja Nov 13 '14 at 20:23
  • One answer did help - the answer of gmauri21. – Ran Nov 13 '14 at 20:54

0 Answers0