Android Question Complete noobie asks: how 2 create view w designer then change properties in code

ezderek

Member
Licensed User
Longtime User
I really don't want to create hundreds of toggle buttons in code, with endless onerous details about size and position etc. That's why I bought software with a GUI designer. I like to drag and drop. But I do want to change bg colors depending on state. I've spent hours looking through code examples about "statelistdrawables" but all of it seems to be about objects created with code. Whenever I try to apply it to views created by the designer I get nothing but error messages. Could anyone please give me some "basic" guidance (no pun intended). Perhaps a few lines of code...? I'd be most grateful. Derek.
 

DonManfred

Expert
Licensed User
Longtime User
I have three buttons designed in designer. Part of Layout1
I have set eventname to button on all three buttons so one click-sub is needed.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private Button1 As Button
    Private Button2 As Button
    Private Button3 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    Button_Click             
End Sub
Sub Button_Click
    Dim zufall As Int = Rnd(1, 101)
  Dim col As Int
  col = Colors.ARGB(Rnd(100, 150),Rnd(20, 800),Rnd(80, 160),Rnd(161, 255))
  Button1.Color = col
  Button1.Text = zufall
  Button1.Tag = zufall

 
    Dim zufall As Int = Rnd(1, 101)
  Dim col As Int
  col = Colors.ARGB(Rnd(100, 150),Rnd(20, 800),Rnd(80, 160),Rnd(161, 255))
  Button2.Color = col
  Button2.Text = zufall
  Button2.Tag = zufall

    Dim zufall As Int = Rnd(1, 101)
  Dim col As Int
  col = Colors.ARGB(Rnd(100, 150),Rnd(20, 800),Rnd(80, 160),Rnd(161, 255))
  Button3.Color = col
  Button3.Text = zufall
  Button3.Tag = zufall

End Sub
 

Attachments

  • testbuttons.zip
    7 KB · Views: 174
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Here a second example. Same layout, same buttons, same "only one click sub". But in this one only the clicked button changes the background
B4X:
Sub Button_Click
    Dim btn As Button = Sender
    Dim zufall As Int = Rnd(1, 101)
  Dim col As Int
  col = Colors.ARGB(Rnd(100, 150),Rnd(20, 800),Rnd(80, 160),Rnd(161, 255))
  btn.Color = col
  btn.Text = zufall
  btn.Tag = zufall
  
End Sub
 

Attachments

  • testbuttons_sender.zip
    7.1 KB · Views: 157
Last edited:
Upvote 0

ezderek

Member
Licensed User
Longtime User
Hi Don

Thanks for answering. Regarding your 1st answer: what I think I'm seeing is that any object named "button" will trigger this same procedure if clicked. Is that right? I don't understand... why is there a button_click in the "create_first_time block"? Is that to initialize the buttons? Also -- How is it possible to DIM the same variables 3 times in one procedure? Any other form of Basic would not even compile that. I really find this language most mysterious.
Derek.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Here my PM answer (so others can read my answer too):
----------------------------------------------------------------------------
1. In designer a button have a name. for ex btnGo
this name is then automatically copied into the property eventname

2. button becomes the name btnquit and this is also copied to the eventname

You have now two buttons in designer. one with btngo and one with btnquit in the field eventname.... THIS would result in that you have TWO subs for the buttons. btgo_click and btnquit_click

BUT you can set the EVENTNAME in designer to the same eventname as an other button. so i realized this one sub thing...

the button_click in the create just do call this sub. Nothing more...
--------------------------------------------------------------------------

Additional answer:

You can remove this button_click-call from activity_create. At appstart the buttons then have the colors designed in designer...
With the call of this sub the buttons are changed at appstart. nothing more.

How is it possible to DIM the same variables 3 times in one procedure? Any other form of Basic would not even compile that. I really find this language most mysterious.

I can imagine that. The best person who can answer such a question would be Erel i think

But i will try to explain what happen with a example

