Random Basic problems

ygz12345

Member
Licensed User
Longtime User
From Basic4android Beginner's Guide, my second program
B4X:
Sub btnEvent_Click
   Dim Send As Button
   
   Send = Sender
   
   Select Send.Tag
   Case "BS"
      If lblResult.Text.Length >0 Then
         lblResult.Text = lblResult.Text.SubString2(0,lblResult.Text.Length - 1)
      End If
   Case Else
      lblResult.Text = lblResult.Text & Send.Tag
   End Select

   If lblResult.Text.Length = 0 Then
      btn0.Visible = False
   Else
      btn0.Visible = True
   End If
End Sub

Question 1
Send = Sender ?? what does sender means ??
Returns the object that raised the event.
It is only valid while inside the event sub.
From google but not really understand what does it do.

Question 2
Select Send.Tag??
Select is a case structure ?? and Send.Tag , Tag means ??

Question 3
lblResult.Text = lblResult.Text.SubString2(0,lblResult.Text.Length - 1)
totally dont understand this phrase....

Sorry for noob questions...
still studying this language
 

yttrium

Active Member
Licensed User
Longtime User
B4X:
lblResult.Text = lblResult.Text.SubString2(0,lblResult.Text.Length - 1)

This line sets the text of the object lblResult to a string, which it pulls by a substring of lblResult. "SubString2" means it takes characters from a string, with a start and end point. 0 means the start point is the very beginning of the string, and lblResult.Text.Length - 1 means that it takes the end character - 1, so the character just before the end character. Basically, it trims off the end character of the string lblResult.Text.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Question 1
Send = Sender ?? what does sender means ??

Sender, just like the explanation says it returns which object raised the event, for example, say you have 20 buttons, using SENDER you can determine which button was pressed, one perfect example is THIS code.
Question 2
Select Send.Tag??
Select is a case structure ?? and Send.Tag , Tag means ??
In this case you are getting the TAG of the object that raised that event.
Question 3

lblResult.Text = lblResult.Text.SubString2(0,lblResult.Text.Length - 1)
I that line there's some string manipulation.

All that information is in the document you mentioned.
 
Upvote 0

ygz12345

Member
Licensed User
Longtime User
i am trying a simple code to get the value from the seekbar but it seem like im only getting value 50 no matter how i trigger the seekbar.

code are as below

B4X:
Sub Globals
   Dim Label1 As Label
   Dim SeekBar1 As SeekBar
End Sub


B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
Label1.Text=SeekBar1.Value
End Sub

anything i have done wrong ??
 
Upvote 0

ygz12345

Member
Licensed User
Longtime User
That's because the default value is 50 and you are not changing that.

setting for the seekbar:
value set to 0;
maximum value set to 100;

the value show 0 no matter how i trigger the seekbar.

which mean seekbar1.value is incorrect ??
 
Upvote 0

ygz12345

Member
Licensed User
Longtime User
You have to update the value as you move the index, read here about SeekBar, also, have you looked at the event to get the value?
B4X:
ValueChanged(Value As Int, UserChanged As Boolean)

you mean i have to start a new event ?? like below ??

B4X:
Sub ValueChanged(Value As Int, UserChanged As Boolean)
CODE...
End Sub
 
Upvote 0

ygz12345

Member
Licensed User
Longtime User
B4X:
Sub seekbar1_ValueChanged (Value As Int, UserChanged As Boolean)
   If UserChanged Then
   a=Value
   End If
End Sub

So how do i get the value of 'a' to another event ??
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
You can dim your 'a' variable in the 'globals' section. Then you can use its value wherever you choose to. If you need an immediate action, you can do it inside the vaueChanged sub, or even call another routine, passing the value to it.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You can access the seekbar's value from anywhere within the module as it is already a global variable. You can use:

B4X:
SeekBar1.Value

To get or set the value.

You would only really use :

B4X:
Sub seekbar1_ValueChanged (Value As Int, UserChanged As Boolean)

If you want to do something as soon as the value is changed.
 
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
I dont know how to set a file like xml or txt or bin or any type of file in my apk file so that when i want use it
I want to copy this file and i want root acces and please set the code can help me
I dont know where is the root directory

Please help me!:sign0085:

Please make a new thread, and when you have, delete your post. Don't threadjack.
 
Upvote 0

ygz12345

Member
Licensed User
Longtime User
B4X:
Sub SeekBar1_ValueChanged (Value As Int, UserChanged As Boolean)
   
End Sub


Sub button1_down
   Label1.Text=SeekBar1.Value
End Sub

i wan to see the value of seekbar when im pressing the button. which mean when i pressing the button and toggle the seekbar at the same time , the value will keep on changing. Unfortunately, i have try the event of click,up,down,long click , none of them working...
any idea ???
 
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
i am trying a simple code to get the value from the seekbar but it seem like im only getting value 50 no matter how i trigger the seekbar.

code are as below

B4X:
Sub Globals
   Dim Label1 As Label
   Dim SeekBar1 As SeekBar
End Sub


B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
Label1.Text=SeekBar1.Value
End Sub

anything i have done wrong ??

The reason this does not work for you, is Sub Activity_Create is called at the very start of the app, or when the activity is destroyed and recreated, like when you tilt the device on the side. When Activity_Create is called, the value of Seekbar is 50. it hasn't been updated or changed yet, because the app is just starting up.

B4X:
Sub SeekBar1_ValueChanged (Value As Int, UserChanged As Boolean)
   
End Sub


Sub button1_down
   Label1.Text=SeekBar1.Value
End Sub

i wan to see the value of seekbar when im pressing the button. which mean when i pressing the button and toggle the seekbar at the same time , the value will keep on changing. Unfortunately, i have try the event of click,up,down,long click , none of them working...
any idea ???

It's very odd that you want to see the value of a seekbar while you are holding down a button. But... you asked, so I will answer. Look at this code. It is very simple, very straightforward and it works. I leave any dressing up or further processing to you...

B4X:
Sub Process_Globals
End Sub

Sub Globals
   Dim Button1 As Button
   Dim Seekbar1 As SeekBar
   Dim Label1 As Label
   Dim isButtonDown As Boolean = False
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Button1.Initialize("Button1")
   Button1.Text = "Show"
   Seekbar1.Initialize("Seekbar1")
   Label1.Initialize("")
   Label1.Color = Colors.Gray
   Activity.AddView(Button1, 40%x, 30%y, 20%x, 10%y)
   Activity.AddView(Seekbar1, 10%x, 10%y, 80%x, 5%y)
   Activity.AddView(Label1, 40%x, 60%y, 20%x, 10%y)
End Sub

Sub Button1_down
   isButtonDown = True
End Sub

Sub Button1_up
   isButtonDown = False
End Sub

Sub Seekbar1_ValueChanged (Value As Int, UserChanged As Boolean)
   If isButtonDown = True Then
      Label1.Text = Value
   End If
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Here is the explanation of what is happening:

In Sub Globals a button, seekbar and label are defined (sorry, I don't use layouts, I create views manually and this is how it's done). Also, a true/false boolean flag is defined to indicate to the seekbar event if the button is currently pressed down or not.

In Sub Activity_Create, these views are initialized and other properties are assigned, like the button text and label color. Then they are added to the acitivity.

Sub Button1_Down simple sets isButtonDown boolean flag variable to true, indicating it is pressed down. Button1_down is called only when the button is first pressed down, not during the entire time it is held down.

Sub Button1_Up sets the variable isButtonDown to false. It is called when the button is first unclicked or let up on.

Sub Seekbar1_ValueChanged is where the magic is. Note that this event sub is called every time the position value of the seekbar is changed, even it it's only by 1. So, as you scroll the seekbar's button across the screen, it can be called hundreds of times. Each time it is called, it checks to see if the button is being held down by checking the boolean variable isButtonDown. If it is True, then the button is being held down, and it sets the value of the seekbar position to the label text (we don't need to say seekbar1.value because 'value' is passed as a parameter, but you can use seekbar1.value if you wish)

That's all there is to it. The hard part is a basic understanding of how android handles views and their event subs, and how to make the different components all work together for the desired end result. Since you seem very new to Basic4android, I suggest you further that basic understanding of android and how its components work and work together. Keep reading the documentation and beginner tutorials. Trying to run before you can walk leads to much frustration. Coding should be fun and enjoyable :)

Jesse
 
Last edited:
Upvote 0
Top