0

I am trying to create a series of Buttons inside a LinearLayout. So I have the following code

XML

<LinearLayout
            android:id="@+id/yearContainer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

in my Activity

LinearLayout yearContainer=(LinearLayout)findViewById(R.id.yearContainer);
for(int i=0;i<16;i++){
Button btn=new Button(this);
    btn.setText("Button "+i);
    btn.setId(150+i);

    LinearLayout.LayoutParams params=new    LinearLayout.LayoutParams(width/3,LayoutParams.WRAP_CONTENT);
    btn.setLayoutParams(params);

    yearContainer.addView(btn);

}

But the Buttons are arranging vertically. I need it as the following pattern.

enter image description here

I am new to android. Please advise

Thanks in advance

0

2 Answers 2

1

You can't do what yo want with a single LinearLayout. You need to use either a TableLayout, or create several Horizontal LinearLayouts inside the Vertical LinearLayout.

Sign up to request clarification or add additional context in comments.

Comments

0

Hi there as mentioned before this can be done using the "Table Layout" or "Grid Layout". But I will also suggest you to use nested simple linear and horizontal layouts. This will make sure your layout will be supported even in phones with older version of Android OS. As Grid Layout requires API Level 14 or higher.

So the code below should get you the result you expected with simple layouts.

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="4" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_weight="1"
            android:weightSum="4" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="1" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="2" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="3" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="4" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="4" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="5" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="6" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="7" />

            <Button
                android:id="@+id/btn_Sampleact_H"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="8" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="4" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="9" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="10" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="11" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="12" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="4" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="13" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="14" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="15" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="16" />
        </LinearLayout>
    </LinearLayout>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.