Android Question StateManager does not have support for SwitchView ?

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Has anyone tried to use the SwitchView and StateManager together?

Seems like it may need support for SwitchViews.

I placed views such as toggle buttons, seek bars, edit boxes and several switch views. All of the views except the switch views retained their values after exiting the app with Activity.Finish and starting the app up again.
 

sorex

Expert
Licensed User
Longtime User
if you can read out the state of that switchview you can always save it like any other variable.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
if you can read out the state of that switchview you can always save it like any other variable.
Hi Sorex,

I was planning on using the KeyValues library but wanted to avoid some extra coding since I got spoiled using the StateManager since it makes the views retain their values prior to exiting an app.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
SwitchView wasnt a standard view at the time StateManager was written.
It is a code module so it is very easy to add support (similar to checkbox) just check the type of object.
Hi,

I tried adding the code that you see commented out but it gave errors when I tried to compile the app. There error stated "Incompatible types. View can not be converted to SwitchView".

B4X:
Sub innerSaveState(v As View, list1 As List)
    Dim data() As Object
    If v Is EditText Then
        Dim edit As EditText
        edit = v
        data = Array As Object(edit.Text, edit.SelectionStart)
    Else If v Is Spinner Then
        Dim spinner1 As Spinner
        spinner1 = v
        data = Array As Object(spinner1.SelectedIndex)
    Else If v Is CheckBox Then
        Dim check As CheckBox
        check = v
        data = Array As Object(check.Checked)
'    Else If v Is SwitchView Then
'        Dim switch As SwitchView
'        switch = v
'        data = Array As Object(switch.Checked)
    Else If v Is RadioButton Then
        Dim radio As RadioButton
        radio = v
        data = Array As Object(radio.Checked)  
    Else If v Is ToggleButton Then
        Dim toggle As ToggleButton
        toggle = v
        data = Array As Object(toggle.Checked)
    Else If v Is SeekBar Then
        Dim seek As SeekBar
        seek = v
        data = Array As Object(seek.Value)
    Else If v Is TabHost Then
        Dim th As TabHost
        th = v
        data = Array As Object(th.CurrentTab)
        For i = 0 To th.TabCount - 1
            th.CurrentTab = i
        Next
        list1.Add(data)
        Dim data() As Object
        Dim r As Reflector
        r.Target = th
        Dim tabParentPanel As Panel
        tabParentPanel = r.RunMethod("getTabContentView")
        For i = 0 To tabParentPanel.NumberOfViews - 1
            innerSaveState(tabParentPanel.GetView(i), list1)
        Next
    Else If v Is ScrollView Then
        Dim sv As ScrollView
        sv = v
        data = Array As Object(sv.ScrollPosition)
        list1.Add(data)
        Dim data() As Object
        innerSaveState(sv.Panel, list1)
    Else If v Is Panel Then
        Dim panel1 As Panel
        panel1 = v
        For i = 0 To panel1.NumberOfViews - 1
            innerSaveState(panel1.GetView(i), list1)
        Next
    End If
    If data.Length > 0 Then list1.Add(data)
End Sub
Sub innerRestoreState(v As View, list1 As List)
    Dim data() As Object
    If v Is EditText Then
        Dim edit As EditText
        edit = v
        data = getNextItem(list1)
        edit.Text = data(0)
        edit.SelectionStart = data(1)
    Else If v Is Spinner Then
        Dim spinner1 As Spinner
        spinner1 = v
        data = getNextItem(list1)
        spinner1.SelectedIndex = data(0)
    Else If v Is CheckBox Then
        Dim check As CheckBox
        check = v
        data = getNextItem(list1)
        check.Checked = data(0)
