Kotlin Vs


In this codelab, you'll learn how to build & run your first pntechcons.com.vn ứng dụng in the Java programming language. (If you're looking for the Kotlin version of this codelab, you can go here.)

What you must know already

This codelab is written for programmers và assumes that you know either the Java or Kotlin programming language. If you are an experienced programmer and adept at reading code, you will likely be able lớn follow this codelab, even if you don't have much experience with Java.

Bạn đang xem: Kotlin vs

What you"ll learn

How to lớn use pntechcons.com.vn Studio lớn build your ứng dụng.How to lớn run your phầm mềm on a device or in the emulator.How to lớn add interactive buttons.How khổng lồ display a second screen when a button is pressed.

Use pntechcons.com.vn Studio & Java to lớn write pntechcons.com.vn apps

You write pntechcons.com.vn apps in the Java programming language using an IDE called pntechcons.com.vn Studio. Based on JetBrains' IntelliJ IDEA software, pntechcons.com.vn Studio is an IDE designed specifically for pntechcons.com.vn development.

Note: This version of the codelab requires pntechcons.com.vn Studio 3.6 or higher.

To work through this codelab, you will need a computer that can run pntechcons.com.vn Studio 3.6 or higher (or already has pntechcons.com.vn Studio 3.6 or higher installed).

2. Install pntechcons.com.vn Studio

Note: This version of the codelab requires pntechcons.com.vn Studio 3.6 or higher.

You can download pntechcons.com.vn Studio 3.6 from the pntechcons.com.vn Studio page.

pntechcons.com.vn Studio provides a complete IDE, including an advanced code editor & tiện ích templates. It also contains tools for development, debugging, testing, and performance that make it faster & easier to lớn develop apps. You can use pntechcons.com.vn Studio lớn test your apps with a large range of preconfigured emulators, or on your own điện thoại device. You can also build production apps và publish apps on the Google Play store.

Note: pntechcons.com.vn Studio is continually being improved. For the lakiểm tra information on system requirements and installation instructions, see the pntechcons.com.vn Studio download page.

pntechcons.com.vn Studio is available for computers running Windows or Linux, & for Macs running macOS. The OpenJDK (Java Development Kit) is bundled with pntechcons.com.vn Studio.

The installation is similar for all platforms. Any differences are noted below.

Accept the default configurations for all steps, and ensure that all components are selected for installation.After the install is complete, the thiết lập wizard downloads & installs additional components, including the pntechcons.com.vn SDK. Be patient, because this process might take some time, depending on your internet tốc độ.When the installation completes, pntechcons.com.vn Studio starts, and you are ready to lớn create your first project.

Troubleshooting: If you run inlớn problems with your installation, see the pntechcons.com.vn Studio release notes or Troubleshoot pntechcons.com.vn Studio.

3. Task: Create your first project

In this step, you will create a new pntechcons.com.vn project for your first tiện ích. This simple app displays the string "Hello World" on the screen of an pntechcons.com.vn virtual or physical device.

Here's what the finished phầm mềm will look like:


What you"ll learn

How lớn create a project in pntechcons.com.vn Studio.How lớn create an emulated pntechcons.com.vn device.How to run your phầm mềm on the emulator.How khổng lồ run your tiện ích on your own physical device, if you have one.

Step 1: Create a new project

Open pntechcons.com.vn Studio.In the Welcome khổng lồ pntechcons.com.vn Studio dialog, clichồng Start a new pntechcons.com.vn Studio project.
Select Basic Activity (not the default). Cliông xã Next.
Give sầu your application a name such as My First App.Make sure the Language is phối to Java.
Leave sầu the defaults for the other fields.Cliông chồng Finish.

After these steps, pntechcons.com.vn Studio:

Creates a thư mục for your pntechcons.com.vn Studio project called MyFirstApp. This is usually in a thư mục called pntechcons.com.vnStudioProjects below your trang chủ directory.Opens the code editor showing your project.

Step 2: Get your screen mix up

When your project first opens in pntechcons.com.vn Studio, there may be a lot of windows and panes open. To make it easier khổng lồ get to lớn know pntechcons.com.vn Studio, here are some suggestions on how khổng lồ customize the layout.

