Hello World with Android application

In the previous post, I showed you how to create a new Android project. I also showed you how to configure an Emulator to run Android applications. In this tutorial, we will go into more detail and we will edit the default text when creating a new project from “Hello World!” to “Hello from Huong Dan Kotlin” and “Hello from Khanh”!

When our application first starts up the project and runs, it will display the text “Hello World!” as follows:

As I said in the previous tutorial, the MainActivity class will take care of displaying the above interface and it uses the activity_main.xml file in the project’s res/layout folder to do this.

When you click on this file activity_main.xml, by default you will see the design interface displayed as follows:

You can switch to Code view (only displaying the content of the activity_main.xml file) or Split (just seeing the content of the activity_main.xml file and seeing how the interface will be displayed at the same time).

As I said in the previous tutorial, Android Studio allows us to drag and drop to design the interface. You can drag and drop components in the Palette to create an interface for your application. Android Studio will automatically generate the content of the activity_main.xml file based on what we drag and drop, configuring the interface components.

The content of the current activity_main.xml file is as follows:

In addition to the root tag <androidx.constraintlayout.widget.ConstraintLayout> that defines the layout for the application, as you can see, we also have a <TextView> tag that defines the content of the text line “Hello World!”.

In short, about layout in Android application, that is how Android application components will be displayed. Imagine, an Android application can run on many different devices, and each device has a different size. So how will the display of the position of each component in an interface be, and how will the distance between the components be? Which layout you use will determine the display for these components! Android SDK defines some default layouts like Linear Layout, Relative Layout, or Constraint Layout that we are using for our example in this tutorial. You should learn carefully the types of layout to use correctly for your application!

TextView is one of the components provided by Android SDK, it helps us to display text in Android application. In addition to TextView, we also have Button to display the button, EditText to enter text, …

Depending on the component, we can define the length, width, color, and some other properties. For the TextView component that we are using, as you can see, there are some properties related to the layout constraint with the parent. You can change the text displayed for this component using the android:text attribute. As I said above, I will change the displayed text to “Hello from Huong Dan Kotlin” guys!

Run the application again, you will see the following results:

To work with a component in an Android application, in addition to designing the interface for it in the application’s Activity XML file, we can also use the code to add events and behaviors according to our needs. But before we can do this, for each component, we need to define the ID for that component. In the code, we will access that component using this ID value.

For example, for the TextView component in our example, we could define an ID using the android:id attribute like so:

In code, we can access this TextView component using the following line of code:

In the Android SDK, the R class will take care of all the information related to the application’s resources. From images, sounds to application components, we can all access them through this R class. For components, we use their IDs to access them through the R class.

Once you have access to the component, you can do anything with that component. For example, I can change the displayed text to “Hello from Khanh” as follows:

The result when I run the application again will be as follows:

Leave a Reply

Your email address will not be published. Required fields are marked *