SlideShare una empresa de Scribd logo
1 de 49
Descargar para leer sin conexión
Android Development
Activities, Views & Intents
by Lope Emano
Activity
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
The Activity Lifecycle:
States and Callbacks
Activity Lifecycle States
● Active or running
the activity is in the foreground of the screen
● Paused
activity has lost focus but is still visible
activity is alive but can be killed if low in memory
● Stopped
activity is completely obscured by another activity
● Destroyed
activity is dropped from memory, killed/finished. to
display it again, it must be restarted
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
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.
Sample activity overriding the
onCreate() callback
Let’s break that down
MainActivity breakdown
● @Override onCreate(Bundle savedInstanceState)
the lifecycle callback
Bundle 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
R.java
R.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
Android Views
View
represents 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
Declaring a View
Break it down
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_height
The size of the view’s rectangular area relative to its
parent.
Example layout types:
match_parent - size matches the view’s parent
wrap_content - size is contained to the size of the
view’s content.
fill_parent vs wrap_content
ViewGroups
ViewGroup
a special view that can contain other views which are
then called child views.
Examples:
LinearLayout, RelativeLayout, TableLayout
LinearLayout
A 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
LinearLayout w/ vertical orientation
LinearLayout w/ vertical orientation
You can nest them
RelativeLayout
RelativeLayout
A Layout where the positions of the children can be
described in relation to each other or to the parent.
Example:
android:layout_above
android:layout_below
android:layout_toRightOf
android:layout_toLeftOf
Handling Input Events
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
Input Events
● onClick() example
Break it down
Input Events
Button 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
Input Events
button.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
Toast being used in Android’s Alarm App
Input Events
button.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
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 Context
Activity 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
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?
In other words
How do we launch another
activity with parameters?
Intent
Intent
An 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.
Break it down
MainActivity Code
Intent intent = new Intent() //instantiate intent object
//specify target activity
intent.setClass(this, OtherActivity.class)
//add extra values as a key-value pair
intent.putExtra(“KEY”, “VALUE”);
//callable within any Activity class
startActivity(intent);
Other_Activity Code
//Calling getIntent() will retrieve the intent used to launch
this activity
Bundle extras = getIntent().getExtras();
//getString() is used because we know that the key’s
value is a String object
String datas = extras.getString(“KEY”);
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?
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

Más contenido relacionado

La actualidad más candente

Android apps development
Android apps developmentAndroid apps development
Android apps developmentRaman Pandey
 
Android Components & Manifest
Android Components & ManifestAndroid Components & Manifest
Android Components & Manifestma-polimi
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Androidma-polimi
 
Presentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestatePresentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestateOsahon Gino Ediagbonya
 
android activity
android activityandroid activity
android activityDeepa Rani
 
Android activity
Android activityAndroid activity
Android activityKrazy Koder
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycleAhsanul Karim
 
Android activity, service, and broadcast recievers
Android activity, service, and broadcast recieversAndroid activity, service, and broadcast recievers
Android activity, service, and broadcast recieversUtkarsh Mankad
 
Android: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast ReceiversAndroid: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast ReceiversCodeAndroid
 
Android Bootcamp Tanzania: android manifest
Android Bootcamp Tanzania: android manifestAndroid Bootcamp Tanzania: android manifest
Android Bootcamp Tanzania: android manifestDenis Minja
 
Day 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through ActivitiesDay 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through ActivitiesAhsanul Karim
 
Mad textbook 63-116
Mad textbook 63-116Mad textbook 63-116
Mad textbook 63-116PrathishGM
 

La actualidad más candente (18)

Android apps development
Android apps developmentAndroid apps development
Android apps development
 
Android Components & Manifest
Android Components & ManifestAndroid Components & Manifest
Android Components & Manifest
 
Android Components
Android ComponentsAndroid Components
Android Components
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Android
 
Presentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestatePresentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestate
 
android activity
android activityandroid activity
android activity
 
Android activity
Android activityAndroid activity
Android activity
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycle
 
Android Lesson 2
Android Lesson 2Android Lesson 2
Android Lesson 2
 
