Creating Android Application
Creating Android Application
Once we are done with Android Studio Installation, open android studio and that
will be like as shown below.
Here we're going to choose the New Project option because we haven’t created any other
project and we need to create a new one. So, we will select the New Project from the given
options.
However, we can choose Import Project if we’d like to import a project from any other way,
for example, Eclipse project into Android Studio. Android Studio will convert the Eclipse
project to an Android Studio project, adding the necessary configuration files for us.
If we select Open Project from the list of options, we can open projects created with either
Android Studio or IntelliJ IDEA.
Check out from Version Control, we can check out a copy of a project that's under version control.
This is a great way to quickly get up to speed with an existing project.
To get us started, choose New Project from the list of options. This will show us a
list of options to configure our new project.
As we click on “New Project” from the above option, then the next screen will be
open like this, where we have to mention our Project’s name, Company
domain and Project location (we called it the main path where this application will
be saved) because the Package name will be created automatically as we create the project in
Android Studio.
After entering all the details if we click on the “Next” button another screen will
appear where we have select the different platforms and SDK targets like as shown
below based on our requirements.
Here we need to select the type of Platform which we are going to use for the
Application development like if we select “ Phone and Tablet”, then it will show it’s
different API and SDK version and similar to others.
If we choose “Wear”, then it will show it’s API and SDK versions like as shown
below.
In case if we choose “TV”, then it will show it’s API and SDK versions like as shown below.
Wear: We use this option for Android Watches which we can wear to our hand and use the same
functionality as we do with the Android devices. You can call, set the alarm, capture images, and
many more things easily.
TV: We use this option for SmartIPTV which is very common these days. We can see our favorite
channels like we see in our Home Televisions and make the changes in the channel easily.
Here we are going to implement an app for phones and tablets, so we selected
a Phone and Tablet option and click “Next” and it will install required components
like as shown below.
Now click Next to select the particular Activity for our requirement.
If we will select the “Empty Activity”, then it will show the empty activity in
our layout.
In case if we choose other options, then it will show the activity which we
have chosen.
Here we are selecting Empty Activity like as shown below.
After choosing the “Activity” for our application, then click on the “Next” button and it will
take you to the next screen like as shown below.
Here we can see that the Activity i.e. EmptyActivity which we selected in the
previous section and the java file name i.e. “ MainActivity”. Now we are ready for
the final step, just click on the “Finish” button and it will take you to the Main page
where we have to do the coding and create new layouts over there.
After clicking Finish, we will be presented with Android Studio's user interface
with the project explorer on the left and the workspace on the right like as shown
below.
Coding the Hello World App
Now that you have a general view of the project structure, let's describe the most
critical component files that constitute the hello world application.
Each Activity represents a screen of the Android app's user interface. Each Activity
has a Java (or Kotlin) implementation file and an XML layout file.
Below is the default Java code generated by the application for the main activity.
01 package com.example.helloworldapplication;
02
03
import androidx.appcompat.app.AppCompatActivity;
04
05
import android.os.Bundle;
06
07 public class MainActivity extends AppCompatActivity {
08
09 @Override
11 super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
12
}
13
}
14
You don't need to understand this code fully. A lot of it is boilerplate and will be the
same for any app. The code defines a MainActivity class and, along with
the onCreate method, defines what happens when the Activity is created. In this
case, it simply initializes the view of the Activity with the layout from
the activity_main layout file.
XML files are used for layouts. The main Activity layout XML file is found in
the project's /app/src/main/res/layout directory. Layout files are named after what
they represent. For example, the Hello World application has one layout, which is
the activity_main.xml named after the main Activity.
01 Activity.xml
<androidx.constraintlayout.widget.ConstraintLayout
03
xmlns:android="https://schemas.android.com/apk/res/android"
04
xmlns:app="http://schemas.android.com/apk/res-auto"
05
xmlns:tools="http://schemas.android.com/tools"
06
android:layout_width="match_parent"
07
android:layout_height="match_parent"
08 tools:context=".MainActivity">
09
10 <TextView
11 android:layout_width="wrap_content"
12 android:layout_height="wrap_content"
android:text="Hello World!"
13
app:layout_constraintBottom_toBottomOf="parent"
14
app:layout_constraintLeft_toLeftOf="parent"
15
app:layout_constraintRight_toRightOf="parent"
16
app:layout_constraintTop_toTopOf="parent" />
17
18
</androidx.constraintlayout.widget.ConstraintLayout>
19
As you can see, we don't need to change much to complete our Hello World app, but
we'll make a small change so that the text stands out better—we'll change the text
colour and font size.
01 <TextView
02 android:layout_width="wrap_content"
android:layout_height="wrap_content"
03
android:text="Hello World!"
04
05 android:textSize="50dp"
06 android:textColor="#800000"
07 app:layout_constraintBottom_toBottomOf="parent"
08 app:layout_constraintLeft_toLeftOf="parent"
09 app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
10
1 STRINGS
2
3 <resources>
5 </resources>
In Android Studio, navigate to the top menu and select Run 'app'. Android Studio will
show a dialog where you can choose which device to run your Android app on.
Choose your connected device and click the OK button.
The Hello World application should now be running on your phone. From here, you
can make modify your app to whatever you want and add more features.