// MovingBallActivity.java by Kari Laitinen, Miika Santala // http://www.naturalprogramming.com/ // 2010-10-08 File received from Miika Santala. // 2012-02-15 First working version. // 2015-03-14 This app was adjusted for Android Studio. /* This file contains the main activity class of an Android application that shows a ball on the screen. There are buttons with which the user can move the ball, or change the color of the ball. This application provides both an Options menu and a Context menu for changing the ball color. The Options menu may not work in new Android devices as the Options key is no longer required in later Android versions. In addition to the files that are created automatically when the Android project is created, this application needs these files: app/src/main/java/moving/ball/MovingBallActivity.java (this file) app/src/main/java/moving/ball/MovingBallView.java app/src/main/res/layout/activity_moving_ball.xml app/src/main/res/menu/color_selection_menu.xml app/src/main/res/values/strings.xml You should put these files to the appropriate folders after you create an Android Studio project whose main activity is MovingBallActivity. More notes at the end of this file. */ package moving.ball ; import android.app.Activity; import android.app.AlertDialog; import android.util.AttributeSet; import android.os.Bundle; import android.graphics.*; import android.view.*; import android.content.* ; import android.widget.* ; // Class Button, etc. import android.widget.AdapterView.AdapterContextMenuInfo ; public class MovingBallActivity extends Activity { MovingBallView moving_ball_view; public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ) ; // The following call specifies that activity_moving_ball.xml is the file // that defines the UI of this Activity. setContentView( R.layout.activity_moving_ball ) ; moving_ball_view = (MovingBallView) findViewById( R.id.moving_ball_view ) ; // With the following call we get a reference to the Button // object that has been created based on the definition in main.xml. Button color_button = (Button) findViewById( R.id.color_button ) ; // with the following line we specify that the 'floating' // context menu must be shown when the 'color' button is long-pressed. registerForContextMenu( color_button ) ; } // The following four methods react to the pressings of the // buttons with which the ball can be moved. // Note that we do not need references to the Button objects in // this program as activity_moving_ball.xml specifies that the following // methods are called when the corresponding buttons are pressed. public void left_button_clicked( View view ) { moving_ball_view.move_ball_left() ; } public void down_button_clicked( View view ) { moving_ball_view.move_ball_down() ; } public void up_button_clicked( View view ) { moving_ball_view.move_ball_up() ; } public void right_button_clicked( View view ) { moving_ball_view.move_ball_right() ; } // The following method is called when the Options // menu needs to be created. The definitions in // res/menu/color_selection_menu.xml are used to // create the menu. @Override public boolean onCreateOptionsMenu( Menu menu ) { super.onCreateOptionsMenu( menu ) ; MenuInflater inflater = getMenuInflater() ; inflater.inflate( R.menu.color_selection_menu, menu ) ; return true; } // In this program we have two menus which are specified with // the same XML file. // The following method is called when the 'floating' // context menu needs to be created. public void onCreateContextMenu( ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo ) { super.onCreateContextMenu( menu, v, menuInfo ) ; MenuInflater inflater = getMenuInflater() ; inflater.inflate( R.menu.color_selection_menu, menu ) ; } // The following method is used to process selections from // both menus, the options menu and the context menu. protected boolean process_menu_selection( int menu_item_id ) { switch ( menu_item_id ) { case R.id.red_color_item: moving_ball_view.set_ball_color( Color.RED ) ; return true; case R.id.white_color_item: moving_ball_view.set_ball_color( Color.WHITE ) ; return true; case R.id.yellow_color_item: moving_ball_view.set_ball_color( Color.YELLOW ) ; return true; case R.id.green_color_item: moving_ball_view.set_ball_color( Color.GREEN ) ; return true; case R.id.blue_color_item: moving_ball_view.set_ball_color( Color.BLUE ) ; return true; case R.id.magenta_color_item: moving_ball_view.set_ball_color( Color.MAGENTA ) ; return true; case R.id.cyan_color_item: moving_ball_view.set_ball_color( Color.CYAN ) ; return true; case R.id.gray_color_item: moving_ball_view.set_ball_color( Color.GRAY ) ; return true; case R.id.light_gray_color_item: moving_ball_view.set_ball_color( Color.LTGRAY ) ; return true; default: return false ; } } // The following method is called when a selection in // the Options menu has been made. public boolean onOptionsItemSelected( MenuItem menu_item ) { if ( process_menu_selection( menu_item.getItemId() ) ) { return true ; } else { return super.onOptionsItemSelected( menu_item ) ; } } // The following method is called when a selection in // the context menu has been made. public boolean onContextItemSelected( MenuItem menu_item ) { if ( process_menu_selection( menu_item.getItemId() ) ) { return true ; } else { return super.onContextItemSelected( menu_item ) ; } } } /* NOTES: In this program, the connection between a Button and the method that handles the pressing of the button is done with the following in the XML file: android:onClick="left_button_clicked" The alternative would be to write a listener for the button, in the following way. left_button.setOnClickListener(new View.OnClickListener() { public void onClick( View view ) { // Perform action on click } }) ; ACKNOWLEDGMENTS: The first version of this Android program was developed by Miika Santala. Many thanks to Miika! */