DEV Community

HMS Community
HMS Community

Posted on

Find the basic Android and HMS questions in Quiz Android app (Kotlin) – Part 2

Image description
Introduction
In this article, we can learn about the basic questions on Android and HMS of Quiz application. The purpose of conducting quizzes within the business is more fun and educative to understand how the business is running straight away, test the knowledge of the user and helps to form a businessman or woman a far better person in terms of the business process. So, I will provide the series of articles on this Quiz App, in upcoming articles.

If you are new to this application, follow my previous article.

https://forums.developer.huawei.com/forumPortal/en/topic/0202877278014350004

Requirements

  1. Any operating system (MacOS, Linux and Windows).
  2. Must have a Huawei phone with HMS 4.0.0.300 or later.
  3. Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 and above installed.
  4. Minimum API Level 24 is required.
  5. Required EMUI 9.0.0 and later version devices.

How to integrate HMS Dependencies

  • First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a Huawei ID.

  • Create a project in android studio, refer Creating an Android Studio Project.

  • Generate a SHA-256 certificate fingerprint.

  • To generate SHA-256 certificate fingerprint. On right-upper corner of android project click Gradle, choose Project Name > Tasks > android, and then click signingReport, as follows.

Image description
Note: Project Name depends on the user created name.

  • Create an App in AppGallery Connect.

  • Download the agconnect-services.json file from App information, copy and paste in android Project under app directory, as follows.
    Image description

  • Enter SHA-256 certificate fingerprint and click Save button, as follows.
    Image description

  • Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer Add Configuration.

maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.6.0.300'

Enter fullscreen mode Exit fullscreen mode
  • Add the below plugin and dependencies in build.gradle(Module) file.
apply plugin: id 'com.huawei.agconnect'
// Huawei AGC
implementation 'com.huawei.agconnect:agconnect-core:1.6.0.300'
Enter fullscreen mode Exit fullscreen mode
  • Now Sync the gradle.

Let us move to development

I have created a project on Android studio with empty activity let us start coding.

In the AndroidActivity.kt we can find the business logic for android questions.

class AndroidActivity : AppCompatActivity(), View.OnClickListener {

