// MovingBallActivity.kt 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. // 2021-01-14 Kotlin version made in 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.kt (this file) app/src/main/java/moving/ball/MovingBallView.kt 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. */ package moving.ball import android.app.Activity import android.graphics.Color import moving.ball.MovingBallView import android.os.Bundle import android.view.* import moving.ball.R import android.view.ContextMenu.ContextMenuInfo import android.widget.Button // Class Button, etc. class MovingBallActivity : Activity() { var moving_ball_view: MovingBallView? = null public override fun onCreate( savedInstanceState: Bundle? ) { 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 = findViewById(R.id.moving_ball_view) as MovingBallView // With the following call we get a reference to the Button // object that has been created based on the definition in main.xml. val color_button = findViewById( R.id.color_button ) as 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. fun left_button_clicked( view: View? ) { moving_ball_view!!.move_ball_left() } fun down_button_clicked( view: View? ) { moving_ball_view!!.move_ball_down() } fun up_button_clicked( view: View? ) { moving_ball_view!!.move_ball_up() } fun 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 fun onCreateOptionsMenu( menu: Menu ) : Boolean { super.onCreateOptionsMenu( menu ) val inflater = menuInflater 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. override fun onCreateContextMenu( menu: ContextMenu, v: View, menuInfo: ContextMenuInfo? ) { super.onCreateContextMenu(menu, v, menuInfo) val inflater = menuInflater 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 fun process_menu_selection( menu_item_id: Int ): Boolean { return when ( menu_item_id ) { R.id.red_color_item -> { moving_ball_view!!.set_ball_color( Color.RED ) true } R.id.white_color_item -> { moving_ball_view!!.set_ball_color( Color.WHITE ) true } R.id.yellow_color_item -> { moving_ball_view!!.set_ball_color( Color.YELLOW ) true } R.id.green_color_item -> { moving_ball_view!!.set_ball_color( Color.GREEN ) true } R.id.blue_color_item -> { moving_ball_view!!.set_ball_color( Color.BLUE ) true } R.id.magenta_color_item -> { moving_ball_view!!.set_ball_color( Color.MAGENTA ) true } R.id.cyan_color_item -> { moving_ball_view!!.set_ball_color( Color.CYAN ) true } R.id.gray_color_item -> { moving_ball_view!!.set_ball_color( Color.GRAY ) true } R.id.light_gray_color_item -> { moving_ball_view!!.set_ball_color( Color.LTGRAY ) true } else -> false } } // The following method is called when a selection in // the Options menu has been made. override fun onOptionsItemSelected( menu_item: MenuItem ) : Boolean { return if ( process_menu_selection( menu_item.itemId ) ) { true } else { super.onOptionsItemSelected( menu_item ) } } // The following method is called when a selection in // the context menu has been made. override fun onContextItemSelected( menu_item: MenuItem ) : Boolean { return if ( process_menu_selection( menu_item.itemId ) ) { true } else { super.onContextItemSelected(menu_item) } } }