Android development session 2 - intent and activity
Android development   session 2 - intent and activityAndroid development   session 2 - intent and activity
Android development session 2 - intent and activity
 
Android activity, service, and broadcast recievers
Android activity, service, and broadcast recieversAndroid activity, service, and broadcast recievers
Android activity, service, and broadcast recievers
 
Android components
Android componentsAndroid components
Android components
 
Android: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast ReceiversAndroid: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast Receivers
 
Android Bootcamp Tanzania: android manifest
Android Bootcamp Tanzania: android manifestAndroid Bootcamp Tanzania: android manifest
Android Bootcamp Tanzania: android manifest
 
Activity
ActivityActivity
Activity
 
Android development session 4 - Fragments
Android development   session 4 - FragmentsAndroid development   session 4 - Fragments
Android development session 4 - Fragments
 
Day 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through ActivitiesDay 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through Activities
 
Mad textbook 63-116
Mad textbook 63-116Mad textbook 63-116
Mad textbook 63-116
 

Similar a Android development - Activities, Views & Intents

Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesAhsanul Karim
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesAhsanul Karim
 
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptxMugiiiReee
 
Android apps development
Android apps developmentAndroid apps development
Android apps developmentMonir Zzaman
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android DevelopmentAly Abdelkareem
 
Android development Training Programme Day 2
Android development Training Programme Day 2Android development Training Programme Day 2
Android development Training Programme Day 2DHIRAJ PRAVIN
 
ANDROID LAB MANUAL.doc
ANDROID LAB MANUAL.docANDROID LAB MANUAL.doc
ANDROID LAB MANUAL.docPalakjaiswal43
 
Multiple Activity and Navigation Primer
Multiple Activity and Navigation PrimerMultiple Activity and Navigation Primer
Multiple Activity and Navigation PrimerAhsanul Karim
 
Quick Intro to Android Development
Quick Intro to Android DevelopmentQuick Intro to Android Development
Quick Intro to Android DevelopmentJussi Pohjolainen
 
Android应用开发简介
Android应用开发简介Android应用开发简介
Android应用开发简介easychen
 
Static Reference Analysis for GUI Objects in Android Software
Static Reference Analysis for GUI Objects in Android SoftwareStatic Reference Analysis for GUI Objects in Android Software
Static Reference Analysis for GUI Objects in Android SoftwareDacong (Tony) Yan
 

Similar a Android development - Activities, Views & Intents (20)

Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through Activities
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through Activities
 
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
 
Android apps development
Android apps developmentAndroid apps development
Android apps development
 
Android App development III
Android App development IIIAndroid App development III
Android App development III
 
Lab1-android
Lab1-androidLab1-android
Lab1-android
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
 
Android development Training Programme Day 2
Android development Training Programme Day 2Android development Training Programme Day 2
Android development Training Programme Day 2
 
ANDROID LAB MANUAL.doc
ANDROID LAB MANUAL.docANDROID LAB MANUAL.doc
ANDROID LAB MANUAL.doc
 
Multiple Activity and Navigation Primer
Multiple Activity and Navigation PrimerMultiple Activity and Navigation Primer
Multiple Activity and Navigation Primer
 
Activity & Shared Preference
Activity & Shared PreferenceActivity & Shared Preference
Activity & Shared Preference
 
Activity
ActivityActivity
Activity
 
Activity
ActivityActivity
Activity
 
Activity
ActivityActivity
Activity
 
Android 3
Android 3Android 3
Android 3
 
Quick Intro to Android Development
Quick Intro to Android DevelopmentQuick Intro to Android Development
Quick Intro to Android Development
 
Android session 2
Android session 2Android session 2
Android session 2
 
Android
AndroidAndroid
Android
 
Android应用开发简介
Android应用开发简介Android应用开发简介
Android应用开发简介
 
Static Reference Analysis for GUI Objects in Android Software
Static Reference Analysis for GUI Objects in Android SoftwareStatic Reference Analysis for GUI Objects in Android Software
Static Reference Analysis for GUI Objects in Android Software
 

Más de Lope Emano

Android development - ListView & Adapter
Android development - ListView & AdapterAndroid development - ListView & Adapter
Android development - ListView & AdapterLope Emano
 
Android development war stories
Android development war storiesAndroid development war stories
Android development war storiesLope Emano
 
Android Development - Process & Tools
Android Development - Process & ToolsAndroid Development - Process & Tools
Android Development - Process & ToolsLope Emano
 
Android development - Network Requests
Android development - Network RequestsAndroid development - Network Requests
Android development - Network RequestsLope Emano
 
Android Architecture
Android ArchitectureAndroid Architecture
Android ArchitectureLope Emano
 
Android History & Importance
Android History & ImportanceAndroid History & Importance
Android History & ImportanceLope Emano
 
Android development War Stories
Android development War StoriesAndroid development War Stories
Android development War StoriesLope Emano
 
Dependency Injection in Android with Dagger
Dependency Injection in Android with DaggerDependency Injection in Android with Dagger
Dependency Injection in Android with DaggerLope Emano
 
Android development
Android developmentAndroid development
Android developmentLope Emano
 

Más de Lope Emano (9)

Android development - ListView & Adapter
Android development - ListView & AdapterAndroid development - ListView & Adapter
Android development - ListView & Adapter
 
Android development war stories
Android development war storiesAndroid development war stories
Android development war stories
 
Android Development - Process & Tools
Android Development - Process & ToolsAndroid Development - Process & Tools
Android Development - Process & Tools
 
Android development - Network Requests
Android development - Network RequestsAndroid development - Network Requests
Android development - Network Requests
 
Android Architecture
Android ArchitectureAndroid Architecture
Android Architecture
 
Android History & Importance
Android History & ImportanceAndroid History & Importance
Android History & Importance
 
Android development War Stories
Android development War StoriesAndroid development War Stories
Android development War Stories
 
Dependency Injection in Android with Dagger
Dependency Injection in Android with DaggerDependency Injection in Android with Dagger
Dependency Injection in Android with Dagger
 
Android development
Android developmentAndroid development
Android development
 

Último

Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 

Último (20)

Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 

Android development - Activities, Views & Intents

  • 1. Android Development Activities, Views & Intents by Lope Emano
  • 3. 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
  • 5. Activity Lifecycle States ● Active or running the activity is in the foreground of the screen ● Paused activity has lost focus but is still visible activity is alive but can be killed if low in memory ● Stopped activity is completely obscured by another activity ● Destroyed activity is dropped from memory, killed/finished. to display it again, it must be restarted
  • 6. 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
  • 7. 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.
  • 8. Sample activity overriding the onCreate() callback
  • 10. MainActivity breakdown ● @Override onCreate(Bundle savedInstanceState) the lifecycle callback Bundle 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
  • 11. R.java R.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
  • 13. View represents 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
  • 16. 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_height The size of the view’s rectangular area relative to its parent. Example layout types: match_parent - size matches the view’s parent wrap_content - size is contained to the size of the view’s content.
  • 19. ViewGroup a special view that can contain other views which are then called child views. Examples: LinearLayout, RelativeLayout, TableLayout
  • 20. LinearLayout A 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
  • 23. You can nest them
  • 24.
  • 26. RelativeLayout A Layout where the positions of the children can be described in relation to each other or to the parent. Example: android:layout_above android:layout_below android:layout_toRightOf android:layout_toLeftOf
  • 27.
  • 28.
  • 30. 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
  • 33. Input Events Button 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
  • 34. Input Events button.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
  • 35. Toast being used in Android’s Alarm App
  • 36. Input Events button.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
  • 37. 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 Context Activity 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
  • 38. 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?
  • 39.
  • 41. How do we launch another activity with parameters?
  • 43. Intent An 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.
  • 44.
  • 46. MainActivity Code Intent intent = new Intent() //instantiate intent object //specify target activity intent.setClass(this, OtherActivity.class) //add extra values as a key-value pair intent.putExtra(“KEY”, “VALUE”); //callable within any Activity class startActivity(intent);
  • 47. Other_Activity Code //Calling getIntent() will retrieve the intent used to launch this activity Bundle extras = getIntent().getExtras(); //getString() is used because we know that the key’s value is a String object String datas = extras.getString(“KEY”);
  • 48. 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?