'    Else If v Is SwitchView Then
'        Dim switch As SwitchView
'        switch = v
'        data = getNextItem(list1)
'        switch.Checked = data(0)
    Else If v Is RadioButton Then
        Dim radio As RadioButton
        radio = v
        data = getNextItem(list1)
        radio.Checked = data(0)
    Else If v Is ToggleButton Then
        Dim toggle As ToggleButton
        toggle = v
        data = getNextItem(list1)
        toggle.Checked = data(0)
    Else If v Is SeekBar Then
        Dim seek As SeekBar
        seek = v
        data = getNextItem(list1)
        seek.Value = data(0)
    Else If v Is TabHost Then
        Dim th As TabHost
        th = v
        data = getNextItem(list1)
        For i = 0 To th.TabCount - 1
            th.CurrentTab = i
        Next
        th.CurrentTab = data(0)
        Dim r As Reflector
        r.Target = th
        Dim tabParentPanel As Panel
        tabParentPanel = r.RunMethod("getTabContentView")
        For i = 0 To tabParentPanel.NumberOfViews - 1
            innerRestoreState(tabParentPanel.GetView(i), list1)
        Next
    Else If v Is ScrollView Then
        Dim sv As ScrollView
        sv = v
        data = getNextItem(list1)
        sv.ScrollPosition = data(0)
        DoEvents
        sv.ScrollPosition = data(0)
        innerRestoreState(sv.Panel, list1)
    Else If v Is Panel Then
        Dim panel1 As Panel
        panel1 = v
        For i = 0 To panel1.NumberOfViews - 1
            innerRestoreState(panel1.GetView(i), list1)
        Next
    End If
End Sub

I uncommented parts of the coding and found that the error comes up with the Else If statement.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
SwitchView class is not a subclass of Android native view so you cannot test it this way.
Try this check instead:
B4X:
Else If GetType(V) = "android.widget.Switch" Then

However it is still not so simple as v doesn't point to a SwitchView class. So you cannot cast it to SwitchView.
The simplest solution is to use code similar to the code in getChecked / setChecked.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
SwitchView class is not a subclass of Android native view so you cannot test it this way.
Try this check instead:
B4X:
Else If GetType(V) = "android.widget.Switch" Then

However it is still not so simple as v doesn't point to a SwitchView class. So you cannot cast it to SwitchView.
The simplest solution is to use code similar to the code in getChecked / setChecked.


Hi Erel,

I get the same error on this line:

B4X:
switch = v

Would you have time to add some coding for the SwitchView ?
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
v is not a SwitchView. You cannot cast it this way.

You need to write:
B4X:
Dim jo As JavaObject = v
jo.RunMethod("setChecked", Array(true))


My level of B4A is not too advanced.

Can you tell me how to alter this portion of coding to include the new coding you have?

B4X:
    Else If GetType(v) = "android.widget.Switch" Then
        Dim switch As SwitchView
        switch = v
        data = getNextItem(list1)
        switch.Checked = data(0)

Thanks.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
B4X:
Else If GetType(v) = "android.widget.Switch" Then
        data = getNextItem(list1)
      Dim jo As JavaObject = v
jo.RunMethod("setChecked", Array(data(0)))

You will need to use similar code (with getChecked) in the code that saves the state.
Hi Erel,

For the code that saves the state, can you tell me what to enter for data = ....

B4X:
Sub innerSaveState(v As View, list1 As List)
    Dim data() As Object
    If v Is EditText Then
        Dim edit As EditText
        edit = v
        data = Array As Object(edit.Text, edit.SelectionStart)
    Else If v Is Spinner Then
        Dim spinner1 As Spinner
        spinner1 = v
        data = Array As Object(spinner1.SelectedIndex)
    Else If v Is CheckBox Then
        Dim check As CheckBox
        check = v
        data = Array As Object(check.Checked)
    Else If GetType(v) = "android.widget.Switch" Then
          Dim jo As JavaObject = v
        jo.RunMethod("getChecked", Array(data(0)))
        data =

Thanks for all the help you are giving me. :)
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

For the code that saves the state, can you tell me what to enter for data = ....

