Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1.  protected void onCreate(Bundle savedInstanceState) {
  2.         super.onCreate(savedInstanceState);
  3.         setContentView(R.layout.activity_main);
  4.  
  5.         GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
  6.         gridLayout.setColumnCount(10);
  7.         gridLayout.setRowCount(10);
  8.     //create ship object for testing
  9.  
  10.         ship ship = new ship("destroyer",new Point[1]);
  11.  
  12.  
  13.         getWindowManager().getDefaultDisplay().getMetrics(new DisplayMetrics());
  14.  
  15.  
  16.         for(int row = 0 ;row < 10; row++){
  17.             for(int col = 0; col < 10; col++){
  18.                 //Create button and add too gridLayout container
  19.                 Button button = new Button(this); //"this" refers too class that contains handlers
  20.                 //Adds button to gridLayout
  21.                 gridLayout.addView(button);
  22.                 //Configure buttons after been added to gridlayout out with 10 pixels
  23.                 button.getLayoutParams().width = 110;
  24.                 button.getLayoutParams().height = 50;
  25.  
  26.                 Point pnt = new Point(row,col);
  27.                 button.setTag(pnt);
  28.  
  29.                 //Click listener for button
  30.                 button.setOnClickListener(new View.OnClickListener() {
  31.                     @Override
  32.                     public void onClick(View v) {
  33.                         Button button = (Button) v;
  34.                         Point pnt = (Point) button.getTag();
  35.                         //if point equals ship object loaded into points change button colour / text / ect...
  36.                         if(pnt.equals(0,1)){
  37.                             button.setText("Hit");
  38.                             button.setClickable(false);
  39.                             Toast.makeText(getApplicationContext(),("You did it!"),Toast.LENGTH_LONG).show();
  40.                         }
  41.                         // if point does not equal ship object loaded into points, change button colour / text/ ect...
  42.                         if(!pnt.equals(0,1)){
  43.                             button.setText("Miss!");
  44.                         }
  45.  
  46.                     }
  47.                 });
  48.  
  49.             }
  50.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement