Layout Issues?

yonson

Active Member
Licensed User
Longtime User
Hi I'm having a few problems with the layout issues and the designer, wondering if someone could clarify a few things for me:-

  • are only 'absolute' layouts supported? what about relative, etc etc?
  • in the designer it seems you can only specify button placement etc in pixels, not in relative values (e.g. 100%x etc) - why is this? I was thinking perhaps it is because non-absolute values can only be specified in the code. This is ok but its unelegant way to develop the code
  • is there no autcompletetextview? I'm surprised as this is such a core part of many android apps, if not are there plans for such a view soon?

thanks!
 

yonson

Active Member
Licensed User
Longtime User
thanks for the response much appreciated.

For the time being I'm using webviews which is ok but just wanted to make sure I couldn't be doing layouts in a more elegant way.

very much looking forward to the autocompletetextview, any idea when it will be available?

Thanks
John
 
Upvote 0

ietv

Member
Licensed User
Longtime User
So if the layout is defined in dip and automatically scales to device scale....

Does that mean that you should not have 2 layout variants with the same pixel size, but different scales?

For example: if you add a layout variant 320*480 scale 1.5, what happens?

What happens if you add a layout variant, but when you run the emulator, you set it to scale display to screen size?

I ask because I'm having some issues with a mod of your Tick Tack Toe example. I changed the buttons to imageviews, and sometimes it detects a click on the wrong imageview. I was wondering if it could be due to scaling and layouts.
 
Upvote 0

ietv

Member
Licensed User
Longtime User
Here's an example of where it's not registering the correct click:

B4X:
   For x = 0 To 2
      For y = 0 To 2
         Dim b As ToggleButton
         Dim xpick, opick As BitmapDrawable
         Dim xImage, oImage As Bitmap
         xImage = LoadBitmap(File.DirAssets, "x180.png")
         oImage = LoadBitmap(File.DirAssets, "o180.png")
         b.Initialize("button") 'All buttons share the same event sub
         xpick.Initialize(xImage)
         opick.Initialize(oImage)
         b.TextSize = 30
         Activity.AddView(b,offsetX + x * (width + 10dip), offsetY + y * (width + 10dip), width, width)
         Buttons(x, y) = b 'store a reference to this view
      Next
   Next

This code is from the Tick Tack Toe example. I was experimenting to see if I could change the buttons to ToggleButtons with bitmap backgrounds.

This compiles and runs, and shows no errors, but the first time I ran it it registerd a click with the 6th button when I actually clicked on the first button.

