Android Question Load Layout randomly.

Hi Everybody.

I have a doubt.

Can I randomly load the layouts (files bal) ?

my app is a quiz/game and I want to load randomly the layouts with the questions and answers.


Thanks.
 

strat

Active Member
Licensed User
Longtime User
Using a string array you can load random layout. Create a string array and put all layout names into the array. At the end of each level produce a random number and load the layout in the index of array.
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
One solution might be, for example, to calculate a random width for a label (containing the question, say, and perhaps between 50 and 90% of screen width), then use stringutils to work out the height it needs to be to fit the question text, and then you can calculate a random top position, to ensure it's all visible.

Use a random background image for the label; set a random colour, and perhaps change the background image of the whole screen to one from a selection held in an array, or even select a different font too.

Presumably there are some common elements such as these to each question and answer? If so, that seems much more sensible than creating loads of layouts that you'll have to maintain.
 
Upvote 0
NJDude i know that is not really practical but i just need to use 10 layout.
I know that its possible to use SQL and its maybe the easy way to make that but i just want that using B4A we can use our language we learn, visual BASIC.
Nwhitfield can you explain better your ideia?
My layout has a label with the question then i have 4 buttons with the answers
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
How much do you want them to look different? For instance, in the TypeAttack game, I use a different typeface for each level. I found some typefaces from fontspace.com (remember to check the usage conditions; you may need to license some, others are free). I create an array with the names of the font files that I added to my project

B4X:
Dim fontList(4) As String
fontList = Array As String("kgcountingstars.ttf","typekeys.ttf","got_heroin.ttf","hotmetal.ttf")

Then when a new level starts, it gets a random typeface like this:

B4X:
Dim gameFont As Typeface
gameFont = Typeface.LoadFromAssets(fontList(Rnd(0,4)))

So you could do something like that with the label for your question, then say
B4X:
Dim su As Stringutils
Dim question As Label
question.Initialize("")
question.Text = "What is the capital of Peru?"
question.Typeface = gameFont
question.TextSize = 14 ' or you could make this random, say between 12 and 18

' set a random width, between 60 and 85% of the screen
question.Width = Rnd(60%x,85%x)
question.Top = 5dip

' centre the box
question.Left = (100%x-question.Width)/2

' now adjust the height to fit the font
question.Height = su.MeasureMultilineTextHeight( question, question.Text)

That will give you a box, with a different font each time, centred in the screen, and the right height to fit all the text.

You could do similar things to change the buttons, or change the position more randomly.

Add a selection of bitmaps for different backgrounds, and choose one of those randomly, too. Alter the colours in the same sort of way.

Yes, loading a layout saves you having to initialise things, and so can make design faster. But you'd still be limited to, say, ten layouts. If you create it in code like this, with different type faces, colours, changes in position, and so on, then there could be far more combinations, and you don't have to spend ages creating lots of different layouts.
 
Upvote 0

strat

Active Member
Licensed User
Longtime User
I think @nwhitfield's solution is more practical and professional but if you want to use the method that you declared before, try the project I uploaded.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

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 panelnumbers(11) As Int
    Dim Panel1 As Panel
    Dim Label1 As Label
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")
    For i=1 To 10
        panelnumbers(i)=0
    Next
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub Panel1_Click
    Dim panelname As String
    Dim name As String
    Dim number As String
    name="Layout"
    number=Rnd(1,11)
    'Find the number of panel that haven't loaded
    Do While panelnumbers(number)=1
        number=Rnd(1,11)
    Loop
    panelnumbers(number)=1'Set panel as loaded
    panelname=name&number
    Activity.LoadLayout(panelname)
    Label1.Text=panelname
End Sub


Once a layout loaded, it is set the "loaded before" and doesn't load again.
After all layout loaded all items of array will be "1". So if you keep click to panel, program will be locked but it is no problem. You can count the number and if it reach to 10, you can finish the game.
 

Attachments

  • randomlayout.zip
    183.5 KB · Views: 223
Upvote 0
@nwhitfield i see it and i think it´s a good ideia that i will use it in the future of this app but I will chose the @strat exemple for now.

@strat just one last thing.
can you show me how to count the number to when its all loaded show that I win the game please?

Thanks for all the help.
 
Last edited:
Upvote 0

strat

Active Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

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 panelnumbers(11) As Int
    Dim Panel1 As Panel
    Dim Label1 As Label
    Dim levelno As Int
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")
    For i=1 To 10
        panelnumbers(i)=0
    Next
    levelno=0
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub Panel1_Click
    Dim panelname As String
    Dim name As String
    Dim number As String
    If levelno=10 Then
        Msgbox("Game Over","Congratulations")
        Activity.Finish
    End If
    name="Layout"
    number=Rnd(1,11)
    'Find the number of panel that haven't loaded
    Do While panelnumbers(number)=1
        number=Rnd(1,11)
    Loop
    panelnumbers(number)=1'Set panel as loaded
    levelno=levelno+1
   
    panelname=name&number
    Activity.LoadLayout(panelname)
    Label1.Text=panelname
End Sub


I used a variable named "levelno" to count the level number. At every click it increases by one. When it reaches the value 10, a message "Game Over" is displayed and activity is closed.
 
Upvote 0
Hi all.
I want to say thanks to everybody who help me in my questions and @strat thank you really for your help in this project.

@nwhitfield i save your example and i will use it in the future of this app and thank you too.

I'm really like to use B4A and in the future i will buy the license for the B4A.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…