Visibility

dlfallen

Active Member
Licensed User
Longtime User
I am trying to write an android version of my probability calculator previously shared in the b4ppc forum. This program requires me to manipulate the visibility of certain labels and text fields, and to change the graphic displayed in an imageview.

I have had success in changing the visibility of buttons, radiobuttons, and panels but have had no success in changing the visibility of labels or edittext. Also, no success at loading a bitmap into the imageview.

I must be missing something basic here, but I have been trying for hours now with no progress. For example,
in Sub Globals I have
Dim
df1Lbl AsLabel

in Sub Activity_Create(FirstTime AsBoolean) I have
df1lbl.Initialize("df1lbl")


But when in code, I have
df1Lbl.Visible = False
nothing happens (i.e., the label remains visible).

For the imageview I have

in Sub Globals
Dim DistImg AsImageView

in Sub Activity_Create(FirstTime AsBoolean)
DistImg.Initialize ("DistImg")

in code
DistImg.Bitmap = LoadBitmap(File.DirAssets, "tdist2.jpg")
does not load the bitmap.

I am testing the code in the emulator and receive no error messages. I have attached the zip file for the infant project. Pressing the button Command1 (1st button on left) demonstrates the behaviors mentioned above. Here is the code for Command1:

Sub Command1_Click
'The button (Command2) and RadioButton (RadioButton2) become invisible.
'The label (DF1Lbl) and EditText (DF1Text) do NOT become invisible.
'The imageview (hourglass) does NOT become visible
'the imageview (DistImg) does NOT load the new bitmap
Command2.Visible = False
DF1Lbl.Visible = False
DF1Text.Visible = False
RadioButton2.Visible = False
hourglass.Visible = True
DistImg.Bitmap = LoadBitmap(File.DirAssets, "tdist2.jpg")
End Sub

Any help would be appreciated.
 

Attachments

  • ProbCalc.zip
    23.5 KB · Views: 201

klaus

Expert
Licensed User
Longtime User
You must remove all the Initialze lines in Activity_Create:
B4X:
[FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]Activity_Create(FirstTime [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008b8b][FONT=Courier New][SIZE=2][COLOR=#008b8b][FONT=Courier New][SIZE=2][COLOR=#008b8b]Boolean[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[/SIZE][/FONT]
[SIZE=2][FONT=Courier New]Activity.LoadLayout([/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"ProbCalc"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]) [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]'Load the layout file.[/COLOR][/SIZE][/FONT]
[SIZE=2][FONT=Courier New][COLOR=#008000]' DistImg.Initialize ("DistImg")[/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New][COLOR=#008000]' Hourglass.Initialize ("Hourglass")[/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New][COLOR=#008000]' df1lbl.Initialize("df1lbl")[/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New][COLOR=#008000]' df1text.Initialize("df1text")[/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New][COLOR=#008000]' df2lbl.Initialize("df2lbl")[/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=Courier New][COLOR=#008000]' df2text.Initialize("df2text")[/COLOR][/FONT][/SIZE]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]End Sub[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
You have already added your views in the Designer and they are already initialized there. Initializing them again set new instances.
You just need to Dim the views in Globals as you did to have a declaration of them and that the IDE recognizes them.
You would need the initilization lines only when you add views by code.

Best regards.
 
Upvote 0

dlfallen

Active Member
Licensed User
Longtime User
Thanks, that solved the problem OK.

All the new terminology in Android is a bit confusing. My approach was to bring code over from b4ppc, make the obvious changes and then run the code. When I get an error message I make a change and run the code again. Eventually, I get a run that is error free. This hit-and-miss approach failed me this time. Early on I was getting an error message which seemed to indicate I needed to initialize everything. When I did, those error messages disappeared but, as you know, the program did not behave the way I thought it should. Obviously, I had made other changes as well because if I comment out the initialization statements in the code I posted, all is well.

It would be a great help to better understand what is going on. I would much rather take a Tesla approach than an Edison one. Your response describing when and when not to use initialization was very helpful. You could have just said I should comment out the initialization statemts and the program would work. But by explaining WHY I gained some understanding into the process, and I will no longer make that mistake in the future, thanks to you.
 
Upvote 0
Top