Search code examples
androidspacingandroid-gridlayout

How to adjust Android gridlayout spacing from Java program


I have the following code :

    LinearLayout symbolsTableLayout = new LinearLayout(context);
    symbolsTableLayout.setId(View.generateViewId());
    symbolsTableLayout.setBackgroundColor(Color.rgb(198,218,250));
    symbolsTableLayout.setHorizontalGravity(Gravity.CENTER);
    symbolsTableLayout.setVerticalGravity(Gravity.CENTER);

    addView(symbolsTableLayout, LinearLayout.LayoutParams.MATCH_PARENT,460);

    GridLayout tableLayout = new GridLayout(context);
    tableLayout.setColumnCount(col_count);
    tableLayout.setRowCount(row_count);

    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.leftMargin = 0;
    layoutParams.rightMargin = 0;
    layoutParams.topMargin = 0;
    layoutParams.bottomMargin = 0;
    tableLayout.setLayoutParams(layoutParams);

    tableLayout.setBackgroundColor(Color.rgb(198,128,230));    // Color.rgb(198,128,230)   pink

    symbolsTableLayout.addView(tableLayout, 1100,470);

    symbolButton = new Button[row_count][col_count];
    for (int row = 0; row < row_count; row++)
      for (int col = 0; col < col_count; col++)
      {
        symbolButton[row][col] = new Button(context);
        symbolButton[row][col].setText(row*col_count+col+1+"");
        symbolButton[row][col].setTypeface(Typeface.MONOSPACE);
        symbolButton[row][col].setAllCaps(false);
        symbolButton[row][col].setTextSize(4);
        symbolButton[row][col].setOnClickListener(Symbol_Button_Listener);

        GridLayout.LayoutParams layoutParam=new GridLayout.LayoutParams();
        layoutParam.setMargins(0,0,0,0);
        layoutParam.width=50;
        layoutParam.height=58;

        GridLayout.LayoutParams param = new GridLayout.LayoutParams();
        param.width = 0;
        param.height = 0;

        param.setMargins(0, 0, 0, 0);
        param.columnSpec = GridLayout.spec(col, GridLayout.FILL, 0.5f);
        param.rowSpec = GridLayout.spec(row, GridLayout.FILL, 0.5f);

        symbolButton[row][col].setLayoutParams(param);
//        Symbol_Button[row][col].setLayoutParams(layoutParam);
//        Table_Layout.addView(Symbol_Button[row][col], 44, 56);
        tableLayout.addView(symbolButton[row][col], 50, 58);
      }

It looks like this :

enter image description here

It only shows 22 x 8 buttons, I want to show 25 buttons in each row, and make the space between all buttons smaller, how to do it ?


Solution

  • It can be done like this :

        LinearLayout symbolsTableLayout = new LinearLayout(context);
        symbolsTableLayout.setId(View.generateViewId());
        symbolsTableLayout.setBackgroundColor(Color.rgb(198,218,250));
        symbolsTableLayout.setHorizontalGravity(Gravity.CENTER);
        symbolsTableLayout.setVerticalGravity(Gravity.CENTER);
    
        addView(symbolsTableLayout, LinearLayout.LayoutParams.MATCH_PARENT,460);
    
        GridLayout tableLayout = new GridLayout(context);
        tableLayout.setColumnCount(col_count);
        tableLayout.setRowCount(row_count);
    //    tableLayout.setBackgroundColor(Color.rgb(198,128,230));    // Color.rgb(198,128,230)   pink
    
        symbolsTableLayout.addView(tableLayout, 1100,400);
    
        symbolButton = new Button[row_count][col_count];
        for (int row = 0; row < row_count; row++)
          for (int col = 0; col < col_count; col++)
          {
            symbolButton[row][col] = new Button(context);
            symbolButton[row][col].setText(row*col_count+col+1+"");
            symbolButton[row][col].setTypeface(Typeface.MONOSPACE);
            symbolButton[row][col].setAllCaps(false);
            symbolButton[row][col].setTextSize(6);
            symbolButton[row][col].setOnClickListener(Symbol_Button_Listener);
    
            GradientDrawable drawable = new GradientDrawable();
            drawable.setShape(GradientDrawable.RECTANGLE);
            drawable.setCornerRadii(new float[]{8, 8, 8, 8, 8, 8, 8, 8});
            drawable.setStroke(3, Color.rgb(153, 187, 255));
            drawable.setColor(Color.rgb(214, 215, 215));
            symbolButton[row][col].setBackground(drawable);
    
            tableLayout.addView(symbolButton[row][col], 44, 50);
          }
    

    Looks like this :

    enter image description here