Advertisement
Guest User

Untitled

a guest
May 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. package com.example.colorgame20;
  2.  
  3. import android.content.Context;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.view.Gravity;
  6. import android.widget.Button;
  7. import android.widget.GridLayout;
  8. import android.widget.LinearLayout;
  9.  
  10. public class Buttons extends AppCompatActivity {
  11.  
  12.  
  13.     Context context;
  14.  
  15.     public Buttons(Context context) {
  16.         this.context = context;
  17.     }
  18.  
  19.  
  20.     public void createButtonsField() {
  21.  
  22.         GridLayout gridLayout = (GridLayout) findViewById(R.id.gameLayout);
  23.  
  24.         int column = Levels.columnQuantityArray[Levels.levelNumber];
  25.         int row = Levels.rowQuantityArray[Levels.levelNumber];
  26.         int total = column*row;
  27.         int height = 1000/column;
  28.         int width = height;
  29.  
  30.         gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
  31.         gridLayout.setColumnCount(column);
  32.         gridLayout.setRowCount(row+1);
  33.  
  34.         Button btn;
  35.  
  36.         for(int i = 0, c = 0, r = 0; i < total; i++, c++)
  37.         {
  38.             if(c == column) {
  39.                 c = 0;
  40.                 r++;
  41.             }
  42.  
  43.             btn  = new Button(context);
  44.             btn.setGravity(Gravity.CENTER);
  45.             btn.setHeight(height);
  46.             btn.setWidth(width);
  47.             int id = Integer.parseInt(r+""+c);
  48.             btn.setId(id);
  49.             GameActivity.BUTTON_IDS.add(id);
  50.  
  51.             gridLayout.addView(btn, i);
  52.  
  53.             GridLayout.LayoutParams param =new GridLayout.LayoutParams();
  54.             param.height = height;
  55.             param.width = width;
  56.             param.rightMargin = 10;
  57.             param.topMargin = 10;
  58.             param.setGravity(Gravity.CENTER);
  59.             param.columnSpec = GridLayout.spec(c);
  60.  
  61.             btn.setLayoutParams (param);
  62.         }
  63.  
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement