introduction to android studio - eitp.gov.hk · let’s code. go to mainactivity •first, we go to...

Post on 08-Oct-2020

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Introduction to Android Studio

Android StudioThe Official IDE for Android• Android Studio provides the fastest tools for building apps on every

type of Android device.

• World-class code editing, debugging, performance tooling, a flexible build system, and an instant build/deploy system all allow you to focus on building unique and high quality apps.

https://developer.android.com/studio/index.html

Download Link:

Instant Run

• Push code and resource changes to your app running on a device or emulator and see the changes instantly come to life.

• Instant Run dramatically speeds up your edit, build, and run cycles, keeping you "in the flow."

Intelligent code editor

• Write better code, work faster, and be more productive with an intelligent code editor that helps you each step of the way.

• Android Studio is built on IntelliJ and is capable of advanced code completion, refactoring, and code analysis.

Develop for all Android devices

• Target multiple form factors with a single project to easily share code among your different versions of your app.

• Android Studio provides a unified environment to develop apps for Android phones, tablets, Android Wear, Android TV, and Android Auto.

Building Your First AppAfter you've installed the Android SDK, start with this class to learn the basics about Android app development.

Creating an Android Project

In Android Studio, create a new project:

If you don't have a project opened, in the Welcome to Android Studio window, click Start a new Android Studio project.

If you have a project opened, select File > New Project.

In the New Project screen, enter the following values:

Application Name: ”Math App"

Company Domain: "example.com"

Android Studio fills in the package name and project location for you, but you can edit these if you'd like. Click Next.

• In the Target Android Devices screen• Select API16: Android 4.1 (Jelly Bean)

• In the Add an Activity to Mobile screen• select Basic Activity and click Next.

Keep defaults setting and click Finish

SDK Manager

• Click SDK Manager on upper tool bar

• Make sure Android 4.1 (Jelly Bean) is clicked and installed.

Your Virtual Devices – AVD Manager

Make sure you have a AVD created.

If not, click Create Virtual Devices. Choose Nexus5 4.95”

System Image

• Select Marshmallow 23 x86 • For Android 6.0

When finish, close and back to

main windows.

• On your left side.• res = Resourse

• Activity_main.xml = user interface, items ..

In activity_main.xml

• Component Tree content the main level of layout of the App

• You will see the toolbar; include; fab represent items in the preview scene of the phone.

In content_main.xml

• In here, it’s the work place we are going to do with.

• We can edit / drag & drop/ on the layout

Open java – com.example.math – MainActiviry

• We are going to CODE now!

The App Layout

Go to content_main.xml

• Select the “Hello World” , See all Properties .

• Layout_width > match_parent• textAlignment > center• TextSize > 50sp• Text > 0• Id > totalTextView

Add a text

• Drag a “TextView” , in the middle of screen , change the text to “What is”

• TextSize = 25

Add a Number Field

• Number (Decimal)

• Hint > Enter Percentage

• textAlignment > center

Copy and Paste

• Copy “What is” and create “%” & “of”

• Copy ”Enter Percentage” and create” Enter Number”

Add a Button

• Add button in the middel

• Change text to “CALCULATE”

Add a button

• background> …> Red

• Text Color > …> White

Change the bar to red color• Go to “activity_main.xml”

• Select the bar set to Red

Delete the fab button

>

Go to MainActivity

• onCreate

•Android Activity Life Cycle

Android Activity Life Cycle

• Activity is a full-screen user interface of an app.

• Comprehensive app consists of interaction between one or more activities.

• Activities are managed by activity stack of Android system.

• The foreground (running) activity is placed on at the top of activity stack.

States of Activity:Active, Paused, Stopped, Dead:

• Active:• Running Activity (foreground).

• Only ONE activity is active each time.

• Paused• The activity is temporary stopped. (i.e., incoming call, switch to other activity),

the paused activity can go back to active if needed.

• Cannot interact with users.

• Stopped• The activity is off screen and does not run any task. i.e., you quit the app by

pressing home button.

• Dead/Inactive• The activity is switched off manually. (force-quit)

• Finish() is called.

• Flow of activity life cycle.

Activity lifecycle functions:

• onCreate() – initialize your app.

• onDestory() – release resource.

• onPause() – store activity stage and data.

• onResume() – restore stored data.

Let’s code. Go to MainActivity

• First, we go to create 3 variable for

• Ans, %txt , numtxt

• When you type “TextView”, it will show a short cut of “TextView(android.widget).

• Press enter to select.

• It’s help you to create the import element on the top of the code.

Before we code, go back to content_main.xml

• See the id of Enter text area.

• Change the editText > percentageTxt

• Change the editText2 > numberTxt

• Change the button > calcBtn

Button

• variable type = Button

• variable name = calcBtn

• Which target the “calcBtn” form our design screen button “CALCULATE”

OnClickListener()

We yet define the TxtView/EditText

• Do this here.

Start to enter the math method

Delete fab that we don’t need

Run the App

top related