If there's a Gradle window open on the right side, clichồng on the minimize button () in the upper right corner to hide it.
Depending on the kích thước of your screen, consider resizing the pane on the left showing the project folders lớn take up less space.

At this point, your screen should look a bit less cluttered, similar khổng lồ the screenshot shown below.


Step 3: Explore the project structure and layout

The upper left of the pntechcons.com.vn Studio window should look similar lớn the following diagram:


Based on you selecting the Basic Activity template for your project, pntechcons.com.vn Studio has phối up a number of files for you. You can look at the hierarchy of the files for your phầm mềm in multiple ways, one is in Project view. Project view shows your files & folders structured in a way that is convenient for working with an pntechcons.com.vn project. (This does not always match the file hierarchy! To see the tệp tin hierarchy, choose the Project files view by clicking (3).)

Double-cliông chồng the app (1) folder khổng lồ expvà the hierarchy of tiện ích files. (See (1) in the screenshot.)If you cliông chồng Project (2), you can hide or show the Project view. You might need to select View > Tool Windows khổng lồ see this option.The current Project view selection (3) is Project > pntechcons.com.vn.

In the Project > pntechcons.com.vn view you see three or four top-level folders below your app folder: manifests, java, java (generated) & res. You may not see java (generated) right away.

Exp& the manifests thư mục.

This folder contains pntechcons.com.vnManifest.xml. This file describes all the components of your pntechcons.com.vn ứng dụng & is read by the pntechcons.com.vn runtime system when your phầm mềm is executed. 2. Expand the java thư mục. All your Java language files are organized here. The java thư mục contains three subfolders:

com.example.myfirstapp: This thư mục contains the Java source code files for your app.

com.example.myfirstứng dụng (pntechcons.com.vnTest): This folder is where you would put your instrumented tests, which are tests that run on an pntechcons.com.vn device. It starts out with a skeleton demo tệp tin.

com.example.myfirstứng dụng (test): This folder is where you would put your unit tests. Unit tests don't need an pntechcons.com.vn device to lớn run. It starts out with a skeleton unit demo file. 3. Expand the res folder. This folder contains all the resources for your phầm mềm, including images, layout files, strings, icons, và styling. It includes these subfolders:

drawable: All your app's images will be stored in this thư mục.

layout: This folder contains the UI layout files for your activities. Currently, your phầm mềm has one activity that has a layout file called activity_main.xml. It also contains content_main.xml, fragment_first.xml, and fragment_second.xml.

menu: This thư mục contains XML files describing any menus in your ứng dụng.

mipmap: This thư mục contains the launcher icons for your phầm mềm.

navigation: This folder contains the navigation graph, which tells pntechcons.com.vn Studio how to navigate between different parts of your application.

values: This thư mục contains resources, such as strings and colors, used in your ứng dụng.

Step 4: Create a virtual device (emulator)

In this task, you will use the pntechcons.com.vn Virtual Device (AVD) manager lớn create a virtual device (or emulator) that simulates the configuration for a particular type of pntechcons.com.vn device.

The first step is lớn create a configuration that describes the virtual device.