B4X:
Sub innerSaveState(v As View, list1 As List)
    Dim data() As Object
    If v Is EditText Then
        Dim edit As EditText
        edit = v
        data = Array As Object(edit.Text, edit.SelectionStart)
    Else If v Is Spinner Then
        Dim spinner1 As Spinner
        spinner1 = v
        data = Array As Object(spinner1.SelectedIndex)
    Else If v Is CheckBox Then
        Dim check As CheckBox
        check = v
        data = Array As Object(check.Checked)
    Else If GetType(v) = "android.widget.Switch" Then
          Dim jo As JavaObject = v
        jo.RunMethod("getChecked", Array(data(0)))
        data =

Thanks for all the help you are giving me. :)
Hi Erel,

Almost there.

I tried the coding but it did not seem to save the state of the SwitchView.

Here is the coding I used to call the StateManager:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    LoadMainLayout
    LoadPreviousState
    Utils.CenterViewOnTop(PanelMain, Activity, 50)
End Sub

Sub Activity_Pause (UserClosed As Boolean)

    SaveCurrentState
End Sub

' Additional sub routines.
'-------------------------
Sub LoadMainLayout

    Activity.LoadLayout("main")
End Sub

Sub LoadPreviousState
    If StateManager.RestoreState(Activity, "Main", 0) = False Then
         
        ' Set the default values.
        '------------------------
    End If

'    SwMasterVolume.Checked = StateManager.GetSetting2("Master Volume State", True)
'    SwReloadLastSavedSettings.Checked = StateManager.GetSetting2("Reload Settings", True)
End Sub

Sub SaveCurrentState

    StateManager.SaveState(Activity, "Main")
    StateManager.SaveSettings

'    StateManager.SetSetting("Master Volume State", SwMasterVolume.Checked)
'    StateManager.SetSetting("Reload Settings", SwReloadLastSavedSettings.Checked) 
End Sub

I commented out the coding I was using to set the SwitchView so at least I know that technique works but would like to use just the StateManager and let it handle it.

Here's the StateManager with the new coding:

B4X:
Sub innerRestoreState(v As View, list1 As List)
    Dim data() As Object
    If v Is EditText Then
        Dim edit As EditText
        edit = v
        data = getNextItem(list1)
        edit.Text = data(0)
        edit.SelectionStart = data(1)
    Else If v Is Spinner Then
        Dim spinner1 As Spinner
        spinner1 = v
        data = getNextItem(list1)
        spinner1.SelectedIndex = data(0)
    Else If v Is CheckBox Then
        Dim check As CheckBox
        check = v
        data = getNextItem(list1)
        check.Checked = data(0)
    Else If GetType(v) = "android.widget.Switch" Then
        data = getNextItem(list1)
          Dim jo As JavaObject = v
        jo.RunMethod("setChecked", Array(data(0)))
Sub innerSaveState(v As View, list1 As List)
    Dim data() As Object
    If v Is EditText Then
        Dim edit As EditText
        edit = v
        data = Array As Object(edit.Text, edit.SelectionStart)
    Else If v Is Spinner Then
        Dim spinner1 As Spinner
        spinner1 = v
        data = Array As Object(spinner1.SelectedIndex)
    Else If v Is CheckBox Then
        Dim check As CheckBox
        check = v
        data = Array As Object(check.Checked)
    Else If GetType(v) = "android.widget.Switch" Then
          Dim jo As JavaObject = v
        jo.RunMethod("getChecked", Array(data(0)))
        data = Array(jo.RunMethod("getChecked", Null))
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
It doesn't let me to download. Please upload a small project with only the statemanager and the switch.

Please rename the Switch.bas file to Switch.b4a

I included some other files because Switch.b4a uses them.
 

Attachments

  • StateManager.bas
    7.2 KB · Views: 139
  • SwitchView.bas
    1.9 KB · Views: 158
  • Switch.bas
    4 KB · Views: 130
  • Utils.bas
    2.4 KB · Views: 154
  • KeyValues.bas
    12.8 KB · Views: 137
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Uninstall then reinstall your app.
Does it now work?
Hi warwound,

Just tried that. Still doesn't make it work.

I see from your profile you are in Norfolk. I lived in Egham, Surrey for a few years back around 1988.
 
Upvote 0
Top