(and yes, I know this won't actually place a bitmap background yet, I was adding things in bit by bit to try to see where the error was).
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
The problem with the click registering the wrong button is almost certainly in the button_Click sub, post that up for people to see.
 
Upvote 0

ietv

Member
Licensed User
Longtime User
The problem with the click registering the wrong button is almost certainly in the button_Click sub, post that up for people to see.

It's exactly the same as the Tick Tack Toe example - I didn't touch that sub:

B4X:
Sub Button_Click
   'Using Sender we find the button that raised this event
   Dim b As Button
   b = Sender
   If b.Text <> "" Then Return 'Already used button
   b.Text = CurrentPlayer
   If CheckIfWin(CurrentPlayer) Then
      'we have a winner
      Msgbox("The winner is: " & CurrentPlayer, "")
      NewGame
      Return
   End If
   'Switch players
   If CurrentPlayer = "X" Then CurrentPlayer = "O" Else CurrentPlayer = "X"
   UpdateCurrentPlayer
   'Check if there are any empty cells
   'We could have instead count 9 clicks. But this check is fast enough.
   Dim freeCellLeft As Boolean
   freeCellLeft = False
   For x = 0 To 2
      For y = 0 To 2
         freeCellLeft = freeCellLeft OR Buttons(x, y).Text = ""
      Next
   Next
   If freeCellLeft = False Then
      Msgbox("Both lost!", "")
      NewGame
   End If
End Sub
 
Upvote 0

ietv

Member
Licensed User
Longtime User
How do you detect what Button has raised the click event?
You should set buttons Tag properties for that.

Best regards.

Erel used Sender in his example. I didn't change that part at all, only the posted code was modified in any way.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Do you also change the text of the buttons somewhere ?
In Erel's code, in the Button_Click event routine he checks only if there is any text on the button anf if no adds text on the button. He doesn't check what exact button has raised the event.
If you need to know what button raised the event, I repeat, you need to use the Tag property.

In the Activity_Create routine add
b.Tag = y * 3 + x
and in the Button_Click event with
b.Tag you get the number of the button that raised the event.

Best regards.
 
Upvote 0

ietv

Member
Licensed User
Longtime User
So Sender does not give a handle to the object that called the sub?

If Sender does not know the handle to the calling view, how does the sub know which tag is being passed to the sub? Or that the Sender does not have text on it?

I must be misunderstanding something.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sender has the handle of the view, but is internally. You can do whatever you want to do with the View.
But, if you have your 9 buttons and want to call a given routine when button 6 was clicked, you don't know with B4A the number of the button. The only way is check its Tag or Text property.

Best regards.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Klaus,

In this case you do not need to know which button was pressed (as in the co-ordinates in the grid)

As I understand it, ietv will get the button pressed (using Sender) and then set the image based on which player pressed it.

Since the buttons are stored in an array, it is easy to check the status of the grid of buttons after the click event has been dealt with - this is done with
B4X:
If CheckIfWin(CurrentPlayer)

I cannot see a fault with the code as posted, the OP needs to zip the app and post it here for us to see it all.
 
Upvote 0

ietv

Member
Licensed User
Longtime User
This might be a problem with the emulator. I've noticed it only happens on the first click - or I think it only happens on the first click, and not every time. So maybe the emulator is reporting something wrong when it gets the focus from the OS.

I originally thought it was the specific AVD (Nook Color), but it happened with other AVDs also.

The attached zip is the problem app.
 

Attachments

  • modifiedTTTexample.zip
    56 KB · Views: 195
Upvote 0

ietv

Member
Licensed User
Longtime User
Strangely enough, I have a modified version of the problem app, and it hasn't shown the strange behavior. But I also use a different method to set the layout, so maybe it is something I'm doing.
 
Upvote 0

ietv

Member
Licensed User
Longtime User
So now the version that previously did not have the issue has started having the issue. I haven't yet looked to see what I changed, but another issue has popped up. I guess I will post it here, but it may need it's own thread.

Used Klaus' method to track the specific button being clicked. Code is attached.

However, here is what got logged when I tried to log the Sender button:

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Resume **

usedButton [IsInitialized=false, player=��, x=0
, y=0]
usedButton [IsInitialized=false, player=��, x=0
, y=0]
usedButton [IsInitialized=false, player=��, x=0
, y=0]
usedButton [IsInitialized=false, player=��, x=0
, y=0]
usedButton [IsInitialized=false, player=��, x=0
, y=0]
usedButton [IsInitialized=false, player=��, x=0
, y=0]
usedButton [IsInitialized=false, player=��, x=0
, y=0]
usedButton [IsInitialized=false, player=��, x=0
, y=0]
usedButton [IsInitialized=false, player=��, x=0
, y=0]

Why am I not getting the Tag object in this log?
 

Attachments

  • thirdmodifiedTTT.zip
    26.4 KB · Views: 161
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
This code block (from your Pause event) needs adding to your Create event
B4X:
For x = 0 To 2
For y = 0 To 2
   ButtonsTags(x, y) = Buttons(x, y).Tag
Next
Next

What you are forgetting is that the resume event is called after the create event, so as you have not set up the ButtonsTags array, the Tags all get "Zeroed".
 
Upvote 0

ietv

Member
Licensed User
Longtime User
This code block (from your Pause event) needs adding to your Create event
B4X:
For x = 0 To 2
For y = 0 To 2
   ButtonsTags(x, y) = Buttons(x, y).Tag
Next
Next

What you are forgetting is that the resume event is called after the create event, so as you have not set up the ButtonsTags array, the Tags all get "Zeroed".

I'll be durned, I thought I understood the activity lifecycle, but I forgot that the activity might not be released from memory even if the app is closed, and I did not know that Resume was called even after the first Create event.

I don't know why the app is not correctly registering which box is clicked, but I am going to try to fix the resume problem first.

Thank you all!
 
Upvote 0
Top