Imagine in YOUR basic you are reading a texfile.
Each line of this file you want to put into a list

as pseudocode would look like

B4X:
sub something
  dim question as string
  dim questions as list
  textfile = fopen ("file.txt")
  while not eof do
    question = readline(textfile)
    ' on each interation of lines in textfile we now have content of line x in the variable question
    questions.add(question)
    ' we add this content to the list
  end
  closefile(textfile)

What would you expect to be inside the lists entries if the textfile would contain

Name of Father?
Pet you love best?
Girl you loved first?

Whats inside the list questions after the above pseudocode?

Right; you would expect that the list looks like

B4X:
questions(0)="Name of Father?"
questions(1)="Pet you love best?"
questions(2)="Girl you loved first?"

right?

In a normal "basic" it would like this, yes. In Basic4Android the values are given as reference
Each "questions.add(question)" will put a reference to the variable question into the list. Not its value.
If you then change the value of question then all references to question have the new value

In reality the pseudocode results in a list questions

B4X:
questions(0)="Girl you loved first?"
questions(1)="Girl you loved first?"
questions(2)="Girl you loved first?"

"Girl you loved first?" was the last value i the loop reading the file. As all values are put to this list as reference, not as value, all reference got the value "Girl you loved first?".

In basic 4 android the code must be something like

B4X:
sub something
  dim questions as list
  textfile = fopen ("file.txt")
  while not eof do
    dim question as string ' A instance of variable question is generated
    question = readline(textfile)
    ' on each interation of lines in textfile we now have content of line x in the variable question
    questions.add(question) ' the given question is the newly generated instance of this variable
    ' we add this content to the list
  end
  closefile(textfile)

so every linecontents is now added to the list. question is in each interation a NEW INSTANCE of the string question.
The first dimmed are not usable at the end of the routine, yes. It got new instances of itself... You can only use the last dimmed question, that´s right. But in the list questions all references are stored. And if you iterated trough this list now the references will find the right value.

B4X:
questions(0)="Name of Father?"
questions(1)="Pet you love best?"
questions(2)="Girl you loved first?"

I´m not sure if i explained it right. I code b4a since 10 month or something. I also have to learn a lot
Maybe guru Erel gives his 5 cent to this He is the best who could describe it ;-)

Anyway i hope my simple example helps a little bit to understand
 
Upvote 0

ezderek

Member
Licensed User
Longtime User
Hello Manfred

Thank you I understood your explanation perfectly. I realize English is not your first language so I appreciate your effort even more. I will probably have more questions as time goes by. I probably should be less proud and stop wasting hours scratching my head and just ask for help instead.
I do think it would be great if there was a more thorough, systematic, graduated instruction manual on b4a. There are a lot of separate tutorials and a lot of different resources, but most of them (not all) seem to be written at a level that is meaningful to more experienced users. Many times late at night I have walked away from my screen, cursing in disgust, saying "I'll have to find a different development package! This one is crazy!" But the reason I keep coming back is - I know Basic so well. It seems like it should be easy. Still as you say, there are many tricky ways in which it is not like Basic at all. Ah well. Once past this stage I hope it will become fun.

Thank you again
Derek.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Thank you I understood your explanation perfectly. I realize English is not your first language so I appreciate your effort even more.

:like

I will probably have more questions as time goes by. I probably should be less proud and stop wasting hours scratching my head and just ask for help instead.

Just ask when something is not clear or something not soing what expected. Put a code example, a good description what you have expected and whats cour code is. Put error-logs or screenshot into this thread....

I´m SURE there is someone who will answer here in forum! Here are so much helping members... That´s why I am doing the same; trying to give help to others if i can and have the time... Looking at my "likes" i think i´m on the right way


If you manage this stage i´m sure you will having much fun with B4A! Really! I did not found any easier developmentplatform for building androidapps. The ONLY IDE too

And having the Rapid debugger it could be fun doing bugfixing

 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…