Way to keep data in an edit box when orientation changes?

mistermentality

Active Member
Licensed User
Longtime User
I have an app that has an edit box and noticed when I move the phone from portrait to landscape mode any text in the box is emptied when the orientation changes (it has two layouts, one 320x480 and one 480x320 for landscape mode).

If I was to keep the edit box contents copied in a string variable is there a way to say to the device "if the phone has changed to landscape mode make the edit box contents equal those in the string variable"?

I know that I can access the phone orientation but I don't want to measure individual changes so is there some way to check for a change in screen width so when one is detected I can make the editbox show the data in the string variable?

Dave
 

mistermentality

Active Member
Licensed User
Longtime User
Anytime you change orientation, the Android system pauses the app, then resumes, yes.



Tom

I just tried this code
B4X:
Sub Activity_Resume
EditText1_message.Text = editstring
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   editstring = EditText1_message.Text
End Sub

but it had no effect. Is there another way around this so that I can not lose the text box contents?

Dave
 
Upvote 0

ssg

Well-Known Member
Licensed User
Longtime User
Hi,

You will need to declare the variable editstring in the process_global part. The variables in the global section are not reset in such a scenario.

Hope that helps.

Cheers!
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hello and I add my question here because it covers most of my iny tiny problem

Added the string "UserText" as a Process_Global
Added to Activity_Pause: UserText = EditText1.Text
and Added to Activity_Resume: EditText1.Text = UserText

and with this last line the original text is not loaded into the EditText1 at all.

As always: thank you in advance.

Helen.
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Yeehhhhhh Erel, you frightened me :D -> faster than the speed of light

This is all (for Louis and I learn from it)


B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
   Dim UserText As String
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)

   Activity.LoadLayout("MainActivity")
'   EditText1.Initialize("t1")
   EditText1.SingleLine = False
'   EditText1.Wrap = True

   If GetDeviceLayoutValues.Height < GetDeviceLayoutValues.Width Then
      EditText1.Width = 465dip
      EditText1.Height = 265dip
   Else
      EditText1.Width = 310dip
      EditText1.Height = 425dip
   End If

   EditText1.Text = "Hi Helen, You are awesome! How would you like my skeleton file sent, just attach here, Dropbox, or email? If email or Dropbox please private message me. If here be aware that others will be able to use your work. Basically I will send you a project that has the forms layed out with labels. All you have to do is go in there and add any kind of color, size them so the labels can be read by anyone, and make sure the events fire. If you wish you can add pictures but please make sure all controls have textual labels so screen readers can recognize them. So let me know how to get them to you and I will. I will need help doing this for all subsequent projects as well and not just this one, so it is only fair that you and anyone else who does this for me, get some kind of payment for your time. If I do attach here and there are multiple submissions from people, I will let everyone know which ones I have chosen."
   EditText1.SelectAll
   EditText1.RequestFocus

End Sub

Sub Activity_Resume

   EditText1.Text = UserText
   EditText1.RequestFocus
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)

   UserText = EditText1.Text

End Sub

Greetz
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Hi Helen,

The problem is that when you run the program UserText is empty.
Then you put "Hi Helen ..." in EditTex1.Text in Activity_Create.
But, as Activity_Resume is called after Activity_Create, you put UserText, which is empty, to EditTex1.Text, so EditTex1.Text will be empty too.
You should replace
B4X:
EditText1.Text = "Hi Helen ..."
EditText1.SelectAll
EditText1.RequestFocus
by
B4X:
If FirstTime = True then
UserText = "Hi Helen..."
End If
Add
EditText1.SelectAll in Activity_Resume.

This should work.

Best regards.
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hi Klaus,

What would I do without you?
Ok, with your explanation I finally know that Pause and Resume are called on opening as well; :sign0144:, ha, ha.
Thank you!

Helen.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Hi Helen,
The flow is the following:
First time:
1) Process_Globals
2) Globals
3) Activity_Create
4) Activity_Resume

Orientation change:
5) Activity_Pause

change of orientation
6) Activity_Create
7) Activity_Resume

A good way to see what happens, is to look in Log Tab.

Best regards.
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Yep Erel,

That explains why I did not get an error in the Pause block and

Yep Klaus,

I know I should use the Log (she says a bit ashamed)
Until now I always try to isolate "my problems" and most of the time that's working fine for me.
Bit by bite I get the "flow" and please don't shoot me if I missed something that has been described before, like the flow.

Thank you gentlemen and have a nice evening.

Helen
 
Last edited:
Upvote 0
Top