Praveen Poswal
Praveen Poswal

Reputation: 11

How Can I refresh the View after I change application locale to RTL language problematically without restarting the activity

In my activity I change locale programatically to RTL language. After that I want my layout to refresh according to RTL locale without restarting the activity.

Here is the code for that :

public void setLocale()

{
    String arr[] = LangCode.split("_");
    Configuration config = new Configuration();

    DisplayMetrics dm = this.getResources().getDisplayMetrics();

    Locale locale = new Locale(arr[0], arr[1]);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Locale.setDefault(locale);
        config.setLocale(locale);
        config.setLayoutDirection(locale);
    } else {
        config.locale = locale;
    }

    this.getResources().updateConfiguration(config, dm);

}

I have tried to invalidate the view after that but with no success.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {

    findViewById(R.id.intro_lang_ll).invalidate();

    findViewById(R.id.intro_lang_ll).requestLayout();

}

I want my view should mirror itself as soon as I change the locale without restarting the activity.

Upvotes: 1

Views: 1646

Answers (2)

Mohamad Shaker
Mohamad Shaker

Reputation: 1486

You can try this

Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);

It works for me .. without delay

Upvotes: 0

Jaimin Modi
Jaimin Modi

Reputation: 1677

If you don't want to refresh your whole Activity then you can use invalidate() as follows :

public void invalidate () 

It invalidate the whole view.

If the view is visible, onDraw(Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().

Upvotes: 1

Related Questions