Skip to main content

Creating a Quiz Android App

This is a tutorial on creating a quiz in Android. After this tutorial you will be able to create your own Android App with your own quiz.
Follow the instructions given below to learn how to create a quiz app in Android.

1. Download Sketchware App for Android devices.

2. Open Sketchware and start a new project. Enter the app name and other details as per your wishes and click save.

3. Now in the View area (main.xml), insert a Scroll(V) and inside that a Linear(V). Set the width of both the Layouts to match parent and set the padding of Scroll(V) to 0. Also set the background color of Linear(V) to green or a color of your choice, and gravity of Linear(V) to center_horizontal, center_vertical.
4. Now insert 4 button widgets (depends on your requirement), one each for each of the quiz. Each quiz can have several questions. Rename the buttons as Quiz1, Quiz2, Quiz3 and Quiz4 respectively. Now for each of the buttons: increase font size to 18, set margin of each to 8, set text style to bold, and set background color to white.
5. Click on main.xml and add four more views. Name them as quiz1, quiz2, quiz3, and quiz4 respectively​.
6. Move to Logic area. Add a new Intent component and name it option (or anything else you want).
7. Next, in Button:button1 onClick event use following component blocks to set screen to quiz1:
Intent option setScreen Quiz1Activity
StartActivity option
Use same logic for other buttons to set screen to Quiz2, Quiz3, and Quiz4.

8. Now move to View area and select quiz1.xml. Inset a Linear(V) (linear1). Set it's width to match_parent and set it's background color to green.

Inside this insert a Linear(H) (linear2), a Linear(V) (linear3; set layout_height of this layout to wrap_content), and another Linear(V) (linear4). Set width of all these linears to match_parent.

9. Increase weight of linear2, linear3 and linear4 to 1, 2, and 2 respectively. Also for the three linears, set margin to 8, and set background color to white.
10. Insert a Textview (textview1) in linear2. Make it's width match_parent, text_size 18, text_style bold, and lines 3.

In linear3, insert four Linear(H) and inside each of these four linears insert a checkbox and a Textview. Set text of each checkbox to empty. For Textview change text_size to 18 and text_style to bold. Also insert a button (button1​) in linear3 and change it's layout_gravity to right, and text to 'Next' or '>>'.

In linear4 Insert three linears and a textview in each linear. In second and third textview write correct answers to all the questions. Insert a button (button2) below that, change it's layout_gravity to center_horizontal and name it as Menu.
11. Now move to Logic area of quiz1 (Quiz1Activity.java).

Create a new Intent component and name it intent.

Go to onCreate event. Add three new blocks and name them as StartQuestions, and Answers. Also add three new number variables namely quesans, and score. Add Start block in onCreate event.


12. Add five new events, one each for the four checkbox onClick, and one for textview1 onClick.

13. In Start defineFunction use following blocks:
Set ques to 1
Set score to 0
textview1 setEnable true
textview1 setText Click here to start Quiz
linear2 setVisible VISIBLE
linear3 setVisible GONE
linear4 setVisible GONE

14. In Questions defineFunction use if then blocks and write all the questions and options for each of the questions, as shown in the image below.
If ques = 1 then
textview1 setText Your first question?
textview2 setText option 1
textview3 setText option 2
textview4 setText option 3
textview5 setText option 4

Add all the questions and their options one by one for each value of ques. I added four questions. After adding all questions use the block as shown below.
If ques = 5 then
linear2 setVisible GONE
linear3 setVisible GONE
linear4 setVisible VISIBLE
textview6 setText join Your Score is: and toString score without decimal


15. In Answers defineFunction​ set variable ans to correct option for each question.
If ques = 1 then
set ans to 1
If ques = 2 then
set ans to 3
If ques = 3 then
set ans to 1
If ques = 4 then
set ans to 4

16. In checkbox1​ onClick, setChecked false all other checkbox. Do this for each of the checkbox onClick.
17. In textview1 onClick, use following blocks:
textview1 setEnable false
linear3 setVisible VISIBLE
Questions
Answers
18. In button1 (which is our Next button) onClick use following blocks:
If checkbox1 ​getChecked then
If ans = 1 then
score increase 1
ques increase 1
checkbox1 setChecked false
Questions
Answers

Use these blocks for each of the checkbox.
19. On button​2 onClick, use setScreen block to move to MainActivity.
Intent intent setScreen MainActivity
StartActivity​ intent
20. Now use same codes to create more quizzes, in this case Quiz2, Quiz3, and Quiz4.

21. Save and run the App. You'll find your first Android quiz app is ready and working.

Here is a video to make it easier for you:

Comments

Popular posts from this blog

Create Music Player app in Sketchware

To create a Music Player app in sketchware, follow the steps given below. 1. Create a new project in Sketchware. 2. In VIEW area on main.xml, add a ListView  listview1  with height wrap_content, and weight 1. Add a SeekBar  seekbar1 , an ImageView  imageview1 , and three TextViews  textview1 ,  textview2 , and  textview3 , as shown in the image below. 3. Create a CustomView  mycustom.xml  and add a TextView  textview1 , and an ImageView  imageview1  in it. For listview1 select mycustom.xml as it's customView. 4. Add a MediaPlayer component  mp , a Shared Preferences component  sp:sp  and a Timer component  timer . 5. Create six More Blocks  MPcreate(pos), MPstart , and  MPpause  for the MediaPlayer and  searchFolders ,  getFileList in [filePath]  and  extra . 6. Add three number variables  n, r  and  songPosition , and five String variables  currentfile ...

Admob rewarded video Ads in Sketchware

To integrate Admob Rewarded Video Ads to a Sketchware project, follow the steps given below. Prerequisites An Android project (Sketchware project) Account in Admob Always place the test ad unit ID before placing your ad unit ID. App ID and ad unit ID can be obtained by registering the app on Admob. But for using test ads no registration is required. Do not click on your own Ads. 1. Create an android project in Sketchware. 2. Add the app to your Admob account. For your app, generate an ad unit ID for Rewarded Video Ads. 3. In Sketchware project, navigate to  Library Manager  and open Admob settings. i. Click on Add manually and add the ad unit ID generated on Admob website, or use test ad unit ID ca-app-pub-3940256099942544/5224354917. ii. If you are not using any banner or interstitial ad units, select the same ad unit ID for both banner and interstitial ads. iii. Add Test Device automatically shown by Sketchware. iv. Save it. v. Switch ON Admob and AppCompat and design. ...

Firebase auth in Sketchware for Login Activity

To create a login activity using Firebase Auth in Sketchware, follow the steps given below. This method uses Firebase authentication service for creating login. 1. In your Firebase account, go to Firebase authentication. 2. In Firebase authentication web set-up, go to SIGN-IN method, and enable Email/password and Anonymous. 3. Go to Project settings in your Firebase project and copy the Web API Key, Project ID, and App ID. 4. Paste the Project ID, App ID, and Web API Key in your project in Sketchware, in the Firebase settings. 5. On the  MainActivity  page add a File Shared preferences component  user:user  and an Intent  i . Also add a Timer  t  and a Firebase Auth  testlogin . 6. Create a new page  login.xml  with Activity called  LoginActivity . 7. In  onCreate  event of MainActivity use blocks as shown in image below. It identifies main page with  File user key page . It also checks if user is logged in to  F...