Android development - Activities, Views & Intents
-
Upload
lope-emano -
Category
Software
-
view
304 -
download
3
description
Transcript of Android development - Activities, Views & Intents
![Page 1: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/1.jpg)
Android DevelopmentActivities, Views & Intents
by Lope Emano
![Page 2: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/2.jpg)
Activity
![Page 3: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/4.jpg)
The Activity Lifecycle:States and Callbacks
![Page 5: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/8.jpg)
Sample activity overriding the onCreate() callback
![Page 9: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/9.jpg)
Let’s break that down
![Page 10: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/12.jpg)
Android Views
![Page 13: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/13.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/14.jpg)
Declaring a View
![Page 15: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/15.jpg)
Break it down
![Page 16: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/17.jpg)
fill_parent vs wrap_content
![Page 18: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/18.jpg)
ViewGroups
![Page 19: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/21.jpg)
LinearLayout w/ vertical orientation
![Page 22: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/22.jpg)
LinearLayout w/ vertical orientation
![Page 23: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/23.jpg)
You can nest them
![Page 24: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/24.jpg)
![Page 25: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/25.jpg)
RelativeLayout
![Page 26: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/26.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/27.jpg)
![Page 28: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/28.jpg)
![Page 29: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/29.jpg)
Handling Input Events
![Page 30: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/30.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/31.jpg)
Input Events● onClick() example
![Page 32: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/32.jpg)
Break it down
![Page 33: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/33.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/34.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/35.jpg)
Toast being used in Android’s Alarm App
![Page 36: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/36.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/37.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/38.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/39.jpg)
![Page 40: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/40.jpg)
In other words
![Page 41: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/41.jpg)
How do we launch another activity with parameters?
![Page 42: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/42.jpg)
Intent
![Page 43: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/43.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/44.jpg)
![Page 45: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/45.jpg)
Break it down
![Page 46: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/46.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/47.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/48.jpg)
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?
![Page 49: Android development - Activities, Views & Intents](https://reader034.fdocuments.in/reader034/viewer/2022042614/558d267dd8b42a3c638b4669/html5/thumbnails/49.jpg)
References:
● http://www.edureka.co/blog/android-tutorials-for-beginners-activity-component/
● http://www.tutorialspoint.com/android/android_acitivities.htm
● http://developer.android.com/reference/android/app/Activity.html