question

OwaisAhmed-0825 avatar image
0 Votes"
OwaisAhmed-0825 asked ·

How to save ImageView Drawable State?

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 ? 
 }

dotnet-csharpdotnet-androiddotnet-xamarinessentials
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered ·

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.



·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

OwaisAhmed-0825 avatar image
0 Votes"
OwaisAhmed-0825 answered ·

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 !

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.