How can i save the language setting once i have selected other language? below is my code!!

[Activity(Label = "App4", MainLauncher = true, Icon = "@drawable/icon", ConfigurationChanges = ConfigChanges.Locale)]

public class MainActivity : Activity
{

    string slangcode = "en";
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);


        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button>(Resource.Id.MyButton);
        Button button1 = FindViewById<Button>(Resource.Id.button1);

        button.Click += button_click;
        button1.Click += button1_click;
    }


    private void button_click(object sender, EventArgs e )
    {
        slangcode = "ms";
        Resources.Configuration.Locale = new Locale(slangcode);
        Resources.UpdateConfiguration(Resources.Configuration, Resources.DisplayMetrics);
        Toast toast = Toast.MakeText(this, String.Format("had cahnge to malay"), ToastLength.Short);
        this.Recreate();
        toast.Show();

    }

    private void button1_click(object sender, EventArgs e)
    {
        slangcode = "zh";
        Resources.Configuration.Locale  = new Locale(slangcode);
        Resources.UpdateConfiguration(Resources.Configuration, Resources.DisplayMetrics);
        Toast toast = Toast.MakeText(this, String.Format("had cahnge to chinese"), ToastLength.Short);
        this.Recreate();
        toast.Show();
    }

Answers

  • KPrathapreddyKPrathapreddy USMember
    edited September 2016

    Try this

    string cultureName = "fr-FR";
        var locale = new Java.Util.Locale(cultureName);
        Java.Util.Locale.Default = locale;
        var config = new Android.Content.Res.Configuration { Locale = locale };
        BaseContext.Resources.UpdateConfiguration(config, BaseContext.Resources.DisplayMetrics)
    
  • BenHlzemerBenHlzemer USMember ✭✭

    UpdateConfiguration is dreprecated but there seems to be no good alternative

Sign In or Register to comment.