Dávid Csurgó February 2016

Adding TextViews dynamically

I'm working on a project which asks true-false questions on android. While asking, it collects the questions, the given answers and the correct answers into a Results[] object array. After the user finished with the questions the ResultsActivity starts and I'm struggling to show the results in the following format:

Line 1: First Question

Line 2: The correct answer, the user's answer

Line 3: Second Question

....

I'll attach the ResultsActivity and the content_results.xml. Could you help me to get on the right path with this?

public class ResultsActivity extends AppCompatActivity {


    private TextView questionText;
    private TextView resultText;
    private TextView givenText;
    private LinearLayout rContent;
    private LinearLayout rSecondaryContent;

    private void ResultQuestionFiller(int i){

            rContent = (LinearLayout) findViewById(R.id.results_content);
            questionText = new TextView(this);
            ViewGroup.LayoutParams fullWidth = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            questionText.setText(Results.True_False_Results[i].getQuestionResourceId());
            rContent.addView(questionText, fullWidth);
        }

    private void ResultAnswerFiller(int i){

        rSecondaryContent = (LinearLayout) findViewById(R.id.results_content_secondary);
        resultText = new TextView(this);
        ViewGroup.LayoutParams splitWidth = new ViewGroup.LayoutParams(100, ViewGroup.LayoutParams.WRAP_CONTENT);

        if (Results.True_False_Results[i].isTFgivenAnswer() == Results.True_False_Results[i].isTFcorrectAnswer()){
            resultText.setText(R.string.right_answer);
            resultText.setTextColor(Color.parseColor("#009933"));
        }
        else{
            resultText.setText(R.string.false_answer);
            resultText.setTextColor(Colo        

Answers


Dalma Racz February 2016

Using a ListView or a RecyclerView would be much more efficient. Here is the code to achieve what you want using a RecyclerView:

Create an object to hold question, user answer and correct answer:

public class QuestionAnswerObject {

    private String question;
    private String userAnswer;
    private String correctAnswer;

    public QuestionAnswerObject(String question, String userAnswer, String correctAnswer) {
        this.question = question;
        this.userAnswer = userAnswer;
        this.correctAnswer = correctAnswer;
    }

    public String getQuestion() {
        return question;
    }

    public String getUserAnswer() {
        return userAnswer;
    }

    public String getCorrectAnswer() {
        return correctAnswer;
    }
}

ActivityMain layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />
</RelativeLayout>

RecyclerView item layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">

<TextView
    android:id="@+id/question"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingTop="5dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<LinearLayout
    android:layout_width 

Post Status

Asked in February 2016
Viewed 2,088 times
Voted 12
Answered 1 times

Search




Leave an answer