After successfully setting up an Android project, all the default files are created with pre-written code. Let’s examine this default code and run the created default app.
On the left panel of the Android Studio window, you’ll find all the files included in the app. Under the java
folder, the first subfolder contains the Java file for your project.
For every activity, a .java
file and a .xml
file are created. In this case, for MainActivity
, both MainActivity.java
and activity_main.xml
are generated. The Java file contains the default code for the app, including an activity that extends the AppCompatActivity
class.
The res
folder contains the layout
subfolder, which includes the XML files for the project. You can find the activity_main.xml
file under the layout folder. This XML file corresponds to MainActivity
and contains various tags similar to HTML.
The onCreate
function in MainActivity.java
overrides a function from AppCompatActivity
. The onCreate(Bundle)
method is where you initialize your activity. When the activity is first started, both onCreate()
methods are called. However, after the initial start, the onCreate()
method of the application is not called for subsequent runs.
Examining activity_main.xml
, you will see several tags. The first tag ensures the version, and the second tag is usually the Layout tag. While there are various types of layouts, the default is often RelativeLayout
, which arranges widgets relative to the screen size.
By default, there is a TextView
widget. This “TextView” is a text field that displays the specified text. It includes various attributes such as layout_width
and layout_height
, which define the width and height of the widget. The attribute wrap_content
limits the width or height to the content of the text. The text
attribute takes a string in quotations (i.e., โ โ), which is displayed on the screen.
To run the default app, click the “Run” option on the Toolbar at the top.
You can choose to run the app on an emulator or connect your phone, which will be listed under Connected Devices. Ensure that Developer Options and USB debugging mode are enabled on your phone.
Click “OK” once everything is set up. Keep in mind that emulators consume a lot of RAM. Generally, 4GB is a decent size, but having more RAM will improve emulator performance.