“Hello World” for Dummies… – Android
In these tutorials, everything begins with Android Studio. It’s official IDE, developed by Google, for Android developers. Please download the latest version of Android Studio.
Important: I assume that you are familiar with Java programming 🙂
Ready? Let’s begin with creating a our first project and understanding our IDE.
This is the welcome screen of Android Studio. You can create a new project, open an existing project, pull an existing project from Git, SVN etc.
As a newbie, click “Start a new Android Studio Project”.
As you can see above, you need to give a name to application, select the target device and add a new activity. You need at least one activity to create an Android app. If you choose “Add No Activity”, you can add it later.
The picture below belongs to the hierarchy window. You can see all project files here. There are four main titles here:
manifest, java, res, gradle
“Hello World” for Dummies Android: Android Manifest
AndroidManifest: According to android.com’s own words, “Every application must have an AndroidManifest.xml
file (with precisely that name) in its root directory. The manifest file provides essential information about your app to the Android system, which the system must have before it can run any of the app’s code”. In English, your application requests for permissions such as connecting to the internet, reading/writing a file, getting the device location etc., or your application shows its activities, services, etc. to device in manifest.
Java: All Java classes are placed under this directory.
Res: This directory contains your app resources such as drawables, layouts, strings, styles, dimensions etc.
Gradle: It is a build system. Gradle takes source files of your application and and get them together to build APK(Android application) file. It also manages the libraries.
In our project we created recently, open “activity_main.xml” file. Just delete the line
1 |
android:text="Hello World!" |
from your TextView because we are going to write it from code. To recognise the existing views in layout, we need to define it in it’s Java class. So, give it an id.
1 |
android:id="@+id/textViewCustom" |
Now, open MainActivity.java file. For an activity, lifecycle begins with onCreate() method. It would be better to know lifecycles of Android. Please see Android Lifecycles page for more.
Define TextView in java.
1 |
TextView textView = (TextView) findViewById(R.id.textViewCustom); |
And set its text “HelloWorld!”.
1 |
textView.setText("HelloWorld!"); |
onCreate method should be like:
1 2 3 4 5 6 7 8 |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = (TextView) findViewById(R.id.textViewCustom); textView.setText("HelloWorld!"); } |
Now run it. If you have an Android device, you can test your app on it. If you don’t, not worries, just build a virtual device. When you press run button, you see available devices and a button at bottom named “Create New Virtual Device”.
Select your device and go!
©Coffee Break Codes – “Hello World” for Dummies… – Android