package com.noprestige.kanaquiz;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import org.apmem.tools.layouts.FlowLayout;

import java.util.ArrayList;

public class MultipleChoicePad extends FlowLayout
{
    private ArrayList<Button> btnChoices = new ArrayList<>();

    public MultipleChoicePad(Context context)
    {
        super(context);
        init(null, 0);
    }

    public MultipleChoicePad(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(attrs, 0);
    }

    public MultipleChoicePad(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle)
    {
    }

    public void onAnswerSubmit(String userAnswer)
    {
        for (Button btnChoice : btnChoices)
            btnChoice.setEnabled(false);
        ((MainQuiz) getContext()).checkAnswer(userAnswer);
    }

    public void enableButtons()
    {
        for (Button btnChoice : btnChoices)
            btnChoice.setEnabled(true);
    }

    public void deleteChoices()
    {
        this.removeAllViews();
        btnChoices = new ArrayList<>();
    }

    public void addChoice(final String answer)
    {
        Button btnNewButton = new Button(this.getContext());
        btnNewButton.setOnClickListener(
                new OnClickListener()
                {
                    @Override
                    public void onClick(View view)
                    {
                        //ref: https://stackoverflow.com/questions/1521640/standard-android-button-with-a-different-color
                        view.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
                        onAnswerSubmit(answer);
                        btnChoices.remove(view); //prevents selected button from being re-enabled
                    }
                }
        );
        btnNewButton.setText(answer);
        this.addView(btnNewButton);
        btnChoices.add(btnNewButton);
    }

    public void setChoices(String[] answers)
    {
        deleteChoices();
        for (String answer : answers)
            addChoice(answer);
    }
}

@@ -1340,141 +1340,47 @@
    p
-ublic void addChoice(final String answer)%0A    %7B%0A        Button btnNewButton = new Button(this.getContext());%0A        btnNewButton.set
+rivate class AnswerListener implements 
 OnCl
@@ -1390,17 +1390,22 @@
 Listener
-(
+%0A    %7B
 %0A       
@@ -1409,68 +1409,117 @@
     
-        new OnClickListener()%0A                %7B%0A      
+String answer;%0A%0A        AnswerListener(String answer)%0A        %7B%0A            this.answer = answer;%0A
       
+  %7D%0A%0A
     
@@ -1532,28 +1532,16 @@
 verride%0A
-            
         
@@ -1583,33 +1583,10 @@
     
-            %7B%0A           
+%7B%0A
     
@@ -1593,17 +1593,16 @@
         
- 
 //ref: h
@@ -1691,28 +1691,16 @@
 t-color%0A
-            
         
@@ -1790,28 +1790,16 @@
         
- 
-           
 onAnswer
@@ -1826,28 +1826,16 @@
         
-  
-          
 btnChoic
@@ -1912,48 +1912,191 @@
     
+%7D%0A
     
+%7D%0A%0A
     
-    %7D%0A                %7D%0A        
+public void addChoice(String answer)%0A    %7B%0A        Button btnNewButton = new Button(this.getContext());%0A        btnNewButton.setOnClickListener(new AnswerListener(answer)
 );%0A 

Split off code for OnClickListener