4

My First App: How to Create Your First Android App Step by Step

 10 months ago
source link: https://code.tutsplus.com/tutorials/creating-your-first-android-app--cms-34497
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

My First App: How to Create Your First Android App Step by Step

To create a native Android app, one that can directly use all the features and functionality available on an Android phone or tablet, you need to use the Android platform's Java API framework. This is the API that allows you to perform common tasks such as drawing text, shapes, and colors on the screen, playing sounds or videos, and interacting with a device's hardware sensors. Over the years, the Android API framework has evolved to become more stable, intuitive, and concise. As a result, being an Android developer today is easier than ever—even more so if you use Android Studio, the official tool for working with the framework.

In this tutorial, I'll show you how to create your first Android app. While doing so, I'll also introduce you to important Android-specific concepts such as views, layouts, and activities.

We'll be starting from scratch to create a very simple app in this tutorial. If you prefer writing less code or need to develop your app as quickly as possible, however, consider using one of the native Android app templates available on CodeCanyon.

Using an app template, you can have a polished, ready-to-publish app in just a matter of hours. You can learn how to use an Android app template by referring to the following tutorial:

Prerequisites

To be able to follow along, you'll need:

  • the latest version of Android Studio
  • a device or emulator running Android Marshmallow or higher

If you don't have Android Studio, do refer to the following tutorial to learn how to install and configure it:

1. Create a New Project

You'll need an Android Studio project to design, develop, and build your app. So launch Android Studio and click on the Start a new Android Studio project button.

On the next screen, choose Add No Activity because we don't want to use any of the templates offered by Android Studio. Then press Next to proceed.

q1.png

q1.png

q1.png

You'll now see a form where you can enter important details about your app, such as its name and package name. The name is, of course, the name your users will see on their phones when they install your app.

The package name, on the other hand, is a unique identifier for your app on Google Play. You must follow the Java package naming conventions while specifying it. For example, if your app's name is MyFirstApp and you work for an organization whose website address is example.com, the package name would ideally be "com.example.myfirstapp".

Next, you must decide the programming language you want to use while coding the app. For now, select Java and press Finish.

Screenshot%20from%202020-01-11%2018-04-59.png

Screenshot%20from%202020-01-11%2018-04-59.png

Screenshot%20from%202020-01-11%2018-04-59.png

Kotlin has been the preferred language for developing Android apps for a while now. So, you might want to use Kotlin for this project. In that case, you should select Kotlin as the language as shown below. You can also see that changing the app name to MyClockApp updates the package name as well.

Newer version of Android Studio also shows you how many devices will be capable of running your app for a particular minimum API level support.

Using Kotlin as Language

Using Kotlin as Language

Using Kotlin as Language

Android Studio will now take a minute or two to generate and configure the project.

2. Create an Activity

An activity is one of the most important components of an Android app. It is what allows you to create and display a user interface to your users. An app can have one or more activities, each allowing the user to perform an action. For example, an email client app can have three activities: one for the user to sign up, one to sign in, and one to compose an email.

To keep this tutorial simple, we'll be creating an app with just one activity. To create the activity, in the Project panel of Android Studio, right-click on app and select New > Activity > Empty Activity.

In the dialog that pops up, type in MainActivity as the name of the activity, check the Launcher Activity option, and press Finish.

Checking the Launcher Activity option is important because it is what allows your users to open the activity using an Android launcher. As such, a launcher activity serves as an entry point to your app.

Screenshot%20from%202020-01-11%2019-09-44.png

Screenshot%20from%202020-01-11%2019-09-44.png

Screenshot%20from%202020-01-11%2019-09-44.png

If you created the project to use Kotlin as your programming language, it makes sense to use Kotlin while creating a New Android Activity as well.

New Android Activity

New Android Activity

New Android Activity

3. Create a Layout

Each activity usually has at least one layout associated with it. When you created your activity in the previous step, you also generated an empty layout for it. To take a look at it, open the activity_main.xml file.

An activity's layout primarily consists of views and view groups. A view, sometimes referred to as a widget, is an individual component of your user interface. Buttons, text fields, labels, and progress bars are common examples of views. A view group is a component that can serve as a container for views. Usually, view groups also help you position and set the dimensions of your views.

ConstraintLayout is one of the most powerful and flexible view groups available today. By default, it is the root node of your activity's layout XML file. It looks like this:

<?xml version="1.0" encoding="utf-8"?>
2
3
<androidx.constraintlayout.widget.ConstraintLayout
4
    xmlns:android="https://schemas.android.com/apk/res/android"
5
    xmlns:app="https://schemas.android.com/apk/res-auto"
6
    xmlns:tools="http://schemas.android.com/tools"
7
    android:layout_width="match_parent"
8
    android:layout_height="match_parent"
9
    tools:context=".MainActivity">  
    <!-- More code here -->  
</androidx.constraintlayout.widget.ConstraintLayout>

We'll be creating a simple clock app in this tutorial. In addition to the local time, it will be able to show the current time in two different countries: India and Germany.

To allow the user to choose the country they're interested in, our layout will have two Button views, one for Germany, and one for India. And to actually show the time, our layout will have a TextClock view.

Accordingly, add the following code inside the ConstraintLayout:

<TextClock
2
    android:id="@+id/my_clock"
3
    android:layout_width="wrap_content"
4
    android:layout_height="wrap_content"
5
    app:layout_constraintBottom_toBottomOf="parent"
6
    app:layout_constraintTop_toTopOf="parent"
7
    app:layout_constraintLeft_toLeftOf="parent"
8
    app:layout_constraintRight_toRightOf="parent"
9
    android:format12Hour="h:mm:ss a"
    android:textSize="32sp"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    android:text="Time in Germany"
    android:onClick="onClickGermany"
    android:id="@+id/germany_button"/>
