How to save ImageView Drawable State?

Owais Ahmed 81 Reputation points
2021-03-21T18:12:09.747+00:00

I want to save my LED_F1 state which is an ImageView,On a certain condition I'm changing the drawable image to another Led color .My Question is How do I save LED_F1 drawable image ? Thanks

   public void LightOn_Drawable_Change()
{
    LED_F1.SetBackgroundResource(Resource.Drawable.LEDGreen_F1);
    //Add Preferences
    var LED_F1_State = Application.Context.GetSharedPreferences("LED_F1 State",FileCreationMode.Private);
    var LED_F1_Editable = LED_F1_State.Edit();
    //LED_F1_Editable.PutInt("owais",LED_F1.Drawable.GetState); //What should I do here ? 
}
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2021-03-22T08:35:47.127+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    Since the returned type of function imageView.Drawable.GetState() is a list, and If you want to use SharedPreferences to save the status of your ImageView, you can try to use JsonConvert to convert it to a string and save this string into SharedPreference.

    You can refer to the following code:

    1.Save value:

            int[] states = imageView.Drawable.GetState();  
            string mstr = JsonConvert.SerializeObject(states);  
    
            ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);  
            ISharedPreferencesEditor editor = prefs.Edit();  
            editor.PutString("states", mstr);  
            editor.Apply();  
    

    2.access value:

          ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);  
           string mString = prefs.GetString("states", "");  
          var mList = JsonConvert.DeserializeObject<List<int>>(mString);  
    
          Toast.MakeText(this, " The first status" + mList[0] ,ToastLength.Long).Show();  
    

    Best Regards,

    Jessie Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Owais Ahmed 81 Reputation points
    2021-05-10T10:07:11.367+00:00

    Sorry for the very late reply ,If this for a drawble ,I have a string number that I want to save ,but its giving me a Newtonsoft.Json.JsonReaderException

         protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
            // Create your application here
    
            // Create your application here
            SetContentView(Resource.Layout.SecondActivity);
            _smsManager_SecondActivity = SmsManager.Default;
    
    
    
            Backbutton = FindViewById<ImageButton>(Resource.Id.backarrow);
            CellNumber = FindViewById<EditText>(Resource.Id.entercellnumber);
            Complaintbutton = FindViewById<ImageButton>(Resource.Id.complaintbutton);
    
            CellNumber.ClearFocus();
            CellNumber.SetCursorVisible(false);
    
    
            CellNumber.KeyPress += CellNumber_KeyPress;
    
             ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
             string mString = prefs.GetString("CellNumber", "");
             var mList = JsonConvert.DeserializeObject<User>(mString);
             Toast.MakeText(this, " The first status" + mList, ToastLength.Long).Show();
             CellNumber.Text = mList.Number; 
        }
    
        public class User
        {
            public string Number { get; set; }
            public string OH_TankSize { get; set; }
            public string UG_TankSize { get; set; }
        }
    
        private void CellNumber_KeyPress(object sender, View.KeyEventArgs e)
        {
            User user = new User() { Number = CellNumber.Text };
    
            e.Handled = false;
            if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter)
            {
                Toast.MakeText(this, CellNumber.Text, ToastLength.Short).Show();
                Preferences.Set("CellNumber", CellNumber.Text);
                CellNumber.SetText(Preferences.Get("CellNumber", "Default_value"), TextView.BufferType.Editable);
    
                string mstr = JsonConvert.SerializeObject(user.Number);
    
                ISharedPreferences pref = Application.Context.GetSharedPreferences("UserInfo", FileCreationMode.Private);
                ISharedPreferencesEditor edit = pref.Edit();
                edit.PutString("CellNumber",mstr);
                edit.Apply();   
            }
    

    Im trying to access the string on the same page , first I'll type the number in a edittext, it saves the data , and then in Oncreate() I try to retrieve it !

    0 comments No comments