CheckboxQuestions is a library that provides with different forms of asking questions. So far there are YesOrNoQuestions, MultipleChoiceQuestions, and MultipleAnswerQuestions. These questions utilize checkboxes to make an elegant looking UI that will fit seamlessly into your app.
- Images
- Implementation
- QuestionsList
- YesOrNoQuestions
- MultipleChoiceQuestions
- MultipleAnswerQuestions
- Questions
- OnAnswerChangedListener
(Second question number is Two instead of 2 as to showcase you can put anything you want as a number)
Screen.Recording.2021-04-22.at.8.28.30.AM.mov
(Above video gets questions from an API)
In your project level build.gradle first add JitPack:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
In your app level build.gradle add:
dependencies {
implementation 'com.github.Cyber-cp:CheckboxQuestions:LATEST_RELEASE'
}
First add JitPack to your build file:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then add the dependancy:
<dependency>
<groupId>com.github.Cyber-cp</groupId>
<artifactId>CheckboxQuestions</artifactId>
<version>LATEST_RELEASE</version>
</dependency>
This is the easiest way to add questions. This works by adding questions to a layout defined in your layout xml file. Here is an example layout file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/questionLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:orientation="vertical">
<Button
android:id="@+id/getAnswers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="logAnswers"
android:text="Press me!" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
(The button is to get answers, the scrollview is in case there are too many questions and they go offscreen)
We mainly only care about the linear layout in the scrollview.
LinearLayout linearLayout = findViewById(R.id.questionLayout);
Now we want to create our QuestionList, but first you have to give it some settings so the view is displayed to your likings.
QuestionListSettings questionListSettings = new QuestionListSettings.SettingsBuilder()
.setCheckboxLocation(Question.LEFT)
.setCheckBoxOrientation(Question.SPLIT_VERTICAL)
.setNumberEnabled(false)
.setOptionTextSize(20)
.setQuestionTextSize(24)
.setSpacing(15)
.create();
The setCheckboxLocation()
allows you to choose whether the options for the question are to the left, center, or right of the screen.
The setCheckBoxOrientation()
allows you to choose whether the options for the question are stacked or sideways to eachother.
The setNumberEnabled()
allows you to choose whether the questions have a visible number.
The setSpacing()
allows you to choose how far apart the options are from eachother.
The setQuestionTextSize()
and setOptionTextSize()
allows you to choose the text size for the question and th options respectively.
Now we want to create our list of questions. There are 2 ways to do this. If you want simple yes or no questionaire, just create a String array full of questions.
String[] string = new String[]{"Is 9+2 = 11?", "Are you happy?", "Did you eat breakfast?"};
If you want to make questions with a correct answer, create an ArrayList of Questions.
ArrayList<Question> list = new ArrayList<>();
list.add(new Question("How are you?", Question.NO_ANSWER, Question.MULTIPLE_CHOICE_QUESTION, "Good", "Bad"));
list.add(new Question("What is 17 * 5", 3, Question.MULTIPLE_CHOICE_QUESTION, "12", "22", "85", "105", "117"));
(You can add as many questions as you like)
Next we want to create our Question List using the settings you created and the list/array you created.
QuestionList questionList = new QuestionList(list, questionListSettings, context);
Now if you want to create the views, just call createQuestionViews()
.
questionList.createQuestionViews();
To add the views to your layout, you can use:
linearLayout.addView(questionList.getQuestionViews());
Generates all the question views. They are stored in a LinearLayout.
Returns a LinearLayout full of question views.
Returns a boolean that is true if all the questions are answered, and false if not all are answered.
Returns a decimal value of how many answers are correct.
Returns an Object ArrayList filled with the selected answers. You can loop through the arraylist using for(Object answer : answers)
and you can try to cast each answer to a specific object, like (int) answer
.
Returns a view that can either be a MultipleChoiceQuestion or YesOrNoQuestion. You can cast the view to a specific type of question and do any methods you want with it.
((MultipleChoiceQuestion) questionList.getQuestion(1)).setCheckedOption(0);
A listener which allows you to run code whenever the answer changes, the index is the index of the question in the QuestionList.
YesOrNoQuestions are a simple form of question which show a question with a number, and only allow a yes or no as an answer, while MultipleChoiceQuestions allow anything as an option. To use it in an XML layout just use the following code:
<com.aadyad.checkboxquestion.Views.YesOrNoQuestion
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:number_enabled="true"
app:spacing_between_boxes="35"
app:checkbox_location="center"
app:checkbox_orientation="horizontal"
app:question_number="1"
app:question_title="Do you have a cold?"
app:correct_answer="0"
app:question_text_size="20"
app:option_text_size="18" />
The number_enabled
attribute allows you to make the number visible or not.
The spacing_between_boxes
attribute allows you to choose the spacing between the checkboxes.
The checkbox_location
attribute allows you to choose whether the checkboxes are to the left, center, or right of the screen. To specify where they are just use app:checkbox_location="left"
, app:checkbox_location="center"
, orapp:checkbox_location="right"
.
The checkbox_orientation
attribute allows you to choose whether the checkboxes are stacked or if they are horizontal. To stack them use app:checkbox_orientation="split_vertical"
to use the horizontally use app:checkbox_orientation="horizontal"
The question_number
attribute allows you to set the number of the question.
The question_title
attribute allows you to set the question text.
The correct_answer
attribute allows you to set the correct answer. Put 0 for no answer.
The question_text_size
attribute allows you to set the question text size.
The option_text_size
attribute allows you to set the option text size.
A listener which allows you to run code whenever the answer changes.
Allows you to choose which option is checked using the option text.
Allows you to choose which option is checked using the index of the option (Starts at 1, NOT 0).
Sets the orientation of the checkboxes.
Sets the textsize of the question.
Sets the textsize for the options.
Returns an int of the selected answer.
Sets the question text.
Sets the question number.
Returns the TextView which holds the question text.
Returns the TextView which holds the question number text.
Returns an int which is the index of the correct answer (the index starts at 1, NOT 0). If it returns 1 that means the first checkbox is the correct answer
Returns a boolean that is true if the selected answer matches the correct answer, and false if it doesn't.
Sets the correct answer of the question.
<com.aadyad.checkboxquestion.Views.MultipleChoiceQuestion
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:question_title="What is the slope-intercept equation of a line?"
app:correct_answer="0"
app:question_number="10"
app:number_enabled="true"
app:option_text_size="20"
app:question_text_size="25"
app:spacing_between_boxes="30"
app:checkbox_orientation="full_vertical"
app:option_1="x = ym + b"
app:option_2="y = mx + b"
app:option_3="y = mb * x"
app:option_4="b = my / x"/>
The number_enabled
attribute allows you to make the number visible or not.
The spacing_between_boxes
attribute allows you to choose the spacing between the checkboxes.
The checkbox_location
attribute allows you to choose whether the checkboxes are to the left, center, or right of the screen. To specify where they are just use app:checkbox_location="left"
, app:checkbox_location="center"
, orapp:checkbox_location="right"
.
The checkbox_orientation
attribute allows you to choose whether the checkboxes are stacked or if they are horizontal. To stack them use app:checkbox_orientation="split_vertical"
to use the horizontally use app:checkbox_orientation="horizontal"
The question_number
attribute allows you to set the number of the question.
The question_title
attribute allows you to set the question text.
The correct_answer
attribute allows you to set the correct answer. Put 0 for no answer.
The question_text_size
attribute allows you to set the question text size.
The option_text_size
attribute allows you to set the option text size.
The option_1
attribute lets you set the text for option 1.
The option_2
attribute lets you set the text for option 2.
The option_3
attribute lets you set the text for option 3.
The option_4
attribute lets you set the text for option 4.
Allows you to choose which option is checked using the option text.
Allows you to choose which option is checked using the index of the option (Starts at 1, NOT 0).
A listener which allows you to run code whenever the answer changes.
Sets the orientation of the checkboxes.
Sets the textsize of the question.
Sets the textsize for the options.
Returns an int of the selected answer.
Sets the question text.
Returns a String array with all the options the question provides.
Returns a Checkbox object at a certain index (the index starts at 0).
Returns the TextView which holds the question text.
Returns the TextView which holds the question number text.
Returns an int which is the index of the correct answer (the index starts at 1, NOT 0). If it returns 1 that means the first checkbox is the correct answer
Returns a boolean that is true if the selected answer matches the correct answer, and false if it doesn't.
Sets the correct answer of the question.
Sets the text of an option retrieved by an index. The option is retrieved by the index in the order they can be seen in.
<com.aadyad.checkboxquestion.Views.MultipleAnswerQuestion
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:question_title="Which equations are equivalent to 90 + 30"
app:question_number="10"
app:number_enabled="true"
app:option_text_size="20"
app:question_text_size="25"
app:spacing_between_boxes="30"
app:checkbox_orientation="full_vertical"
app:option_1="91 + 29"
app:option_2="50 + 70"
app:option_3="100 + 20"
app:option_4="99 + 890980"/>
The number_enabled
attribute allows you to make the number visible or not.
The spacing_between_boxes
attribute allows you to choose the spacing between the checkboxes.
The checkbox_location
attribute allows you to choose whether the checkboxes are to the left, center, or right of the screen. To specify where they are just use app:checkbox_location="left"
, app:checkbox_location="center"
, orapp:checkbox_location="right"
.
The checkbox_orientation
attribute allows you to choose whether the checkboxes are stacked or if they are horizontal. To stack them use app:checkbox_orientation="split_vertical"
to use the horizontally use app:checkbox_orientation="horizontal"
The question_number
attribute allows you to set the number of the question.
The question_title
attribute allows you to set the question text.
The question_text_size
attribute allows you to set the question text size.
The option_text_size
attribute allows you to set the option text size.
The option_1
attribute lets you set the text for option 1.
The option_2
attribute lets you set the text for option 2.
The option_3
attribute lets you set the text for option 3.
The option_4
attribute lets you set the text for option 4.
Allows you to choose which option is checked using the option text.
Allows you to choose which option is checked using the index of the option (Starts at 1, NOT 0).
A listener which allows you to run code whenever the answer changes.
Sets the orientation of the checkboxes.
Sets the textsize of the question.
Sets the textsize for the options.
Returns an Integer ArrayList of the selected answer.
Sets the question text.
Returns a String array with all the options the question provides.
Returns a Checkbox object at a certain index (the index starts at 0).
Returns the TextView which holds the question text.
Returns the TextView which holds the question number text.
Returns an Integer ArrayList which contains the indexes of the correct answers (the index starts at 1, NOT 0).
Returns a boolean that is true if the selected answers matches the correct answer, and false if it doesn't.
Sets the correct answer of the question.
Sets the text of an option retrieved by an index. The option is retrieved by the index in the order they can be seen in.
Questions are an object that allow you to make a QuestionList full of Multiple Choice Questions. There are 3 constructors for the Question object, which means there are 2 ways to define your Question object
One way:
Question q = new Question("What is the slope intercept equation of a line?", 2, Question.MULTIPLE_CHOICE_QUESTION, "x = yb + m", "y = mx + b", "m = yx + b", "b = mx + y");
The first argument that was passed is the question, The third arg is the type of question, the fourth arg is a String array full of possible answers, and the second arg is the index (this index STARTS at 1, NOT 0) of the correct answer in the array.
The fourth arg can also be written as:
new String[]{"x = yb + m", "y = mx + b", "m = yx + b", "b = mx + y"}
Another way to create a Question is:
Question q = new Question("What is the slope intercept equation of a line?", "y = mx + b", Question.MULTIPLE_CHOICE_QUESTION, "x = yb + m", "y = mx + b", "m = yx + b", "b = mx + y");
The second arg here is a string version of the correct answer.
The final way is:
Question q = new Question("What is the slope intercept equation of a line?", new ArrayList<Integer>(Arrays.asList(1, 2, 3)), Question.MULTIPLE_ANSWER_QUESTION, "x = yb + m", "y = mx + b", "m = yx + b", "b = mx + y");
In this example, the second arg is an Integer ArrayList of the correct answers. Everything else stays the same.
If there is no answer, the second arg can be set to Question.NO_ANSWER (or 0), or null.
A listener that allows you to detect when the answer for a question is changed. It may look like this:
multipleChoiceQuestion.addOnAnswerChangedListener(new OnAnswerChangedListener() {
@Override
public void onAnswerChanged(int selectedAnswerIndex, String selectedAnswerText) {
}
@Override
public void onAnswerChanged(ArrayList<Integer> listOfSelectedAnswerIndexes) {
}
});
DISCLAIMER: The names of the variables may not be correct when you implement this listener into your app. All indexes used in this interface start at 1. The onAnswerChanged without an ArrayList is used for YesOrNoQuestions and MultipleChoiceQuestions, while the method with an ArrayList is for MultipleAnswerQuestions.