We‟ll develop a simple strobe light app in this project. We‟ll go through accessing the button in Java code, getting the screen background color, setting this color and sensing button taps to change the background color.
Our aim is to develop an Android app where the background color of the app is varied as in a red/blue strobe light. The background color of the app will change from red to blue or vice versa each time we click a button located in the middle of the screen. This is a very simple app but will teach the basics steps of visual programming.
Watch the video below:
Creating a New Android Project
Firstly, please select “Create a new project”. If Android Studio is already running select File > New > New Project from the top menu
I named the app as “Lighthead app” as shown in Figure Below, but you can give any name you‟d like to.
Naming the app
Then, I selected the app to be compatible with phones and tablets having Android 4.0.3 (Ice Cream Sandwich) or later:
Selecting app compatibility
We‟ll have a simple screen therefore “Empty Activity” does the job in the next dialog:
Finally, leave the name of the activity as “MainActivity” and then click “Finish” to create the project files:
After the project is successfully created, the default view of the Android Studio will appear in which the middle pane will show the “MainActivity.java” file as shown below:
Default view in Android Studio
Developing the User Interface
Let‟s open the user interface layout file activity_main.xml where we will place the button on the screen. As we can see from the figure above, the left pane shows the folders and files of our project. Make sure that the view type is Android and select the folders res > layout and then double-click on the file activity_main.xml there as shown in Figure 1.
When the activity_main.xml file is opened, the layout it contains will be shown in the middle pane as shown in Figure 2. This file and other xml files contain the layout information of an Android app in Android Studio. In fact, xml files are not only used in Android app development but also in other areas of computing. xml files are good to express the relations among different entities in a hierarchical way therefore is a good choice to use in layout design. xml files are text files but Android Studio interprets them as layouts as long as they are in the correct format. We can also view the text file representation of activity_main.xml in Android Studio by selecting the Text tab as indicated by the arrow in Figure 2
Figure 1: Finding activity_main.xml file in project explorer
Figure 2. Viewing activity_main in Android Studio