    private var mCurrentPosition: Int = 1
    private var mQuestionsList: ArrayList<AndroidQuestions>? = null
    private var mSelectedOptionPosition: Int = 0
    private var mCorrectAnswers: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_android)

        mQuestionsList = AndroidConstants.getQuestions()
        setQuestion()

        txt_option1.setOnClickListener(this)
        txt_option2.setOnClickListener(this)
        txt_option3.setOnClickListener(this)
        txt_option4.setOnClickListener(this)
        btn_ardsubmit.setOnClickListener(this)

    }

    private fun setQuestion() {
        val question = mQuestionsList!![mCurrentPosition -1]
        defaultOptionsView()
        if(mCurrentPosition == mQuestionsList!!.size){
            btn_ardsubmit.text = "FINISH"
        } else {
            btn_ardsubmit.text = "SUBMIT"
        }
        progressBar_ard.progress = mCurrentPosition
        txt_progress_ard.text = "$mCurrentPosition" + "/" + progressBar_ard.max
        txt_ardquestion.text = question!!.questions
        // iv_image.setImageResource(question.image)
        txt_option1.text = question.option1
        txt_option2.text = question.option2
        txt_option3.text = question.option3
        txt_option4.text = question.option4
    }

    private fun defaultOptionsView() {
        val options = ArrayList<TextView>()
        options.add(0, txt_option1)
        options.add(1, txt_option2)
        options.add(2, txt_option3)
        options.add(3, txt_option4)
        for(option in options){
            option.setTextColor(Color.parseColor("#7A8089"))
            option.typeface = Typeface.DEFAULT
            option.background = ContextCompat.getDrawable(this, R.drawable.border_bg)
        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.txt_option1 -> {
                selectedOptionView(txt_option1, 1)
            }
            R.id.txt_option2 -> {
                selectedOptionView(txt_option2, 2)
            }
            R.id.txt_option3 -> {
                selectedOptionView(txt_option3, 3)
            }
            R.id.txt_option4 -> {
                selectedOptionView(txt_option4, 4)
            }
            R.id.btn_ardsubmit -> {
                if(mSelectedOptionPosition == 0){
                    mCurrentPosition++
                    when{
                        mCurrentPosition <= mQuestionsList!!.size -> {
                            setQuestion()
                        } else  -> {
                        val intent = Intent(this, AndroidResultActivity::class.java)
                        intent.putExtra(AndroidConstants.TOTAL_ARD_QUESTIONS, mCorrectAnswers)
                        intent.putExtra(AndroidConstants.CORRECT_ARD_ANSWERS, mQuestionsList!!.size)
                        startActivity(intent)
                    }
                    }
                } else {
                    val question = mQuestionsList?.get(mCurrentPosition -1)
                    if(question!!.correctAnswer != mSelectedOptionPosition){
                        answerView(mSelectedOptionPosition, R.drawable.wrong_bg)
                    } else {
                        mCorrectAnswers ++
                    }
                    answerView(question!!.correctAnswer, R.drawable.correct_bg)
                    if(mCurrentPosition == mQuestionsList!!.size){
                        btn_ardsubmit.text = "FINISH"
                    } else {
                        btn_ardsubmit.text = "Next Question"
                    }
                    mSelectedOptionPosition = 0
                }
            }
        }

    }

    private fun answerView(answer:Int, drawableView:Int){
        when(answer) {
            1 -> {
                txt_option1.background = ContextCompat.getDrawable(this, drawableView)
            }
            2 -> {
                txt_option2.background = ContextCompat.getDrawable(this, drawableView)
            }
            3 -> {
                txt_option3.background = ContextCompat.getDrawable(this, drawableView)
            }
            4 -> {
                txt_option4.background = ContextCompat.getDrawable(this, drawableView)
            }
        }
    }

    private fun selectedOptionView(tv: TextView, selectedOptionNum: Int){
        defaultOptionsView()
        mSelectedOptionPosition = selectedOptionNum
        tv.setTextColor(Color.parseColor("#363A43"))
        tv.setTypeface(tv.typeface, Typeface.BOLD)
        tv.background = ContextCompat.getDrawable(this, R.drawable.select_bg)
    }

}
Enter fullscreen mode Exit fullscreen mode

In the AndroidResultActivity.kt we can find the business logic for android result view.

class AndroidResultActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_android_result)

        val totalQuestions = intent.getIntExtra(AndroidConstants.TOTAL_ARD_QUESTIONS, 0)
        val correctAnswers = intent.getIntExtra(AndroidConstants.CORRECT_ARD_ANSWERS, 0)
       // txt_ardscore.text = "Your Score is $correctAnswers out of $totalQuestions"
        txt_ardscore.text = "Your Score is $totalQuestions out of $correctAnswers"

        btn_ardfinish.setOnClickListener {
            startActivity(Intent(this, Home::class.java))
        }

    }

}
Enter fullscreen mode Exit fullscreen mode

Create a data class AndroidQuestions.kt to access variables.

data class AndroidQuestions(
    val id:Int,
    val questions:String,
    val option1:String,
    val option2:String,
    val option3:String,
    val option4:String,
    val correctAnswer:Int
)
Enter fullscreen mode Exit fullscreen mode

Create object class AndroidConstants.kt to show questions.

object AndroidConstants {

    const val TOTAL_ARD_QUESTIONS: String = "total_questions"
    const val CORRECT_ARD_ANSWERS: String = "correct_answers"

    fun getQuestions():ArrayList<AndroidQuestions>{
        val questionsList = ArrayList<AndroidQuestions>()

        // 1
        val ques1 = AndroidQuestions(1, "On which thread broadcast receivers will work in android?",
           "Worker Thread","Main Thread", "Activity Thread", "None of the above", 2)
        questionsList.add(ques1)

        // 2
        val ques2 = AndroidQuestions(2, "What was the first phone released that ran the Android OS?",
             "Google gPhone","HTC Hero", "Motorola Droid", "T-Mobile G1", 4)
        questionsList.add(ques2)

        // 3
        val ques3 = AndroidQuestions(3, "During an Activity life-cycle, what is the first callback method invoked by the system?",
             "onCreate()", "onStart()", "onStop()", "onDestroy()", 1)
        questionsList.add(ques3)

        // 4
        val ques4 = AndroidQuestions(4, "What is mean by ADB?",
            "Application Debug Bridge", "Android Debug Bridge", "Application data bridge", "Android data bridge", 2)
        questionsList.add(ques4)

        // 5
        val ques5 = AndroidQuestions(5, "If you want to increase the whitespace between widgets, you will need to use the ___ property",
            "Android:digits", "Android:capitalize", "Android:padding", "Android:autoText", 3)
        questionsList.add(ques5)

        // 6
        val ques6 = AndroidQuestions(6, "What is ANR responding time in android?",
            "10 seconds", "5 seconds", "1 minute", "45 seconds", 2)
        questionsList.add(ques6)

        // 7
        val ques7 = AndroidQuestions(7, "Choose the layout, that is deprecated",
            "Absolute Layout", "Frame Layout", "Relative Layout", "Linear Layout", 2)
        questionsList.add(ques7)

        // 8
        val ques8 = AndroidQuestions(8, "Which method can access a view element of a layout resource in activity",
            "onCreate()", "findViewById()", "setContentView()", "None", 2)
        questionsList.add(ques8)

        // 9
        val ques9 = AndroidQuestions(9, "In Which Directory XML Layout files are stored",
            "/assets", "/src", "/res/values", "/res/layout", 4)
        questionsList.add(ques9)

        // 10
        val ques10 = AndroidQuestions(10, "What are the functionalities in AsyncTask in Android?",
            "onPreExecution()", "onPostExecution()", "DoInBackground()", "onProgressUpdate()", 2)
        questionsList.add(ques10)
        return questionsList
    }

}
Enter fullscreen mode Exit fullscreen mode

In the HMSActivity.kt we can find the business logic for android questions.

class HMSActivity : AppCompatActivity(),View.OnClickListener {

    private var kCurrentPosition: Int = 1
    private var kQuestionsList: ArrayList<HMSQuestions>? = null
    private var kSelectedOptionPosition: Int = 0
    private var kCorrectAnswers: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_hmsactivity)

      kQuestionsList = HMSConstants.getQuestions()
        setQuestion()

        txt_option1_hms.setOnClickListener(this)
        txt_option2_hms.setOnClickListener(this)
        txt_option3_hms.setOnClickListener(this)
        txt_option4_hms.setOnClickListener(this)
        btn_hms_submit.setOnClickListener(this)

    }

    private fun setQuestion() {
        val question = kQuestionsList!![kCurrentPosition -1]
        defaultOptionsView()
        if(kCurrentPosition == kQuestionsList!!.size){
            btn_hms_submit.text = "FINISH"
        } else {
            btn_hms_submit.text = "SUBMIT"
        }
        progressBar_hms.progress = kCurrentPosition
        txt_progress_hms.text = "$kCurrentPosition" + "/" + progressBar_hms.max
        txt_hmsquestion.text = question!!.hms_questions
        // iv_image.setImageResource(question.image)
        txt_option1_hms.text = question.hms_option1
        txt_option2_hms.text = question.hms_option2
        txt_option3_hms.text = question.hms_option3
        txt_option4_hms.text = question.hms_option4
    }

    private fun defaultOptionsView() {
        val options = ArrayList<TextView>()
        options.add(0, txt_option1_hms)
        options.add(1, txt_option2_hms)
        options.add(2, txt_option3_hms)
        options.add(3, txt_option4_hms)
        for(option in options){
            option.setTextColor(Color.parseColor("#7A8089"))
            option.typeface = Typeface.DEFAULT
            option.background = ContextCompat.getDrawable(this, R.drawable.border_bg)
        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.txt_option1_hms -> {
                selectedOptionView(txt_option1_hms, 1)
            }
            R.id.txt_option2_hms -> {
                selectedOptionView(txt_option2_hms, 2)
            }
            R.id.txt_option3_hms -> {
                selectedOptionView(txt_option3_hms, 3)
            }
            R.id.txt_option4_hms -> {
                selectedOptionView(txt_option4_hms, 4)
            }
            R.id.btn_hms_submit -> {
                if(kSelectedOptionPosition == 0){
                    kCurrentPosition++
                    when{
                        kCurrentPosition <= kQuestionsList!!.size -> {
                            setQuestion()
                        } else  -> {
                        val intent = Intent(this, HMSResultActivity::class.java)
                        intent.putExtra(HMSConstants.TOTAL_HMS_QUESTIONS, kCorrectAnswers)
                        intent.putExtra(HMSConstants.CORRECT_HMS_ANSWERS, kQuestionsList!!.size)
                        startActivity(intent)
                    }
                    }
                } else {
                    val question = kQuestionsList?.get(kCurrentPosition -1)
                    if(question!!.hms_correctAnswer != kSelectedOptionPosition){
                        answerView(kSelectedOptionPosition, R.drawable.wrong_bg)
                    } else {
                        kCorrectAnswers ++
                    }
                    answerView(question!!.hms_correctAnswer, R.drawable.correct_bg)
                    if(kCurrentPosition == kQuestionsList!!.size){
                        btn_hms_submit.text = "FINISH"
                    } else {
                        btn_hms_submit.text = "Next Question"
                    }
                    kSelectedOptionPosition = 0
                }
            }
        }
    }

    private fun answerView(answer:Int, drawableView:Int){
        when(answer) {
            1 -> {
                txt_option1_hms.background = ContextCompat.getDrawable(this, drawableView)
            }
            2 -> {
                txt_option2_hms.background = ContextCompat.getDrawable(this, drawableView)
            }
            3 -> {
                txt_option3_hms.background = ContextCompat.getDrawable(this, drawableView)
            }
            4 -> {
                txt_option4_hms.background = ContextCompat.getDrawable(this, drawableView)
            }
        }
    }

    private fun selectedOptionView(tv: TextView, selectedOptionNum: Int){
        defaultOptionsView()
        kSelectedOptionPosition = selectedOptionNum
        tv.setTextColor(Color.parseColor("#363A43"))
        tv.setTypeface(tv.typeface, Typeface.BOLD)
        tv.background = ContextCompat.getDrawable(this, R.drawable.select_bg)
    }

}
Enter fullscreen mode Exit fullscreen mode

In the HMSResultActivity.kt we can find the business logic for android result view.

class HMSResultActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_hmsresult)

        val totalQuestions = intent.getIntExtra(HMSConstants.TOTAL_HMS_QUESTIONS, 0)
        val correctAnswers = intent.getIntExtra(HMSConstants.CORRECT_HMS_ANSWERS, 0)
        txt_hmsscore.text = "Your Score is $totalQuestions out of $correctAnswers"

        btn_hmsfinish.setOnClickListener {
            startActivity(Intent(this, Home::class.java))
        }

    }
}
Enter fullscreen mode Exit fullscreen mode

Create a data class HMSQuestions.kt to access variables.

data class HMSQuestions(
    val id:Int,
    val hms_questions:String,
    val hms_option1:String,
    val hms_option2:String,
    val hms_option3:String,
    val hms_option4:String,
    val hms_correctAnswer:Int
)
Enter fullscreen mode Exit fullscreen mode

Create object class HMSConstants.kt to show questions.

object HMSConstants {

    const val TOTAL_HMS_QUESTIONS: String = "total_questions"
    const val CORRECT_HMS_ANSWERS: String = "correct_answers"

    fun getQuestions():ArrayList<HMSQuestions>{
        val hmsquestionsList = ArrayList<HMSQuestions>()

        // 1
        val hques1 = HMSQuestions(1, "What is HMS?",
            "Huawei Medical Services","Huawei Mobile Services", "Huawei Mission Services", "Huawei Military Services", 2)
        hmsquestionsList.add(hques1)

        // 2
        val hques2 = HMSQuestions(2, "Which Kit provides a secure login authorization function of users?",
            "Map Kit","Account Kit", "Location Kit", "Scan Kit", 2)
        hmsquestionsList.add(hques2)

        // 3
        val hques3 = HMSQuestions(3, "Scan Kit supports -- formats for most business scenarios.",
            "12", "14", "15", "13", 4)
        hmsquestionsList.add(hques3)

        // 4
        val hques4 = HMSQuestions(4, "How many landmark names can be recognized by ML Kit?",
            "300", "3000+", "500", "5000+", 4)
        hmsquestionsList.add(hques4)

        // 5
        val hques5 = HMSQuestions(5, "What is the full form of OAID?",
            "Open Advertising Identifier", "Open Adverse Identity", "Open Amendment Information", "Open All Identity", 1)
        hmsquestionsList.add(hques5)

        // 6
        val hques6 = HMSQuestions(6, "How many user addresses are allowed by Huawei Kit for user?",
            "5", "8", "15", "10", 4)
        hmsquestionsList.add(hques6)

        // 7
        val hques7 = HMSQuestions(7, "Which type of format supports Panorama Kit?",
            "JPG", "JPEG", "PNG", "All of the above", 4)
        hmsquestionsList.add(hques7)

        // 8
        val hques8 = HMSQuestions(8, "Which method is used to obtain the last requested available location.",
            "getLastLocation()", "onLocationAvailability()", "onLocationResult", "None", 1)
        hmsquestionsList.add(hques8)

        // 9
        val hques9 = HMSQuestions(9, "How many languages does the Video Editor Kit supports?",
            "100", "48", "55", "78", 4)
        hmsquestionsList.add(hques9)

        // 10
        val hques10 = HMSQuestions(10, "What are the formats supported by Audio Editor Kit?",
            "MP3", "WAV and AAC", "M4A", "All of the above", 4)
        hmsquestionsList.add(hques10)
        return hmsquestionsList
    }

}
Enter fullscreen mode Exit fullscreen mode

In the activity_android.xml we can create the UI screen for questions.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context=".android.AndroidActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp"
        android:gravity="center" >

        <TextView
            android:id="@+id/txt_ardquestion"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:gravity="center"
            android:text="What is Android?"
            android:textColor="#363A43"
            android:textSize="18sp">
        </TextView>

        <LinearLayout
            android:id="@+id/progress_details_android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:layout_marginTop="16dp" >

            <ProgressBar
                android:id="@+id/progressBar_ard"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:minHeight="50dp"
                android:progress="0"
                android:indeterminate="false"
                android:max="10">
            </ProgressBar>

            <TextView
                android:id="@+id/txt_progress_ard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:gravity="center"
                android:textColorHint="#7A8089"
                android:textSize="14sp"
                tools:text="0/10">
            </TextView>

        </LinearLayout>

        <TextView
            android:id="@+id/txt_option1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/border_bg"
            android:padding="15dp"
            android:gravity="center"
            android:textColor="#7A8089"
            android:textSize="18sp"
            tools:text="Apple">
        </TextView>

        <TextView
            android:id="@+id/txt_option2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/border_bg"
            android:padding="15dp"
            android:gravity="center"
            android:textColor="#7A8089"
            android:textSize="18sp"
            tools:text="Google">
        </TextView>

        <TextView
            android:id="@+id/txt_option3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/border_bg"
            android:padding="15dp"
            android:gravity="center"
            android:textColor="#7A8089"
            android:textSize="18sp"
            tools:text="iOS">
        </TextView>

        <TextView
            android:id="@+id/txt_option4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/border_bg"
            android:padding="15dp"
            android:gravity="center"
            android:textColor="#7A8089"
            android:textSize="18sp"
            tools:text="Huawei">
        </TextView>

        <Button
            android:id="@+id/btn_ardsubmit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/design_default_color_primary"
            android:text="Submit"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

</ScrollView>
Enter fullscreen mode Exit fullscreen mode

In the activity_android_result.xml we can create the UI screen for result.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@drawable/ardresult_bg"
    android:padding="20dp"
    tools:context=".android.AndroidResultActivity">

    <TextView
        android:id="@+id/txt_ardresult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="25dp"
        android:text="Result"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:textSize="22sp">
    </TextView>

    <ImageView
        android:id="@+id/img_ardtrophy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:src="@drawable/trophy"/>

    <TextView
        android:id="@+id/txt_ardcongratulations"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Hi, Congratulations!!"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:textSize="25sp">
    </TextView>

    <TextView
        android:id="@+id/txt_ardscore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="Your Score is 5 out of 10"
        android:textColor="@color/hwid_auth_button_color_red"
        android:textSize="20sp">
    </TextView>

    <Button
        android:id="@+id/btn_ardfinish"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@android:color/white"
        android:text="Finish"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="18sp"
        android:textStyle="bold" />

</LinearLayout>
Enter fullscreen mode Exit fullscreen mode

In the activity_hmsactivity.xml we can create the UI screen for questions.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context=".hms.HMSActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp"
        android:gravity="center" >

        <TextView
            android:id="@+id/txt_hmsquestion"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:gravity="center"
            android:text="What is hms?"
            android:textColor="#363A43"
            android:textSize="18sp">
        </TextView>

        <LinearLayout
            android:id="@+id/progress_details_hms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:layout_marginTop="16dp" >

            <ProgressBar
                android:id="@+id/progressBar_hms"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:minHeight="50dp"
                android:progress="0"
                android:indeterminate="false"
                android:max="10">
            </ProgressBar>

            <TextView
                android:id="@+id/txt_progress_hms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:gravity="center"
                android:textColorHint="#7A8089"
                android:textSize="14sp"
                tools:text="0/10">
            </TextView>
        </LinearLayout>

        <TextView
            android:id="@+id/txt_option1_hms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/border_bg"
            android:padding="15dp"
            android:gravity="center"
            android:textColor="#7A8089"
            android:textSize="18sp"
            tools:text="Apple">
        </TextView>

        <TextView
            android:id="@+id/txt_option2_hms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/border_bg"
            android:padding="15dp"
            android:gravity="center"
            android:textColor="#7A8089"
            android:textSize="18sp"
            tools:text="Google">
        </TextView>

        <TextView
            android:id="@+id/txt_option3_hms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/border_bg"
            android:padding="15dp"
            android:gravity="center"
            android:textColor="#7A8089"
            android:textSize="18sp"
            tools:text="iOS">
        </TextView>

        <TextView
            android:id="@+id/txt_option4_hms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/border_bg"
            android:padding="15dp"
            android:gravity="center"
            android:textColor="#7A8089"
            android:textSize="18sp"
            tools:text="Huawei">
        </TextView>

        <Button
            android:id="@+id/btn_hms_submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/design_default_color_primary"
            android:text="Submit"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

</ScrollView>
Enter fullscreen mode Exit fullscreen mode

In the activity_hmsresult.xml we can create the UI screen for result.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@drawable/hmsresult_bg"
    android:padding="20dp"
    tools:context=".hms.HMSResultActivity">

    <TextView
        android:id="@+id/txt_hmsresult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="25dp"
        android:text="Result"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:textSize="22sp">
    </TextView>

    <ImageView
        android:id="@+id/img_hmstrophy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:src="@drawable/victory"/>

    <TextView
        android:id="@+id/txt_hmscongratulations"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Congrats done HMS Quiz!!"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:textSize="25sp">
    </TextView>

    <TextView
        android:id="@+id/txt_hmsscore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="Your Score is 6 out of 10"
        android:textColor="@color/black"
        android:textSize="20sp">
    </TextView>

    <Button
        android:id="@+id/btn_hmsfinish"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@android:color/white"
        android:text="Finish"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="18sp"
        android:textStyle="bold" />

</LinearLayout>
Enter fullscreen mode Exit fullscreen mode

Demo
Image description

Image description

Tips and Tricks

  1. Make sure you are already registered as Huawei developer.
  2. Set minSDK version to 24 or later, otherwise you will get AndriodManifest merge issue.
  3. Make sure you have added the agconnect-services.json file to app folder.
  4. Make sure you have added SHA-256 fingerprint without fail.
  5. Make sure all the dependencies are added properly.

Conclusion
In this article, we have learned about the basic questions on Android and HMS of Quiz application. The purpose of conducting quizzes within the business is more fun and educative to understand how the business is running straight away, test the knowledge of the user, and help to form a businessman or woman into a far better person in terms of the business process. So, I will provide a series of articles on this Quiz App, in upcoming articles

I hope you have read this article. If you found it is helpful, please provide likes and comments.

Reference
Click the URL - https://www.geeksforgeeks.org/how-to-create-a-quiz-app-in-android/

Top comments (0)