Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
- gridLayout.setColumnCount(10);
- gridLayout.setRowCount(10);
- //create ship object for testing
- ship ship = new ship("destroyer",new Point[1]);
- getWindowManager().getDefaultDisplay().getMetrics(new DisplayMetrics());
- for(int row = 0 ;row < 10; row++){
- for(int col = 0; col < 10; col++){
- //Create button and add too gridLayout container
- Button button = new Button(this); //"this" refers too class that contains handlers
- //Adds button to gridLayout
- gridLayout.addView(button);
- //Configure buttons after been added to gridlayout out with 10 pixels
- button.getLayoutParams().width = 110;
- button.getLayoutParams().height = 50;
- Point pnt = new Point(row,col);
- button.setTag(pnt);
- //Click listener for button
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Button button = (Button) v;
- Point pnt = (Point) button.getTag();
- //if point equals ship object loaded into points change button colour / text / ect...
- if(pnt.equals(0,1)){
- button.setText("Hit");
- button.setClickable(false);
- Toast.makeText(getApplicationContext(),("You did it!"),Toast.LENGTH_LONG).show();
- }
- // if point does not equal ship object loaded into points, change button colour / text/ ect...
- if(!pnt.equals(0,1)){
- button.setText("Miss!");
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement