Android Game Development – Create the Project in Android Studio
To begin developing apps, you need to create a project in Android Studio. Mostly, we design in XML and write code in Java or Kotlin. If you haven’t installed Android Studio yet, go ahead and install Android Studio from this link before you begin. I am using version 3.1.4 at the time of recording. In this lecture, I will cover the basics of designing XML layout. I am going to add an ImageButton, a TextView and change some basic properties. Then, I will show you how to get the Click event on that ImageButton. By the end, you’ll be able to design this XML layout and get the Click event on ImageButton. Start Android Studio. In the Welcome to Android Studio window, click Start a new Android Studio project. In the Create Android Project screen, write Flappy Bird for the Application name. For Company domain I am going to use “sandipbhattacharya.com” which is my website. So, the package appears in reverse order that is, “com.sandipbhattacharya.flappybird”. Click the three dots to change the project location. I am going to save the project on my Desktop. Just below the package name, you can see two checkboxes for including support for C++ and Kotlin. As we’re using Java for this project, we can leave them unchecked. Click Next. In the Target Android Devices screen, keep the default values and click Next. In the Add an Activity to Mobile screen, select Empty Activity and click Next. In the Configure Activity screen, keep the default values and click Finish. Wait for Android Studio to configure and build your empty project. You need a play button in the MainActivity layout. When pressed, the game should start. To do that, copy and paste the main_background and play button images into the drawable folder. Go to the layout folder under res. Double-click activity_main.xml. Change the Layout to RelativeLayout. Change the background to, Next, we can delete the TextView as we don’t need that anymore. Go to design tab. Select the TextView and hit Delete key. From the palette, click Buttons and drag and drop an ImageButton onto the center of the layout. This causes the Resources window to pop up. Click “Project”. Select the image “play”. Click OK. The Android ImageButton component is an ImageView subclass which displays a button with an image instead of text and can be pressed or clicked by the user. Go to text tab and change this piece of code to. Go to design tab. From the palette click “Common”. Next, drag and drop a TextView below the ImageButton. The Android TextView component is a View subclass which is capable of showing text. Click text tab. Change the text to “Play”. Delete unnecessary codes. Change the textSize to 34 sp. SP stands for Scale-independent pixels. It’s a measurement unit and its scaled according to the user’s preferred text size, so you should use this when defining text size. For layout sizes, you should use dp or density-independent pixel. Position it below the imagebutton: Give a margin-top of 10dp. Change the color. Now, let’s get the click event on ImageButton. The purpose of this button is to start the game when it’s tapped. But for now, let’s identify the click event. There are many ways to accomplish this. One technique is to write a method name in the onClick property and implement the method in MainActivity.java. Write. Next, go to MainActivity.java. Define startGame by writing. This method takes a View parameter even if we don’t need it. Just import View by pressing ALT and ENTER, if it’s not imported automatically. Right-click on the package and create a new Java Class called GameActivity. Select Activity as superclass. Click OK. Inside this class, right-click, Generate, Override methods and select onCreate. We’ll get back to this class later. Open MainActivity.java and go to the definition of startGame(). Use a log statement to confirm that we’re getting the click event. To write log statements, you can use the Log class with the following methods: Log.d(), Log.i() etc. The two parameters are tag and message respectively. So, write. Import Log by pressing ALT and ENTER if it’s not imported automatically. Click the green Run button to run the app in Android Emulator. Click Logcat. Now, click on the ImageButton and confirm that we are getting the log information. So to recap – I have covered a little bit about designing a
basic XML layout. You also know how to get the Click event on ImageButton and write a Log statement. Next, I’ll give you a high-level
overview of our project. See you in the next video.