lundi 22 mai 2017

Android LinearLayout one element per line

I would like programmatically align one CheckBox element per line in a LinearLayout. I'm adding the CheckBox element dynamically in the LinearLayout object.

I've try to set CheckBox with setLines(1), setWidth(400).

public class QuizQuestionActivity extends AppCompatActivity {

    TextView questionText;
    LinearLayout answerLinearLayout;
    ArrayList<CheckBox> answers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_quiz_question);
        setTitleQuestion("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pellentesque pulvinar rutrum.");

        answerLinearLayout = (LinearLayout) findViewById(R.id.answerLinearLayout);
        displayAnswers();
    }

    private void setTitleQuestion(String titleQuestion) {
        setContentView(R.layout.activity_quiz_question);
        questionText = (TextView) findViewById(R.id.titleQuestionText);
        questionText.setText(titleQuestion);
    }

    private void displayAnswers() {
        createAnswers();
        for (CheckBox answer: answers) {
            answerLinearLayout.addView(answer);
        }
    }

    private void createAnswers() {
        answers = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            String content = "Réponse : "+i;
            CheckBox checkbox = createAnswer(content);
            System.out.println(checkbox);

            answers.add(checkbox);
        }
    }

    private CheckBox createAnswer(String text) {
        CheckBox answer = new CheckBox(this);

        answer.setLines(1);
        answer.setWidth(400);
        answer.setText(text);
        answer.setClickable(true);

        return answer;
    }

    private String getQuizQuestionId() {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            return extras.getString(MainActivity.INTENT_QUIZ_QUESTION_ID);
        }

        return null;
    }
}




<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:app="http://ift.tt/GEGVYd"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.alexbdev.quizzy.QuizQuestionActivity">


    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="368dp"
        android:layout_height="80dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/titleQuestionText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:ellipsize="none"
            android:gravity="left"
            android:textStyle="normal" />
    </ScrollView>

    <LinearLayout
        android:id="@+id/answerLinearLayout"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:orientation="horizontal"
        app:layout_constraintTop_toBottomOf="@+id/scrollView2"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"></LinearLayout>

</android.support.constraint.ConstraintLayout>

Edit:

Ok i think i need to pass LinearLayout.LayoutParams as second parameters to the function addView, i've tried these following code but only the first element is displayed.

private void displayAnswers() {
    createAnswers();
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
    );
    for (CheckBox answer: answers) {
        answerLinearLayout.addView(answer, params);
    }
}




Aucun commentaire:

Enregistrer un commentaire