diff --git a/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/OnAnswerChangedListener.java b/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/OnAnswerChangedListener.java new file mode 100644 index 0000000..3d3b91c --- /dev/null +++ b/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/OnAnswerChangedListener.java @@ -0,0 +1,8 @@ +package com.aadyad.checkboxquestion; + +import java.util.ArrayList; + +public interface OnAnswerChangedListener { + public void onAnswerChanged(int answer); + public void onAnswerChanged(ArrayList answer); +} diff --git a/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/QuestionList.java b/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/QuestionList.java index bc20d9e..0219f91 100644 --- a/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/QuestionList.java +++ b/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/QuestionList.java @@ -22,7 +22,7 @@ public class QuestionList { Context context; int orientation; - public QuestionList(ArrayList questions, QuestionListSettings settings, Context context){ + public QuestionList(ArrayList questions, QuestionListSettings settings, Context context) { choiceQuestions = questions; this.settings = settings; this.context = context; @@ -30,7 +30,7 @@ public QuestionList(ArrayList questions, QuestionListSettings settings linearLayout = new LinearLayout(context); } - public QuestionList(String[] questions, QuestionListSettings settings, Context context){ + public QuestionList(String[] questions, QuestionListSettings settings, Context context) { this.questions = questions; this.settings = settings; this.orientation = settings.getCheckBoxOrientation(); @@ -38,12 +38,12 @@ public QuestionList(String[] questions, QuestionListSettings settings, Context c linearLayout = new LinearLayout(context); } - public void createQuestionViews(){ + public void createQuestionViews() { int i = 1; linearLayout = new LinearLayout(context); linearLayout.setOrientation(LinearLayout.VERTICAL); - if (questions == null){ - for (Question q : choiceQuestions){ + if (questions == null) { + for (Question q : choiceQuestions) { switch (q.type) { case Question.MULTIPLE_CHOICE_QUESTION: Log.d("TAG", "createQuestionViews: " + Arrays.toString(q.options)); @@ -77,7 +77,7 @@ public void createQuestionViews(){ } - public void setLayoutOrientation(int orientation){ + public void setLayoutOrientation(int orientation) { this.orientation = orientation; for (int i = 0; i < linearLayout.getChildCount(); i++) { YesOrNoQuestion yesOrNoQuestion; @@ -108,7 +108,7 @@ public void setLayoutOrientation(int orientation){ createQuestionViews(); } - public float getPercentageOfCorrectAnswers(){ + public float getPercentageOfCorrectAnswers() { int correctAnswers = 0; int allAnswers = linearLayout.getChildCount(); for (int i = 0; i < linearLayout.getChildCount(); i++) { @@ -122,9 +122,9 @@ public float getPercentageOfCorrectAnswers(){ Log.d("TAG", "answer: " + answer); int correctAnswer = choiceQuestions.get(i).correctAnswer; Log.d("TAG", "correct answer: " + correctAnswer); - if (correctAnswer == 0){ + if (correctAnswer == 0) { allAnswers--; - } else if (answer == correctAnswer){ + } else if (answer == correctAnswer) { correctAnswers++; } } catch (ClassCastException ignored) { @@ -137,9 +137,9 @@ public float getPercentageOfCorrectAnswers(){ Log.d("TAG", "answer: " + answer); int correctAnswer = choiceQuestions.get(i).correctAnswer; Log.d("TAG", "correct answer: " + correctAnswer); - if (correctAnswer == 0){ + if (correctAnswer == 0) { allAnswers--; - } else if (answer == correctAnswer){ + } else if (answer == correctAnswer) { correctAnswers++; } } catch (Exception ignored) { @@ -154,9 +154,9 @@ public float getPercentageOfCorrectAnswers(){ ArrayList correctAnswer = choiceQuestions.get(i).multipleCorrectAnswer; Collections.sort(correctAnswer); Log.d("TAG", "correct answer: " + correctAnswer); - if (correctAnswer.size() == 0 || correctAnswer == null){ + if (correctAnswer.size() == 0 || correctAnswer == null) { allAnswers--; - } else if (answer.equals(correctAnswer)){ + } else if (answer.equals(correctAnswer)) { correctAnswers++; } } catch (ClassCastException ignored) { @@ -165,10 +165,10 @@ public float getPercentageOfCorrectAnswers(){ } Log.d("TAG", "getPercentageOfCorrectAnswers: " + correctAnswers); Log.d("TAG", "getPercentageOfCorrectAnswers: " + allAnswers); - return (float) correctAnswers/allAnswers; + return (float) correctAnswers / allAnswers; } - public ArrayList getAnswers(){ + public ArrayList getAnswers() { ArrayList answers = new ArrayList<>(); Log.d("answers", "list size: " + linearLayout.getChildCount()); for (int i = 0; i < linearLayout.getChildCount(); i++) { @@ -200,7 +200,7 @@ public ArrayList getAnswers(){ return answers; } - public boolean areAllQuestionsAnswered(){ + public boolean areAllQuestionsAnswered() { Log.d("answers", "list size: " + linearLayout.getChildCount()); for (int i = 0; i < linearLayout.getChildCount(); i++) { YesOrNoQuestion yesOrNoQuestion; @@ -209,7 +209,7 @@ public boolean areAllQuestionsAnswered(){ try { yesOrNoQuestion = (YesOrNoQuestion) getQuestion(i); - if (yesOrNoQuestion.getAnswer() == 0){ + if (yesOrNoQuestion.getAnswer() == 0) { return false; } } catch (ClassCastException ignored) { @@ -217,7 +217,7 @@ public boolean areAllQuestionsAnswered(){ } try { multipleChoiceQuestion = (MultipleChoiceQuestion) getQuestion(i); - if (multipleChoiceQuestion.getAnswer() == 0){ + if (multipleChoiceQuestion.getAnswer() == 0) { return false; } } catch (Exception ignored) { @@ -226,7 +226,7 @@ public boolean areAllQuestionsAnswered(){ try { multipleAnswerQuestion = (MultipleAnswerQuestion) getQuestion(i); - if (multipleAnswerQuestion.getAnswer().size() == 0){ + if (multipleAnswerQuestion.getAnswer().size() == 0) { return false; } } catch (Exception ignored) { @@ -236,7 +236,7 @@ public boolean areAllQuestionsAnswered(){ return true; } - public View getQuestion(int index){ + public View getQuestion(int index) { YesOrNoQuestion yesOrNoQuestion = null; MultipleChoiceQuestion multipleChoiceQuestion = null; MultipleAnswerQuestion multipleAnswerQuestion = null; @@ -259,27 +259,38 @@ public View getQuestion(int index){ } - if (yesOrNoQuestion != null){ + if (yesOrNoQuestion != null) { return yesOrNoQuestion; - }else if (multipleChoiceQuestion != null){ + } else if (multipleChoiceQuestion != null) { return multipleChoiceQuestion; - } else if(multipleAnswerQuestion != null){ + } else if (multipleAnswerQuestion != null) { return multipleAnswerQuestion; - }else { + } else { return null; } } - public void addOnValueChangedRunnable(int index, Runnable r){ + public void addOnAnswerChangedListener(int index, OnAnswerChangedListener r) { try { - ((MultipleChoiceQuestion) getQuestion(index)).doOnValueChanged(r); - } catch (Exception e) { - YesOrNoQuestion yesOrNoQuestion = (YesOrNoQuestion) getQuestion(index); - //Todo: add do on value changed + ((MultipleChoiceQuestion) getQuestion(index)).addOnAnswerChangedListener(r); + } catch (Exception ignored) { + + } + + try { + ((MultipleAnswerQuestion) getQuestion(index)).addOnAnswerChangedListener(r); + } catch (Exception ignored) { + + } + + try { + ((YesOrNoQuestion) getQuestion(index)).addOnAnswerChangedListener(r); + } catch (Exception ignored) { + } } - public LinearLayout getQuestionViews(){ + public LinearLayout getQuestionViews() { return linearLayout; } } diff --git a/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/Views/MultipleAnswerQuestion.java b/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/Views/MultipleAnswerQuestion.java index f57df2e..8ce423d 100644 --- a/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/Views/MultipleAnswerQuestion.java +++ b/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/Views/MultipleAnswerQuestion.java @@ -15,6 +15,7 @@ import androidx.annotation.Nullable; +import com.aadyad.checkboxquestion.OnAnswerChangedListener; import com.aadyad.checkboxquestion.Question; import com.aadyad.checkboxquestion.QuestionListSettings; import com.aadyad.checkboxquestion.R; @@ -29,7 +30,7 @@ public class MultipleAnswerQuestion extends LinearLayout { public static final int RIGHT = 2; Context context; - private Runnable onValueChanged; + private OnAnswerChangedListener onAnswerChangedListener; ArrayList checkBoxes = new ArrayList<>(); @@ -41,11 +42,14 @@ public class MultipleAnswerQuestion extends LinearLayout { public MultipleAnswerQuestion(Context context) { this(context, null); this.context = context; + this.onAnswerChangedListener = null; } public MultipleAnswerQuestion(Context context, @Nullable AttributeSet attrs) { super(context, attrs); this.context = context; + this.onAnswerChangedListener = null; + setOrientation(LinearLayout.VERTICAL); LayoutInflater.from(context).inflate(R.layout.multiple_answer_question, this, true); @@ -97,15 +101,6 @@ public void init(String title, String number, boolean numEnabled, int spacing, i layout = findViewById(R.id.multipleChoiceHolder); mainLayout = findViewById(R.id.mainLayout); - if (onValueChanged == null) { - onValueChanged = new Runnable() { - @Override - public void run() { - - } - }; - } - this.spacing = spacing; checkBoxes.add(option1); @@ -176,7 +171,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //Remove selected answer to integer arraylist selectedAnswers.remove(selectedAnswer); } - onValueChanged.run(); + if (onAnswerChangedListener != null) { + onAnswerChangedListener.onAnswerChanged(selectedAnswers); + } } }); @@ -213,7 +210,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //Remove selected answer to integer arraylist selectedAnswers.remove(selectedAnswer); } - onValueChanged.run(); + if (onAnswerChangedListener != null) { + onAnswerChangedListener.onAnswerChanged(selectedAnswers); + } } }); @@ -244,7 +243,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //Remove selected answer to integer arraylist selectedAnswers.remove(selectedAnswer); } - onValueChanged.run(); + if (onAnswerChangedListener != null) { + onAnswerChangedListener.onAnswerChanged(selectedAnswers); + } } }); @@ -328,7 +329,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //Remove selected answer to integer arraylist selectedAnswers.remove(selectedAnswer); } - onValueChanged.run(); + if (onAnswerChangedListener != null) { + onAnswerChangedListener.onAnswerChanged(selectedAnswers); + } } }); @@ -343,7 +346,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //Remove selected answer to integer arraylist selectedAnswers.remove(selectedAnswer); } - onValueChanged.run(); + if (onAnswerChangedListener != null) { + onAnswerChangedListener.onAnswerChanged(selectedAnswers); + } } }); @@ -358,7 +363,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //Remove selected answer to integer arraylist selectedAnswers.remove(selectedAnswer); } - onValueChanged.run(); + if (onAnswerChangedListener != null) { + onAnswerChangedListener.onAnswerChanged(selectedAnswers); + } } }); @@ -373,7 +380,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //Remove selected answer to integer arraylist selectedAnswers.remove(selectedAnswer); } - onValueChanged.run(); + if (onAnswerChangedListener != null) { + onAnswerChangedListener.onAnswerChanged(selectedAnswers); + } } }); } @@ -474,8 +483,8 @@ public void setCheckboxOrientation(int orientation){ } } - public void doOnValueChanged(Runnable runnable){ - this.onValueChanged = runnable; + public void addOnAnswerChangedListener(OnAnswerChangedListener onAnswerChangedListener){ + this.onAnswerChangedListener = onAnswerChangedListener; } public void setCheckedOption(int option){ diff --git a/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/Views/MultipleChoiceQuestion.java b/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/Views/MultipleChoiceQuestion.java index ce9e5cd..50b06ac 100644 --- a/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/Views/MultipleChoiceQuestion.java +++ b/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/Views/MultipleChoiceQuestion.java @@ -14,6 +14,7 @@ import androidx.annotation.Nullable; +import com.aadyad.checkboxquestion.OnAnswerChangedListener; import com.aadyad.checkboxquestion.Question; import com.aadyad.checkboxquestion.QuestionListSettings; import com.aadyad.checkboxquestion.R; @@ -28,7 +29,7 @@ public class MultipleChoiceQuestion extends LinearLayout { public static final int RIGHT = 2; Context context; - private Runnable onValueChanged; + private OnAnswerChangedListener onAnswerChangedListener; ArrayList checkBoxes = new ArrayList<>(); @@ -40,11 +41,13 @@ public class MultipleChoiceQuestion extends LinearLayout { public MultipleChoiceQuestion(Context context) { this(context, null); this.context = context; + this.onAnswerChangedListener = null; } public MultipleChoiceQuestion(Context context, @Nullable AttributeSet attrs) { super(context, attrs); this.context = context; + this.onAnswerChangedListener = null; setOrientation(LinearLayout.VERTICAL); LayoutInflater.from(context).inflate(R.layout.multiple_choice_question, this, true); @@ -94,15 +97,6 @@ public void init(String title, String number, boolean numEnabled, int spacing, i layout = findViewById(R.id.multipleChoiceHolder); mainLayout = findViewById(R.id.mainLayout); - if (onValueChanged == null) { - onValueChanged = new Runnable() { - @Override - public void run() { - - } - }; - } - this.spacing = spacing; checkBoxes.add(option1); @@ -136,20 +130,20 @@ public void run() { LinearLayout templayout = null; - if (options.length > 4){ + if (options.length > 4) { Log.d("TAG", "init: length greater than 4"); - for (int i = 4; i < options.length; i++){ + for (int i = 4; i < options.length; i++) { - if (templayout == null){ + if (templayout == null) { templayout = new LinearLayout(context); templayout.setOrientation(HORIZONTAL); } - if (orientation == Question.HORIZONTAL){ + if (orientation == Question.HORIZONTAL) { throw new RuntimeException("Cannot have more than 4 options when horizontal. Try split_vertical or vertical."); - } else if (orientation == Question.SPLIT_VERTICAL){ + } else if (orientation == Question.SPLIT_VERTICAL) { Log.d("TAG", "init: " + templayout.getChildCount()); - if (templayout.getChildCount() == 0){ + if (templayout.getChildCount() == 0) { Log.d("TAG", "init: child count 0"); View spacer = new View(context); spacer.setLayoutParams(new ViewGroup.LayoutParams(0, spacing)); @@ -165,15 +159,17 @@ public void run() { checkBox.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - for (CheckBox checkBox1 : checkBoxes){ - if (!checkBox1.equals(checkBox)){ + for (CheckBox checkBox1 : checkBoxes) { + if (!checkBox1.equals(checkBox)) { checkBox1.setChecked(false); - }else { + } else { checkBox1.setChecked(true); } } buttonClicked = finalI + 1; - onValueChanged.run(); + if (onAnswerChangedListener != null) { + onAnswerChangedListener.onAnswerChanged(buttonClicked); + } } }); @@ -189,7 +185,7 @@ public void onClick(View v) { e.printStackTrace(); layout.addView(templayout); } - } else if (templayout.getChildCount() == 2){ + } else if (templayout.getChildCount() == 2) { Log.d("TAG", "init: child count 2"); //Creating new checkbox @@ -201,15 +197,17 @@ public void onClick(View v) { checkBox.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - for (CheckBox checkBox1 : checkBoxes){ - if (!checkBox1.equals(checkBox)){ + for (CheckBox checkBox1 : checkBoxes) { + if (!checkBox1.equals(checkBox)) { checkBox1.setChecked(false); - }else { + } else { checkBox1.setChecked(true); } } buttonClicked = finalI + 1; - onValueChanged.run(); + if (onAnswerChangedListener != null) { + onAnswerChangedListener.onAnswerChanged(buttonClicked); + } } }); @@ -217,9 +215,9 @@ public void onClick(View v) { layout.addView(templayout); templayout = null; } - } else if (orientation == Question.FULL_VERTICAL){ + } else if (orientation == Question.FULL_VERTICAL) { Log.d("TAG", "init: " + templayout.getChildCount()); - if (templayout.getChildCount() == 0){ + if (templayout.getChildCount() == 0) { Log.d("TAG", "init: child count 0"); //Creating new checkbox @@ -231,15 +229,17 @@ public void onClick(View v) { checkBox.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - for (CheckBox checkBox1 : checkBoxes){ - if (!checkBox1.equals(checkBox)){ + for (CheckBox checkBox1 : checkBoxes) { + if (!checkBox1.equals(checkBox)) { checkBox1.setChecked(false); - }else { + } else { checkBox1.setChecked(true); } } buttonClicked = finalI + 1; - onValueChanged.run(); + if (onAnswerChangedListener != null) { + onAnswerChangedListener.onAnswerChanged(buttonClicked); + } } }); @@ -261,19 +261,19 @@ public void onClick(View v) { if (a3 == null) { option3.setVisibility(GONE); - } else{ + } else { option3.setVisibility(VISIBLE); } if (a4 == null) { option4.setVisibility(GONE); - } else{ + } else { option4.setVisibility(VISIBLE); } - if (option4.getText().toString().equals("")){ + if (option4.getText().toString().equals("")) { option4.setVisibility(GONE); } - if (option3.getText().toString().equals("")){ + if (option3.getText().toString().equals("")) { option3.setVisibility(GONE); } @@ -295,11 +295,11 @@ public void onClick(View v) { } private void setCheckBoxLocation(int location) { - if (location == LEFT){ + if (location == LEFT) { mainLayout.setGravity(Gravity.LEFT); - } else if (location == CENTER){ + } else if (location == CENTER) { mainLayout.setGravity(Gravity.CENTER); - } else if (location == RIGHT){ + } else if (location == RIGHT) { mainLayout.setGravity(Gravity.RIGHT); } } @@ -315,66 +315,75 @@ protected void onFinishInflate() { option1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - for (CheckBox checkBox1 : checkBoxes){ - if (!checkBox1.equals(option1)){ + for (CheckBox checkBox1 : checkBoxes) { + if (!checkBox1.equals(option1)) { checkBox1.setChecked(false); - }else { + } else { option1.setChecked(true); } } buttonClicked = 1; - onValueChanged.run(); + if (onAnswerChangedListener != null) { + onAnswerChangedListener.onAnswerChanged(buttonClicked); + } } }); option2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - for (CheckBox checkBox1 : checkBoxes){ - if (!checkBox1.equals(option2)){ - if (checkBox1.isChecked()){ + for (CheckBox checkBox1 : checkBoxes) { + if (!checkBox1.equals(option2)) { + if (checkBox1.isChecked()) { checkBox1.setChecked(false); } - }else { + } else { checkBox1.setChecked(true); } } buttonClicked = 2; - onValueChanged.run(); + if (onAnswerChangedListener != null) { + onAnswerChangedListener.onAnswerChanged(buttonClicked); + } } }); option3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - for (CheckBox checkBox1 : checkBoxes){ - if (!checkBox1.equals(option3)){ - if (checkBox1.isChecked()){ + for (CheckBox checkBox1 : checkBoxes) { + if (!checkBox1.equals(option3)) { + if (checkBox1.isChecked()) { checkBox1.setChecked(false); } - }else { + } else { checkBox1.setChecked(true); } } buttonClicked = 3; - onValueChanged.run(); + + if (onAnswerChangedListener != null) { + onAnswerChangedListener.onAnswerChanged(buttonClicked); + } } }); option4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - for (CheckBox checkBox1 : checkBoxes){ - if (!checkBox1.equals(option4)){ - if (checkBox1.isChecked()){ + for (CheckBox checkBox1 : checkBoxes) { + if (!checkBox1.equals(option4)) { + if (checkBox1.isChecked()) { checkBox1.setChecked(false); } - }else { + } else { checkBox1.setChecked(true); } } - onValueChanged.run(); buttonClicked = 4; + if (onAnswerChangedListener != null) { + onAnswerChangedListener.onAnswerChanged(buttonClicked); + } } }); } @@ -388,15 +397,15 @@ public void setQuestion(String question) { questionTitle.setText(question); } - public void setOptionTextSize(float optionTextSize){ + public void setOptionTextSize(float optionTextSize) { final CheckBox option1 = (CheckBox) findViewById(R.id.answer1); final CheckBox option2 = (CheckBox) findViewById(R.id.answer2); final CheckBox option3 = (CheckBox) findViewById(R.id.answer3); final CheckBox option4 = (CheckBox) findViewById(R.id.answer4); - if (optionTextSize == QuestionListSettings.TEXT_SIZE_AUTO){ + if (optionTextSize == QuestionListSettings.TEXT_SIZE_AUTO) { //Todo: add code to auto adjust size using textheight - }else if (!(optionTextSize == QuestionListSettings.TEXT_SIZE_DEFAULT)){ + } else if (!(optionTextSize == QuestionListSettings.TEXT_SIZE_DEFAULT)) { option1.setTextSize(optionTextSize); option2.setTextSize(optionTextSize); option3.setTextSize(optionTextSize); @@ -404,13 +413,13 @@ public void setOptionTextSize(float optionTextSize){ } } - public void setQuestionTextSize(float questionTextSize){ + public void setQuestionTextSize(float questionTextSize) { TextView questionNumber = (TextView) findViewById(R.id.question_number); TextView questionTitle = (TextView) findViewById(R.id.question_title); - if (questionTextSize == QuestionListSettings.TEXT_SIZE_AUTO){ + if (questionTextSize == QuestionListSettings.TEXT_SIZE_AUTO) { //Todo: add code to auto adjust size using height - } else if (!(questionTextSize == QuestionListSettings.TEXT_SIZE_DEFAULT)){ + } else if (!(questionTextSize == QuestionListSettings.TEXT_SIZE_DEFAULT)) { questionTitle.setTextSize(questionTextSize); questionNumber.setTextSize(questionTextSize); } @@ -421,7 +430,7 @@ public void setQuestionNumber(String number) { questionNumber.setText(number + ". "); } - public void setCheckboxOrientation(int orientation){ + public void setCheckboxOrientation(int orientation) { View spacing1 = findViewById(R.id.spacing1); View layoutSpacing = findViewById(R.id.spacingLayouts); View spacing2 = findViewById(R.id.spacing2); @@ -432,19 +441,19 @@ public void setCheckboxOrientation(int orientation){ ViewGroup.LayoutParams layoutParams2 = spacing2.getLayoutParams(); ViewGroup.LayoutParams layoutParams3 = layoutSpacing.getLayoutParams(); - if (orientation == Question.HORIZONTAL){ + if (orientation == Question.HORIZONTAL) { //Log.d("Orientation", "setLayoutOrientation: horizontal"); layoutParams1.width = spacing; layoutParams2.width = spacing; layoutParams3.width = spacing; layoutParams1.height = 0; layoutParams2.height = 0; - layoutParams3.height= 0; + layoutParams3.height = 0; layoutSpacing.setLayoutParams(layoutParams3); spacing1.setLayoutParams(layoutParams1); spacing2.setLayoutParams(layoutParams2); layout.setOrientation(HORIZONTAL); - } else if (orientation == Question.SPLIT_VERTICAL){ + } else if (orientation == Question.SPLIT_VERTICAL) { layout.setOrientation(VERTICAL); layoutParams3.width = 0; layoutParams1.width = spacing; @@ -455,9 +464,9 @@ public void setCheckboxOrientation(int orientation){ layoutSpacing.setLayoutParams(layoutParams3); spacing1.setLayoutParams(layoutParams1); spacing2.setLayoutParams(layoutParams2); - } else if (orientation == Question.AUTO){ + } else if (orientation == Question.AUTO) { //Todo: code auto orientation if a question goes out of view - } else if (orientation == Question.FULL_VERTICAL){ + } else if (orientation == Question.FULL_VERTICAL) { LinearLayout layout1 = findViewById(R.id.checkBoxHolder1); LinearLayout layout2 = findViewById(R.id.checkBoxHolder2); layout1.setOrientation(VERTICAL); @@ -475,27 +484,29 @@ public void setCheckboxOrientation(int orientation){ } } - public void doOnValueChanged(Runnable runnable){ - this.onValueChanged = runnable; + public void addOnAnswerChangedListener(OnAnswerChangedListener onAnswerChangedListener) { + this.onAnswerChangedListener = onAnswerChangedListener; } - public void setCheckedOption(int option){ - for (int i = 0; i < checkBoxes.size(); i++){ - if (i == option - 1){ + + + public void setCheckedOption(int option) { + for (int i = 0; i < checkBoxes.size(); i++) { + if (i == option - 1) { checkBoxes.get(i).setChecked(true); break; - } else{ + } else { checkBoxes.get(i).setChecked(false); } } } - public void setCheckedOption(String option){ - for (CheckBox checkBox : checkBoxes){ - if (checkBox.getText().equals(option)){ + public void setCheckedOption(String option) { + for (CheckBox checkBox : checkBoxes) { + if (checkBox.getText().equals(option)) { checkBox.setChecked(true); break; - } else{ + } else { checkBox.setChecked(false); } } diff --git a/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/Views/YesOrNoQuestion.java b/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/Views/YesOrNoQuestion.java index 837e12c..f0ced40 100644 --- a/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/Views/YesOrNoQuestion.java +++ b/CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/Views/YesOrNoQuestion.java @@ -14,6 +14,7 @@ import androidx.annotation.Nullable; +import com.aadyad.checkboxquestion.OnAnswerChangedListener; import com.aadyad.checkboxquestion.Question; import com.aadyad.checkboxquestion.QuestionListSettings; import com.aadyad.checkboxquestion.R; @@ -24,6 +25,8 @@ public class YesOrNoQuestion extends LinearLayout { public static final int CENTER = 1; public static final int RIGHT = 2; + OnAnswerChangedListener onAnswerChangedListener; + private LinearLayout layout; private int spacing; @@ -31,12 +34,14 @@ public class YesOrNoQuestion extends LinearLayout { public YesOrNoQuestion(Context context) { this(context, null); + this.onAnswerChangedListener = null; } public YesOrNoQuestion(Context context, @Nullable AttributeSet attrs) { super(context, attrs); setOrientation(LinearLayout.VERTICAL); LayoutInflater.from(context).inflate(R.layout.yes_or_no_question, this, true); + this.onAnswerChangedListener = null; String title; String number; @@ -44,14 +49,14 @@ public YesOrNoQuestion(Context context, @Nullable AttributeSet attrs) { int boxLocation; int orientation; float questionTextSize; - float checkBozTextSize; + float checkBoxTextSize; boolean numberEnabled; TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.YesOrNoQuestion, 0, 0); try { //optionLayoutHeight = a.getFloat(R.styleable.YesOrNoQuestion_option_layout_height, QuestionListSettings.TEXT_SIZE_DEFAULT); questionTextSize = a.getFloat(R.styleable.YesOrNoQuestion_question_text_size, QuestionListSettings.TEXT_SIZE_DEFAULT); - checkBozTextSize = a.getFloat(R.styleable.YesOrNoQuestion_option_text_size, QuestionListSettings.TEXT_SIZE_DEFAULT); + checkBoxTextSize = a.getFloat(R.styleable.YesOrNoQuestion_option_text_size, QuestionListSettings.TEXT_SIZE_DEFAULT); //questionLayoutHeight = a.getFloat(R.styleable.YesOrNoQuestion_question_layout_height, QuestionListSettings.TEXT_SIZE_DEFAULT); title = a.getString(R.styleable.YesOrNoQuestion_question_title); boxLocation = a.getInt(R.styleable.YesOrNoQuestion_checkbox_location, 0); @@ -63,7 +68,7 @@ public YesOrNoQuestion(Context context, @Nullable AttributeSet attrs) { a.recycle(); } - init(title, number, numberEnabled, spacing, orientation, boxLocation, questionTextSize, checkBozTextSize); + init(title, number, numberEnabled, spacing, orientation, boxLocation, questionTextSize, checkBoxTextSize); } // Setup views @@ -112,6 +117,7 @@ public void onClick(View v) { no.setChecked(false); yes.setChecked(true); buttonClicked = 1; + onAnswerChangedListener.onAnswerChanged(buttonClicked); } }); @@ -121,6 +127,7 @@ public void onClick(View v) { no.setChecked(true); yes.setChecked(false); buttonClicked = 2; + onAnswerChangedListener.onAnswerChanged(buttonClicked); } }); } @@ -182,6 +189,10 @@ public void setCheckboxOrientation(int orientation){ } } + public void addOnAnswerChangedListener(OnAnswerChangedListener onAnswerChangedListener) { + this.onAnswerChangedListener = onAnswerChangedListener; + } + public void setCheckedOption(int option){ final CheckBox yes = (CheckBox) findViewById(R.id.yes); final CheckBox no = (CheckBox) findViewById(R.id.no); diff --git a/CheckboxQuestion/app/src/main/java/com/aadyad/checkboxquestions_library/MainActivity.java b/CheckboxQuestion/app/src/main/java/com/aadyad/checkboxquestions_library/MainActivity.java index 9010955..b2527a0 100644 --- a/CheckboxQuestion/app/src/main/java/com/aadyad/checkboxquestions_library/MainActivity.java +++ b/CheckboxQuestion/app/src/main/java/com/aadyad/checkboxquestions_library/MainActivity.java @@ -4,9 +4,11 @@ import android.util.Log; import android.view.View; import android.widget.LinearLayout; +import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; +import com.aadyad.checkboxquestion.OnAnswerChangedListener; import com.aadyad.checkboxquestion.Question; import com.aadyad.checkboxquestion.QuestionList; import com.aadyad.checkboxquestion.QuestionListSettings; @@ -101,14 +103,52 @@ public void run() { .create(); list.add(new Question("Hi", new ArrayList(Arrays.asList(1, 2, 3)), Question.MULTIPLE_ANSWER_QUESTION, "sa", "adasda", "adasd", "sdasd", "sahusaudsia")); + list.add(new Question("Yeet?", Question.NO_ANSWER, Question.YES_OR_NO_QUESTION)); questionList = new QuestionList(list, questionListSettings, getApplicationContext()); questionList.createQuestionViews(); + questionList.addOnAnswerChangedListener(0, new OnAnswerChangedListener() { + @Override + public void onAnswerChanged(int answer) { + Toast.makeText(MainActivity.this, "Answer: " + answer, Toast.LENGTH_SHORT).show(); + + } + + @Override + public void onAnswerChanged(ArrayList answer) { + + } + }); + + questionList.addOnAnswerChangedListener(21, new OnAnswerChangedListener() { + @Override + public void onAnswerChanged(int answer) { + Toast.makeText(MainActivity.this, "Answer: " + answer, Toast.LENGTH_SHORT).show(); + + } + + @Override + public void onAnswerChanged(ArrayList answer) { + + } + }); + + questionList.addOnAnswerChangedListener(20, new OnAnswerChangedListener() { + @Override + public void onAnswerChanged(int answer) { + + } + + @Override + public void onAnswerChanged(ArrayList answer) { + Toast.makeText(MainActivity.this, "Answer: " + answer, Toast.LENGTH_SHORT).show(); + } + }); + linearLayout.addView(questionList.getQuestionViews()); } }); - } public void onFailure(Call call, IOException e) {