0

I am pretty new to Android. I am trying to create buttons dynamically in android.

But all the buttons are coming vertically listed column wise.I would want 25 buttons to be distributed in 5 rows and 5 columns.

package com.sudarshan.tictactoenew;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.DynamicLayout;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

public class NumericalNextLevel extends AppCompatActivity {

    @Override



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numerical_next_level);
        createLayoutDynamically(5);
    }

    private void createLayoutDynamically(int n) {

        int count=0;
        for (int i = 0; i < n; i++)
        {
            for(int j=0;j<n;j++)
            {
                Button myButton = new Button(this);
                myButton.setText("");
                myButton.setId(count);

                final int id_ = myButton.getId();

                LinearLayout layout = (LinearLayout) findViewById(R.id.myDynamicLayout);
                layout.addView(myButton);

                myButton.setLayoutParams(new LayoutParams(50, LayoutParams.WRAP_CONTENT));
                count++;
            }


        }
    }

}
4
  • use a gridview instead of a linear layout Commented Jun 15, 2021 at 12:56
  • Hi Sudarshan , I have added one answer , you can try and if it is something you were looking for then mark it as right answer , it will help others also ! If it doesn't t work you can comment below the answer , Thanks ! Commented Jun 15, 2021 at 14:01
  • Thanks it works.Most simple solution Commented Jun 15, 2021 at 14:16
  • actually , instead using linear layout such purpose you can go with grid layout in android , it is very useful for such purpose ,anyways I am glad it worked for you ! Commented Jun 15, 2021 at 14:17

2 Answers 2

1

There are multiple ways to achieve this , as so far you are new but you have some Idea how Linea Layout works , so I am posting here code with Linear Layout !

Before going to Code let's undertand how it works , If you make layout using only linearlayout , it might be a trouble , becase Linear Layout set components in single direction like vertical or horizontal. but we can add multiple horiznotal Linearlayout in one Verttical Linear Layout then it will work like charm !

Here is rought drawaing what I am trying to say !

enter image description here

I have modified your function as below !

 private void createLayoutDynamically(int n) {

        int count=0;
        LinearLayout layout = (LinearLayout) findViewById(R.id.myDynamicLayout); //Main Vertical Linear Layout
        layout.setOrientation(LinearLayout.VERTICAL); //Setting that LinearLayout as Vertical

        for (int i = 0; i < n; i++)
        {
            LinearLayout row=new LinearLayout(this); //new Horizontal LinearLayout
            row.setOrientation(LinearLayout.HORIZONTAL); //keeping this layout's orientation horizontal
            row.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); //setting it's width and Height

            for(int j=0;j<n;j++)
            {
                Button myButton = new Button(this);
                myButton.setLayoutParams(new LayoutParams(50, LayoutParams.WRAP_CONTENT));
                myButton.setText("");
                myButton.setId(count);
                final int id_ = myButton.getId();
                row.addView(myButton);
                count++;
            }
            
            //Once Button Added to Horizontal Row add it back to Vertical Linearlayout
            layout.addView(row);

        }
    }

I have commented the lines so you can understand !

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

Comments

0

You can use FlowLayout to do what you want with your buttons. Just replace your LinearLayout with FlowLayout.

FlowLayout Library link

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.