Android development - Activities, Views & Intents

49
Android Development Activities, Views & Intents by Lope Emano

description

Android development - Activities, Views & Intents

Transcript of Android development - Activities, Views & Intents

Page 1: Android development - Activities, Views & Intents

Android DevelopmentActivities, Views & Intents

by Lope Emano

Page 2: Android development - Activities, Views & Intents

Activity

Page 3: Android development - Activities, Views & Intents

Activity● provides the user a screen/interface in order to

interact with the application● android applications are generally made up of

activities● activities have their own “lifecycles” in order to

manage multiple activities and lessen their impact to a device’s memory and resources

● activities have 4 states throughout its lifecycle● an activity’s lifecycle has a series of callbacks that we

can leverage on when building apps

Page 4: Android development - Activities, Views & Intents

The Activity Lifecycle:States and Callbacks

Page 5: Android development - Activities, Views & Intents

Activity Lifecycle States● Active or running

the activity is in the foreground of the screen● Paused

activity has lost focus but is still visibleactivity is alive but can be killed if low in memory

● Stoppedactivity is completely obscured by another activity

● Destroyedactivity is dropped from memory, killed/finished. to

display it again, it must be restarted

Page 6: Android development - Activities, Views & Intents

Activity Lifecycle Callbacks● onCreate()

This is the first callback and called when the activity is first created.● onStart()

This callback is called when the activity becomes visible to the user.● onResume()

This is called when the user starts interacting with the application.● onPause()

Where you deal with the user leaving your activity. Any changes made by the user should at this point be committed

Page 7: Android development - Activities, Views & Intents

Activity Lifecycle Callbacks● onStop()

Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one

● onDestroy()The final call you receive before your activity is

destroyed.● onRestart()

Called after your activity has been stopped, prior to it being started again.

Page 8: Android development - Activities, Views & Intents

Sample activity overriding the onCreate() callback

Page 9: Android development - Activities, Views & Intents

Let’s break that down

Page 10: Android development - Activities, Views & Intents

MainActivity breakdown● @Override onCreate(Bundle savedInstanceState)

the lifecycle callbackBundle object contains extra data when the activity

gets started (more on this later)

● super.onCreate()invokes the onCreate callback of the parent class

to maintain consistent behaviour

● setContentView(R.layout.activity_main)sets the layout for the activity

Page 11: Android development - Activities, Views & Intents

R.javaR.java or “R” connects java code and resource files contained in the src/main/res folder

This file is auto generated when compiling the code. You should not be editing this.

Example:src/res/layout/activity_main.xml --- R.layout.activity_main

src/res/drawable-hdpi/ic_launcher.png --- R.drawable.ic_launcher

Page 12: Android development - Activities, Views & Intents

Android Views

Page 13: Android development - Activities, Views & Intents

Viewrepresents the basic building block for user interface components

occupies a rectangular area on the screen and is responsible for drawing and event handling

Examples:Button, TextView, EditText, ImageView, RadioButton, Checkbox

Page 14: Android development - Activities, Views & Intents

Declaring a View

Page 15: Android development - Activities, Views & Intents

Break it down

Page 16: Android development - Activities, Views & Intents

Declaring a View● <TextView></TextView>

views are defined in xml

● android:id=”@+id/txt_hello_world”One of the many tags a view can have. this one

specifies the id of the given view. This will be accessible later in our java code via R.id.txt_hello_world

● android:layout_width & android:layout_heightThe size of the view’s rectangular area relative to its

parent.Example layout types:

match_parent - size matches the view’s parentwrap_content - size is contained to the size of the

view’s content.

Page 17: Android development - Activities, Views & Intents

fill_parent vs wrap_content

Page 18: Android development - Activities, Views & Intents

ViewGroups

Page 19: Android development - Activities, Views & Intents

ViewGroupa special view that can contain other views which are then called child views.

Examples:LinearLayout, RelativeLayout, TableLayout

Page 20: Android development - Activities, Views & Intents

LinearLayoutA Layout that arranges its children in a single column

or a single row

The direction of the row can be set by setting android:orientation=”horizontal|vertical”

The default direction is horizontal

Page 21: Android development - Activities, Views & Intents

LinearLayout w/ vertical orientation

Page 22: Android development - Activities, Views & Intents

LinearLayout w/ vertical orientation

Page 23: Android development - Activities, Views & Intents

You can nest them

Page 24: Android development - Activities, Views & Intents
Page 25: Android development - Activities, Views & Intents

RelativeLayout

Page 26: Android development - Activities, Views & Intents

RelativeLayoutA Layout where the positions of the children can be

described in relation to each other or to the parent.

Example:

android:layout_aboveandroid:layout_belowandroid:layout_toRightOfandroid:layout_toLeftOf

Page 27: Android development - Activities, Views & Intents
Page 28: Android development - Activities, Views & Intents
Page 29: Android development - Activities, Views & Intents

Handling Input Events

Page 30: Android development - Activities, Views & Intents

Input Events● onClick()

From View.OnClickListener. ● onLongClick()

From View.OnLongClickListener. ● onFocusChange()

From View.OnFocusChangeListener. Called when the user navigates onto or away from the item

● onKey()From View.OnKeyListener. Called when the user is focused on the item and presses or releases a hardware key on the device.

● onTouch()From View.OnTouchListener. Called when the user performs an action qualified as a touch event

Page 31: Android development - Activities, Views & Intents

Input Events● onClick() example

Page 32: Android development - Activities, Views & Intents

Break it down

Page 33: Android development - Activities, Views & Intents

Input EventsButton button = (Button) findViewById(R.id.btnLogin);● a Button is a type of View or Button inherits View● findViewById() is accessible within the Activity class.● (Button) is type casting in java. We need to do this

because findViewById() returns a View object.● R.id.btnLocation

R is a reference for R.java, the bridge between java and xml code. N

Note the btnLocation naming convention

Page 34: Android development - Activities, Views & Intents

Input Eventsbutton.setOnClickListener(new View.OnClickListener{});● setOnClickListener is inherited from the View class● View.OnClickListener is a java interface. When

instantiating an interface directly you must supply its required methods.

Toast.makeText(getApplicationContext(), "You clicked me!", Toast.LENGTH_SHORT);● Toast provides simple feedback about an operation in

a small popup.● Toast.LENGTH_SHORT, Toast.LENGTH_LONG are

integers that return the toast’s display duration

Page 35: Android development - Activities, Views & Intents

Toast being used in Android’s Alarm App

Page 36: Android development - Activities, Views & Intents

Input Eventsbutton.setOnClickListener(new View.OnClickListener{});● setOnClickListener is inherited from the View class● View.OnClickListener is a java interface. When

instantiating an interface directly you must supply its required methods.

Toast.makeText(getApplicationContext(), "You clicked me!", Toast.LENGTH_SHORT).show();● Toast provides simple feedback about an operation in

a small popup.● Toast.LENGTH_SHORT, Toast.LENGTH_LONG are

integers that return the toast’s display duration● show() to display

Page 37: Android development - Activities, Views & Intents

getApplicationContext()● the context of current state of the application/object

and lets newly created object understand what’s going on

● you’ll be using this a lot as an android developer, many objects will require it

● 2 kinds of ContextActivity Context - Accessible via getContext or this

within an Activity class

Application Context - The context of the entire application. Use this if you want something to be hooked to the app’s lifecycle instead of only a specific activity

Page 38: Android development - Activities, Views & Intents

Suppose we have an activity that shows a list and when we tap on

an item in that list we’d like to view it in another activity. How

would we do that?

Page 39: Android development - Activities, Views & Intents
Page 40: Android development - Activities, Views & Intents

In other words

Page 41: Android development - Activities, Views & Intents

How do we launch another activity with parameters?

Page 42: Android development - Activities, Views & Intents

Intent

Page 43: Android development - Activities, Views & Intents

IntentAn abstract description of an operation to be

performed

Its most significant use is in the launching of activities, where it can be thought of as the glue between activities

2 Primary pieces of information:

● action - The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.

● data - The data to operate on, such as an ID, url, or even image taken from a camera activity.

Page 44: Android development - Activities, Views & Intents
Page 45: Android development - Activities, Views & Intents

Break it down

Page 46: Android development - Activities, Views & Intents

MainActivity CodeIntent intent = new Intent() //instantiate intent object//specify target activityintent.setClass(this, OtherActivity.class)//add extra values as a key-value pairintent.putExtra(“KEY”, “VALUE”);//callable within any Activity classstartActivity(intent);

Page 47: Android development - Activities, Views & Intents

Other_Activity Code//Calling getIntent() will retrieve the intent used to launch this activityBundle extras = getIntent().getExtras();//getString() is used because we know that the key’s value is a String objectString datas = extras.getString(“KEY”);

Page 48: Android development - Activities, Views & Intents

What we’ve learned● A lot. Like many other frameworks, reading the docs

and hunting down resources in the internet is important as an android developer.

● Activities have their own lifecycles and states.● Xml syntax for views can be taxing, but you’ll get used

to it!● There can be many ways to create a layout using

ViewGroups, we just need to find the most efficient/clean.

● Intents can be tedious since you have to know certain things in order to do them properly. i.e. what if you want to bundle an object instead of a POJO?