In pntechcons.com.vn Studio, select Tools > AVD Manager, or cliông chồng the AVD Manager icon in the toolbar.
Click +Create Virtual Device. (If you have sầu created a virtual device before, the window shows all of your existing devices & the +Create Virtual Device button is at the bottom.) The Select Hardware window shows a menu of pre-configured hardware device definitions.Choose a device definition, such as Pixel 2, and cliông chồng Next. (For this codelab, it really doesn't matter which device definition you pick).In the System Image dialog, from the Recommended tab, choose the lakiểm tra release. (This does matter.)If a Download link is visible next to a lachạy thử release, it is not installed yet, và you need to lớn download it first. If necessary, cliông chồng the link lớn start the download, and cliông xã Next when it's done. This may take a while depending on your connection speed.

Note: System images can take up a large amount of disk space, so just tải về what you need.

In the next dialog box, accept the defaults, và click Finish.

The AVD Manager now shows the virtual device you added.

If the Your Virtual Devices AVD Manager window is still open, go ahead and close it.

Xem thêm: Hướng Dẫn Chơi Căn Bản

Step 5: Run your phầm mềm on your new emulator

In pntechcons.com.vn Studio, select Run > Run ‘app' or click the Run icon in the toolbar. The inhỏ will change when your tiện ích is already running.

If you get a dialog box stating "Instant Run requires that the platkhung corresponding khổng lồ your target device (pntechcons.com.vn N...) is installed" go ahead và click Install và continue.

In Run > Select Device, under Available devices, select the virtual device that you just configured. This menu also appears in the toolbar.


The emulator starts và boots just lượt thích a physical device. Depending on the tốc độ of your computer, this may take a while. You can look in the small horizontal status bar at the very bottom of pntechcons.com.vn Studio for messages lớn see the progress.

Messages that might appear briefly in the status bar

Gradle build running


Waiting for target device to lớn come on line


Installing APK


Launching activity


Once your tiện ích builds & the emulator is ready, pntechcons.com.vn Studio uploads the app lớn the emulator & runs it. You should see your phầm mềm as shown in the following screenshot.


Note: It is a good practice to start the emulator at the beginning of your session. Don't cchiến bại the emulator until you are done testing your app, so that you don't have lớn wait for the emulator to boot again. Also, don't have sầu more than one emulator running at once, to reduce memory usage.

Step 6: Run your app on a device (if you have one)

What you need:

An pntechcons.com.vn device such as a phone or tablet.A data cable lớn connect your pntechcons.com.vn device lớn your computer via the USB port.

Run your ứng dụng on a device

To let pntechcons.com.vn Studio communicate with your device, you must turn on USB Debugging on your pntechcons.com.vn device.

On pntechcons.com.vn 4.2 and higher, the pntechcons.com.vn options screen is hidden by default. To show pntechcons.com.vn options & enable USB Debugging:

On your device, open Settings > About phone và tap Build number seven times.Return to the previous screen (Settings). pntechcons.com.vn options appears at the bottom of the các mục. Tap pntechcons.com.vn options.Enable USB Debugging.

Now you can connect your device và run the ứng dụng from pntechcons.com.vn Studio.

Connect your device lớn your development machine with a USB cable. On the device, you might need to lớn agree to allow USB debugging from your development device.In pntechcons.com.vn Studio, click Run in the toolbar at the top of the window. (You might need khổng lồ select View > Toolbar khổng lồ see this option.) The Select Deployment Target dialog opens with the danh mục of available emulators and connected devices.Select your device, and click OK. pntechcons.com.vn Studio installs the app on your device & runs it.

Note: If your device is running an pntechcons.com.vn platsize that isn't installed in pntechcons.com.vn Studio, you might see a message asking if you want khổng lồ install the needed platform. Clichồng Install và Continue, then click Finish when the process is complete.


If you're stuck, quit pntechcons.com.vn Studio & restart it.

If pntechcons.com.vn Studio does not recognize your device, try the following:

Disconnect your device from your development machine & reconnect it.Restart pntechcons.com.vn Studio.

If your computer still does not find the device or declares it "unauthorized":

Disconnect the device.On the device, open Settings->pntechcons.com.vn Options.Tap Revoke USB Debugging authorizations.Reconnect the device to lớn your computer.When prompted, grant authorizations.

If you are still having trouble, check that you installed the appropriate USB driver for your device. See the Using Hardware Devices documentation.

Cheông chồng the troubleshooting section in the pntechcons.com.vn Studio documentation.

Step 7: Explore the phầm mềm template

When you created the project and selected Basic Activity, pntechcons.com.vn Studio set up a number of files, folders, & also user interface elements for you, so you can start out with a working tiện ích và major components in place. This makes it easier to lớn build your application.

Looking at your phầm mềm on the emulator or your device, in addition khổng lồ the Next button, notice the floating action button with an tin nhắn inhỏ.

If you tap that button, you'll see it has been mix up khổng lồ briefly show a message at the bottom of the screen. This message space is called a snackbar, and it's one of several ways khổng lồ notify users of your ứng dụng with brief information.


At the top right of the screen, there's a menu with 3 vertical dots.

If you tap on that, you'll see that pntechcons.com.vn Studio has also created an options thực đơn with a Settings vật phẩm. Choosing Settings doesn't bởi anything yet, but having it phối up for you makes it easier to lớn add user-configurable settings khổng lồ your app.

Later in this codelab, you'll look at the Next button & modify the way it looks và what it does.

4. Task: Explore the layout editor

Generally, each screen in your pntechcons.com.vn ứng dụng is associated with one or more fragments. The single screen displaying "Hello first fragment" is created by one fragment, called FirstFragment. This was generated for you when you created your new project. Each visible fragment in an pntechcons.com.vn phầm mềm has a layout that defines the user interface for the fragment. pntechcons.com.vn Studio has a layout editor where you can create và define layouts.

Layouts are defined in XML. The layout editor lets you define and modify your layout either by coding XML or by using the interactive visual editor.

Every element in a layout is a view. In this task, you will explore some of the panels in the layout editor, and you will learn how to change property of views.

What you"ll learn

How to lớn use the layout editor.How to mix property values.How khổng lồ add string resources.How to lớn add color resources.

Step 1: mở cửa the layout editor

Find & open the layout folder (phầm mềm > res > layout) on the left side in the Project panel.
Double-cliông xã fragment_first.xml.

Troubleshooting: If you don't see the tệp tin fragment_first.xml, confirm you are running pntechcons.com.vn Studio 3.6 or later, which is required for this codelab.

The panels lớn the right of the Project view comprise the Layout Editor. They may be arranged differently in your version of pntechcons.com.vn Studio, but the function is the same.


On the left is a Palette (1) of views you can add to lớn your app.

Below that is a Component Tree (2) showing the views currently in this tệp tin, & how they are arranged in relation to lớn each other.

In the center is the Design editor (3), which shows a visual representation of what the contents of the tệp tin will look like when compiled inkhổng lồ an pntechcons.com.vn app. You can view the visual representation, the XML code, or both.

In the upper right corner of the Design editor, above Attributes (4), find the three icons that look lượt thích this:
These represent Code (code only), Split (code + design), & Design (kiến thiết only) views.Try selecting the different modes. Depending on your screen kích thước and work style, you may prefer switching between CodeDesign, or staying in Split view. If your Component Tree disappears, hide & show the Palette.

Split view:


At the lower right of the Design editor you see + and - buttons for zooming in & out. Use these buttons khổng lồ adjust the kích cỡ of what you see, or cliông chồng the zoom-to-fit button so that both panels fit on your screen.


The Design layout on the left shows how your app appears on the device. The Blueprint layout, shown on the right, is a schematic view of the layout.

Practice using the layout menu in the top left of the kiến thiết toolbar khổng lồ display the design view, the blueprint view, & both views side by side.

Depending on the kích cỡ of your screen and your preference, you may wish to only show the Design view or the Blueprint view, instead of both.

Use the orientation ibé to change the orientation of the layout. This allows you to lớn demo how your layout will fit portrait và landscape modes.
Use the device thực đơn to view the layout on different devices. (This is extremely useful for testing!)


On the right is the Attributes panel. You'll learn about that later.

Step 2: Explore and rekích cỡ the Component Tree

In fragment_first.xml, look at the Component Tree. If it's not showing, switch the mode to Design instead of Split or Code.


This panel shows the view hierarchy in your layout, that is, how the views are arranged in relation khổng lồ each other. 2. If necessary, resize the Component Tree so you can read at least part of the strings. 3. Cliông xã the Hide icon at the top right of the Component Tree.


The Component Tree closes. 4. Bring back the Component Tree by clicking the vertical label Component Tree on the left.


Step 3: Explore view hierarchies

In the Component Tree, notice that the root of the view hierarchy is a ConstraintLayout view.

Every layout must have a root view that contains all the other views. The root view is always a view group, which is a view that contains other views. A ConstraintLayout is one example of a view group. 2. Notice that the ConstraintLayout contains a TextView, called textview_first và a Button, called button_first.

Xem thêm: Top 5 Tựa Game Đua Xe Ô Tô Tốc Độ Hàng Đầu, Game Đua Xe Hay


If the code isn't showing, switch lớn Code or Split view using the icons in the upper right corner.In the XML code, notice that the root element is . The root element contains a element and a element.

Step 4: Change property values

In the code editor, examine the properties in the TextView element.Clichồng on the string in the text property, & you'll notice it refers lớn a string resource, hello_first_fragment.pntechcons.com.vn:text="