Android Question how to create a quiz in B4A?

anedlis2

New Member
I need a quiz where the first question would be displayed and the answers typed on the keyboard, then it would display whether the user answered right or wrong and display the second question etc. At the end, the scores are added up and a score is written. i would also like different images to be displayed in each question
 

zed

Active Member
Licensed User
It will be complicated if the user has to type the answer on the keyboard. If the answer is only one word, it can still be fine. But if it is a sentence! How do you check the answer. You have to check the syntax, the spelling,... The simplest is a multiple-choice question.

First, you have to create a database with the questions and the possible answers (a choice of 3 or 5 answers).
Then, create a template with the designer.
Imageview to display your image, a label for the question and 3 or 5 labels for possible answers with radio button.
Then you check if the answer is the same as in your database. If it is TRUE you save Score +1 and you display the second question.
 
Upvote 0

zed

Active Member
Licensed User
Here is an example.

Database:
db_quiz.png


B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private SQL1 As SQL
    Private Toast As BCToast
    
    Private GoodAnswer As Int
    Private myQ, myA1, myA2, myA3 As String
    Private lblQuestion As Label
    Private rb1 As RadioButton
    Private rb2 As RadioButton
    Private rb3 As RadioButton
    
    Private qId As Int = 1 'question id
    Private nbrQ As Int 'number of questions
    Private lblNbrQ As Label
    Private score As Int = 0
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    B4XPages.SetTitle(Me, "Quiz")
    
    ' If the database does not exist, copy the database
    If Not(File.Exists(xui.DefaultFolder, "quiz.db3")) Then
        File.Copy(File.DirAssets, "quiz.db3", xui.DefaultFolder, "quiz.db3")
    End If
    SQL1.Initialize(xui.DefaultFolder, "quiz.db3", True)
    Toast.Initialize(Root)
    
    nbrQ = SQL1.ExecQuerySingleResult("SELECT count(*) FROM table_quiz") 'number of questions in the db
        
    loadQuestion(qId)
    
End Sub

Private Sub loadQuestion(n As Int)
    
    reset_radiobutton
    
    lblNbrQ.Text =  $"Question ${n} of ${nbrQ}"$
    
    Dim RS As ResultSet = SQL1.ExecQuery("SELECT * FROM table_quiz where id_question ="&n&" ")
    
    RS.NextRow
    myQ = RS.GetString("question").Trim
    myA1 = RS.GetString("answer1").Trim
    myA2 = RS.GetString("answer2").Trim
    myA3 = RS.GetString("answer3").Trim
    GoodAnswer = RS.GetInt("tag")
        
    lblQuestion.Text = myQ
    rb1.Text = myA1
    rb2.Text = myA2
    rb3.Text = myA3

End Sub


Private Sub btCheck_Click
'    check
    Dim userSelected As Int
    If rb1.Checked Then userSelected = 1
    If rb2.Checked Then userSelected = 2
    If rb3.Checked Then userSelected = 3

    If userSelected = GoodAnswer Then 'compare the answers
        score = score+1
        Toast.Show($"You win [b]Score : ${score} points[/b]"$)
    Else
        Toast.Show($"You lost : [b]Score : ${score} points[/b]"$)
    End If
    
    qId = qId+1 'question id

    If qId <= nbrQ Then
        loadQuestion(qId) 'next question
    Else
        Sleep(1000)
        Toast.Show($"Completed Quiz. You have a score of [b]${score} points[/b]"$)
    End If
    
    
End Sub

Private Sub reset_radiobutton
    rb1.Checked = False
    rb2.Checked = False
    rb3.Checked = False
End Sub
 

Attachments

  • quiz.zip
    11 KB · Views: 107
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Here is an example
Since the project posted by @zed did not have the database included, I thought I would make one and attach it here so those who do not wish to create it from scratch can use the attached to test this good example by zed. You need to rename it to quiz.db3 and then copy it to the assets folder. It would not let me save it with the db3 extension.
 

Attachments

  • quiz.txt
    12 KB · Views: 88
Upvote 0

zed

Active Member
Licensed User
I forgot to add the db in the FileManager of the IDE.
Here is the full zip
 

Attachments

  • quiz.zip
    11.6 KB · Views: 90
Upvote 0

aeric

Expert
Licensed User
Longtime User
Get some inspiration from this project from @aeric.It’s for B4J but it could be a starting point

I am now sharing the B4X version. Hope it can help someone.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Hi, Can i do it in random questions? But not repeating?
Sure.
There may be some other ways but how I will do it is like this.

Let say the total questions you want to show to your user/student is 10.

Execute a query to select the question ids under a specific topic/category id. Add the results into a list.

Create a map.
Do a loop until the size of the map equals to 10.
Inside the loop, use random functions to get the index between 0 and the size of the list-1.
Get the id = list.Get(index)
Add the id to the map with key equals to the id and value assign to question and choices of answers.

Now load this map to the user by looping the items.
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Can i do it in random questions? But not repeating?
The other approach I take would be:
1. A given button is clicked to dIsplay for example 5 random questions out of 10 from the database table. It can be any number, not just 5
2. Each of the 5 questions is answered and a score is kept.
3. A second button is clicked to display the remaining 5 random questions which are answered one at time and they are not a repeat of the first set of questions. I would use a NOT IN clause in the queqy using string builder to differentiate them from the first set of questions. The questions can be reset any time by clicking any one of the two buttons.
 
Upvote 0

holyraf

Member
Hello guys, Thank you for the responses! I managed to randomize the Questions. But I'm stuck at this:

The choices are consist of radio buttons right? My goal is to change the background color of the Radio button to green if the answer is correct. Then the other radio buttons will change to red. Same aspect if the user click the wrong answer, Only the good answer will change color to green. Thanks guys.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Hello guys, Thank you for the responses! I managed to randomize the Questions. But I'm stuck at this:

The choices are consist of radio buttons right? My goal is to change the background color of the Radio button to green if the answer is correct. Then the other radio buttons will change to red. Same aspect if the user click the wrong answer, Only the good answer will change color to green. Thanks guys.
Please create a new question.
 
  • Like
Reactions: zed
Upvote 0
Top