20
<Button
21
    android:layout_width="match_parent"
22
    android:layout_height="wrap_content"
23
    app:layout_constraintBottom_toTopOf="@id/germany_button"
24
    android:text="Time in India"
25
    android:onClick="onClickIndia"
26
    android:id="@+id/india_button"/>

Note that each view must have the layout_width and layout_height properties. They decide how large the view is. Other properties such as layout_constraintBottom_toBottomOf and layout_constraintLeft_toLeftOf are necessary to position the view. With the above code, the TextClock view will be placed at the center of the screen, and both the Button views towards the bottom of the screen.

By default, the TextClock view only shows the hours and minutes. The format12Hour property, however, allows you to change that. In the above code, its value is set to h:mm:ss a. This tells the TextClock view that it should display the hours, minutes, seconds, and also an AM/PM suffix.

Also note that each Button view has an onClick property. This property is used to assign click event handlers to the buttons.

The handlers don't exist yet, but you can ask Android Studio to generate them for you. To do so, hover over the name of the handler until you see a red light bulb appear beside it. Then click on the light bulb and select the second option, the one with the yellow light bulb.

Screenshot%20from%202020-01-11%2019-45-56.png

Screenshot%20from%202020-01-11%2019-45-56.png

Screenshot%20from%202020-01-11%2019-45-56.png

At this point, you can try pressing Shift-F10 to run the app. If there are no errors in your XML code, you should see something like this on your phone or emulator:

device-2020-01-11-194845.png

device-2020-01-11-194845.png

device-2020-01-11-194845.png

Although the buttons don't work yet, the TextClock view should show the local time, updating itself every second.

Advertisement

4. Implement Event Handlers

When you generated event handlers for the two buttons, Android Studio added two methods to your activity's Java file, MainActivity.java. If you open it, you should find the following code in it:

public void onClickGermany(View view) { 
2
3
}
4
5
public void onClickIndia(View view) {
6
7
}

Inside the event handlers, all we need to do is change the time zone of the TextClock view. But how do you reference a view that's in your layout XML file from inside your Java file? Well, you just use the findViewById() method.

Once you have a reference to the TextClock view, you can call its setTimeZone() method to change its time zone. So add the following code inside the onClickGermany() method:

TextClock clock = findViewById(R.id.my_clock);
2
clock.setTimeZone("Europe/Berlin");

Similarly, add the following code inside the onClickIndia() method:

TextClock clock = findViewById(R.id.my_clock);
2
clock.setTimeZone("Asia/Kolkata");

If you're wondering what R is, it's an auto-generated class that contains, among other things, the IDs of all the views you have in your layouts. The findViewById() method expects you to use this class while passing an ID to it.

The same two methods written in Kotlin will look like this:

fun onClickGermany(view: View) {
2
    val clock = findViewById<TextClock>(R.id.my_clock)
3
    clock.timeZone = "Europe/Berlin"
4
}
5
6
fun onClickIndia(view: View) {
7
    val clock = findViewById<TextClock>(R.id.my_clock)
8
    clock.timeZone = "Asia/Kolkata"
9
}

The keyword fun precedes the method name. You might have also observed that the findViewById() method here expects the type of view inside the angle brackets. Depending on your preference, the same line could also be written as follows:

val clock: TextClock = findViewById(R.id.my_clock)

At this point, you can press Shift-F10 again to re-run the app. You should now be able to click on the buttons to change the clock's time zone.

Conclusion

You just created your first fully functional, native app for Android! I encourage you to make a few changes to it. For instance, you could try using other time formats or time zones. You could also try changing the positions of the buttons and the clock view.

There are dozens of views and view groups you can use to create your apps. Do refer to the official documentation to learn about them.

Android App Templates and UI Kits From CodeCanyon

You may have noticed that our app looks very plain and simple. That's because we're using the default theme, without applying any styles to our views. CodeCanyon is full of Android UI kits that offer beautiful, hand-crafted styles you can apply to your views.

The kits generally also have several custom views and layouts. You can refer to the following articles to learn more about them:

  • top_view_of_designers_creative_app_interface_for_u_293RUSH.jpgtop_view_of_designers_creative_app_interface_for_u_293RUSH.jpgtop_view_of_designers_creative_app_interface_for_u_293RUSH.jpg
    20+ Best Android App Templates for 2023
    franc_LucasTutspluspic.jpgfranc_LucasTutspluspic.jpgfranc_LucasTutspluspic.jpg
    Franc Lucas
    27 Apr 2023
  • discussing_mobile_app_LWV3XU2.jpgdiscussing_mobile_app_LWV3XU2.jpgdiscussing_mobile_app_LWV3XU2.jpg
    10 Best Multi-Purpose Android App Templates
    NB.jpgNB.jpgNB.jpg
    Nona Blackman
    23 May 2022
  • d.jpgd.jpgd.jpg
    Jump-Start Your Android App UI With a Material Design Template
    picGrid.jpgpicGrid.jpgpicGrid.jpg
    Ashraff Hathibelagal
    21 Sep 2019
  • top_view_of_designers_creative_app_interface_for_u_293RUSH.jpgtop_view_of_designers_creative_app_interface_for_u_293RUSH.jpgtop_view_of_designers_creative_app_interface_for_u_293RUSH.jpg
    20+ Best Android App Templates for 2023
    franc_LucasTutspluspic.jpgfranc_LucasTutspluspic.jpgfranc_LucasTutspluspic.jpg
    Franc Lucas
    27 Apr 2023
  • MD.jpgMD.jpgMD.jpg
    Best Material Design Android App Templates
    NB.jpgNB.jpgNB.jpg
    Nona Blackman
    07 Sep 2019

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK