SlideShare a Scribd company logo
1 of 50
Lec 5 – Creating Your First
Application
Eyad M. AlMassri
Using AndroidStudio –basic
Android
• Lets do a “Hello World Project”
• Start up AndroidStudio (assume you
have installed with Android support)
Hello World Project – Android Studio
1. App name, package
2. Target
3. Activity type
4. Name of Activity
5. Now finish to setup
Hello World Project
Hello World Project---basic folders &files
Folders
• Number of folders and files
are auto-
generated when you create a
project
File of note
AndroidManifest.xml – like a
ship’s manifest
it tells us the components of our
app
Project basics….
Android Studio
• manifests/AndroidManifest.xml:
an XML file describing the
application being built and
what components – activities,
services, etc. – are being
supplied by that application
• java/: holds the Java source
code for the application
res/: contains
layout files
drawable =icons
menus
values = static files like strings.xml
Gradle/: an Ant script for
compiling the application and
installing it on the device
(integrated with IDE—don’t see it
there)
default.properties: a property file
used by the compiler
res/ – contains multiple xml files
and other resources used by
application
Project basics….
Android Studio
•Gradle Scripts/: an gradle script
for compiling the application and
installing it on the device
(integrated with IDE—don’t see
it there)
–gradle.properties: a property file
used by the compiler
Manifest file – AndroidManifest.xml
• an XML file describing the application being built
and what components – activities, services,
etc. – are being supplied by that application
• Initially created by IDE
• must declare all activities, services, broadcast
receivers and content provider of the application.
• must contain the required permissions for the
application.
– For example if the application requires network access it
must be specified here.
Hello World Project – AndroidManifest.xml
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworldandroid"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloWorldAndroid"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>
Here have intent created for
This acitivity that is associated with
launch of application
Define Application –see res/strings.xml
–see res/drawable-resolution/icon.png
THE LAYOUT
Interface specifications
Interface ---2 options
• Do it with XML file(s)
– Many modern frameworks whether for mobile
programming like Android or iOS or for other
platforms have gone to specifying GUI (graphical
user interface) elements in static XML files rather
than programming source code (like java).
– The reason –it allows separation of the look (view)
from how the code works (model and controller).
Have you ever heard of Model View Controller –it
is a famous software engineering framework that
programmers try to achieve in their software
systems.
• Do it with Java code.
– This has similarities if you have created desktop
GUI Java applications
OPTION 1: The Layout with
XML
• We are going to discuss a
specific resource in the
res folder
res/layout/activity_main.xml
The Layout-the interface
• res/layout/main.xml = contains
layout for interface
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
The above will create an interface in vertical (versus portrait) mode that fills
the parent
Both in width and write and wraps and content as necessary.
The Layout-the interface
• res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
• android:orientation = vertical (versus portrait) mode.
• that fills the parent both in width and write and wraps and
content as necessary.
Landscape
Vertical
Hello World Project
Layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
This adds a “TextView” to the interface and this has the text
referenced by @string/hello in it.
Creating a Layout file—2
options
1) Do it manually –type it in
2) For Layouts corresponding to GUIs
You have a drag and drop option
we will learn about in a later lecture
Using IDE to Visually
Create XML file
• Visual creation of XML file
• AndroidStudio: New->
Layout resource file
– Select layout (root)
– &qualifiers as needed
– drag & drop
Visually Creating XML interface
• I dragged and dropped an EditText
view and a Button. Below I show you
the corresponding code.
res/layout/main2.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:text="@string/hello" android:id="@+id/editText1" android:inputType="textMultiLine"
android:layout_width="169dp" android:layout_height="115dp" android:layout_x="11dp"
android:layout_y="20dp"></EditText>
<Button android:id="@+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button" android:layout_x="27dp"
android:layout_y="146dp"></Button>
</AbsoluteLayout>
LETS LOOK AT THE “MAIN”
CLASS
It is an Activity
Activity is a class in the Android sdk (android.app.Activity)
It represents us now the main application itself—we will learn
more about what this means in a near future lecture –remember
we are at the beginning!
Create an Activity with simple
text Interface ……first
• An Android user interface is
composed of hierarchies of objects
called Views.
• A View is a drawable object used as
an element in your UI layout, such as
a button, image, or
– In the next code will create a text label.
Each of these objects is a subclass of
the View class and the subclass that
handles text is TextView.
Hello World Project – here we are creating
interface with programatically/code
create a TextView with the class constructor,
which accepts an Android Context instance as
its parameter.
•Context is a handle to the system
•provides services like resolving resources,
•obtaining access to databases and preferences, etc.
The Activity class inherits from Context, and because
your HelloAndroid class is a subclass of Activity, it is
also a Context. So, you can pass this as your Context
reference to the TextView.
text content is set with setText(CharSequence)
pass the TextView to setContentView() in order to display
it as the content for the Activity UI.
Hello World Project – here we are creating
interface with programatically/code
Helloworld.java
package com.teach.helloworld;
import android.app.Activity;
import android.os.Bundle;
public class helloworld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android.....Lynne");
setContentView(tv);
}
}
What we just learned
• You will have a class created for you
that is your android application, i.e. “
helloworld” and it extends Activity
• Your application is an Activity and
that also descends from Content
• Context = class that gives us access
to System
• provides services like resolving resources,
• obtaining access to databases and
preferences, etc
• .constructor needs Context instance (we
What we just learned
• View = is a GUI element, there are
different kinds like the TextView we
are using
Alter your Activity to Use an
XML layout file
• package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Now going to change the
code so it uses instead an
XML file for its intervace
This line of code says
use the xml layout file called
main.xml that is pointed to
inside your R.java file in memory
Interfaces ---XML-based layout
• Previous HelloWorld example has
interface created in java code.
• ALTERNATIVE: XML-based layout
files. Example that duplicates previous.
• <?xml version="1.0" encoding="utf-8"?>
<LinearLayout……….>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"/>
Above located in res/layout/main.xml
Here is our main.xml file
that will be used and do
the same thing as our
previous java code –show
a TextView
XML interface
• <TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="hello"/>
– xmlns:android XML namespace declaration that tells
the Android tools that you are going to refer to common
attributes defined in the Android namespace. The
outermost tag in every Android layout file must have this
attribute.
– android:layout_width This attribute defines how much of
the available width on the screen this View should
consume. As it's the only View so you want it to take up
the entire screen, which is what a value of "fill_parent"
Lets understand the XML
Tag used to describe our
TextView
XML interface
• android:layout_height This is just like
android:layout_width, except that it
refers to available screen height.
• android:text This sets the text that
the TextView should display. In this
example, you use a string resource
instead of a hard-coded string value.
The hello string is defined in the
res/values/strings.xml file.
MORE ON RESOURCES
The res directory
We mentioned res folder
Android Studio
res/ directory
• res/layout/ : contains one or more
xml files that define interfaces
• res/values/strings.xml : file where
you specify strings (constants) that
you can use in your program
• res/drawable-* : gives file use for
different resolutions like icon.jpg.
*=hdpi (high), mdpi(medium), ldpi
(low). Depends on device what
mode it can/may be running in.
• res/menu/ : contains one or more
xml files for menus
res/ directory
EVENT HANDLING
So we have an interface what can we do with it.
Event Handling = code that responds when “events”
happen. For GUI elements we usually have events
associated with them. For example, a button has the event
of hitting the button.
res/ directory
Widget : Views that have events
• For a list of the widgets provided by
Android, see the android.widget
package.
• Some Examples
– Button
– CheckBox
– DatePicker
– EditText
– ImageView
– SearchView
There are more ---here are a
few.
Event Handling
• Decide what Widgets who’s events to
process
3 steps:
1)Decide what events to respond to
2)Create a listener to respond to each
event
3)Register the listener to the
corresponding widget
• Define an event listener and
register it with the View.
– View.OnClickListener (for handling
"clicks" on a View),
View.OnTouchListener (for handling
touch screen events in a View), and
View.OnKeyListener (for handling
device key presses within a View)
• http://developer.android.com/guid
e/topics/ui/ui-events.html details
more
Event Handling
Lets add a Button to a program-
based interface
• Step 1: Add button
• Step 2: Register Event Handler
• TWO OPTIONS – separate class to
handle event(s), OR have the Activity
containing the button do the event
handling
• Step 3: Implement Event
Handler…for a Button means
implementing the
View.OnClickListener interface
Event handling done by Activity
itself –one option
• Here code to handle is inside Activity
itself
• public class ExampleActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedValues) {
...
Button button = (Button)findViewById(R.id.corky);
button.setOnClickListener(this);
}
// Implement the OnClickListener callback
public void onClick(View v) {
// do something when the button is clicked
}
...
}
This option is okay only if the
event handling code is simple
and you will not reuse it ever
---if the code is longer or will
reuse make a separate class
Event Handling - here have a
SEPARATE class
EVENT HANDLING CODE in separate object mCorkyListner
// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
//Now inside your Activity class
protected void onCreate(Bundle savedValues) {
...
// STEP 1: Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
// STEP 2: Register the onClick listener with the implementation above
button.setOnClickListener(mCorkyListener);
...
}
Better for readability and
reuse
RUN YOUR CODE
Running Your Android
Application in the Emulator
• Now you can run the
MyFirstAndroidApp project using
the following steps found on the next
few slides.
Running Your Android
Application in the Emulator
(Cont’d)1. With the Run/Debug Configuration named app
already selected, click the Run icon ( ) on the
toolbar.
– Here is the app configuration selected:
Running Your Android
Application in the Emulator
(Cont’d)
2. You are now prompted to Choose a running
device.
– The default emulator you launched for the
previous example should be listed as a
running device.
– If it is not already running, select Launch
emulator and choose the appropriate AVD
if it is not already selected.
– Then click OK.
Running Your Android
Application in the Emulator
(Cont’d)
Running Your Android
Application in the Emulator
(Cont’d)
3. If not already started, the Android
emulator starts up, which might take a
moment.
4. Unlock the emulator if it is locked.
5. The application starts, as shown in
the figure on the next slide.
Running Your Android
Application in the Emulator
(Cont’d)
Running Your Android
Application in the Emulator
(Cont’d)
6. Click the Back button in the emulator to end
the application, or click Home to suspend it.
7. Click the All Apps button found in the
Favorites tray to browse all installed
applications from the All Apps screen.
Running Your Android
Application in the Emulator
(Cont’d)
8. Your screen should now present all the
applications installed on the device.
– Click the My First Android App icon
to launch the application again.
Running Your Android
Application in the Emulator
(Cont’d)
Running code
• TIP: Emulator can take a long time to load at
first----be patient and keep it up---just re-run
after changes and won’t have to relaunch
emulator, will just load up new app.
• Look if you have Intell Virtualization speed
up, check out GPU and snapshot options –
search online for current info on these tips

More Related Content

What's hot

Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android DevelopmentAly Abdelkareem
 
Android Technology
Android TechnologyAndroid Technology
Android TechnologyR
 
Android Development Workshop
Android Development WorkshopAndroid Development Workshop
Android Development WorkshopPeter Robinett
 
Android fundamentals and tutorial for beginners
Android fundamentals and tutorial for beginnersAndroid fundamentals and tutorial for beginners
Android fundamentals and tutorial for beginnersBoom Shukla
 
Introduction to android
Introduction to androidIntroduction to android
Introduction to androidzeelpatel0504
 
Introduction to Android development - Presentation Report
Introduction to Android development - Presentation ReportIntroduction to Android development - Presentation Report
Introduction to Android development - Presentation ReportAtul Panjwani
 
Android Overview
Android OverviewAndroid Overview
Android Overviewatomi
 
Android Development: The Basics
Android Development: The BasicsAndroid Development: The Basics
Android Development: The BasicsMike Desjardins
 
Custom Android App Development – Web Animation India
Custom Android App Development – Web Animation IndiaCustom Android App Development – Web Animation India
Custom Android App Development – Web Animation IndiaMarion Welch
 
Android Programming Basics
Android Programming BasicsAndroid Programming Basics
Android Programming BasicsEueung Mulyana
 
What's new in android M(6.0)
What's new in android M(6.0)What's new in android M(6.0)
What's new in android M(6.0)Yonatan Levin
 
Android Training - Part 2
Android Training - Part 2Android Training - Part 2
Android Training - Part 2Tbldevelopment
 
Android Web app
Android Web app Android Web app
Android Web app Sumit Kumar
 
Android terminologies
Android terminologiesAndroid terminologies
Android terminologiesjerry vasoya
 
android-tutorial-for-beginner
android-tutorial-for-beginnerandroid-tutorial-for-beginner
android-tutorial-for-beginnerAjailal Parackal
 
Generating efficient APK by Reducing Size and Improving Performance
Generating efficient APK by Reducing Size and Improving PerformanceGenerating efficient APK by Reducing Size and Improving Performance
Generating efficient APK by Reducing Size and Improving PerformanceParesh Mayani
 
Android Apps Development Basic
Android Apps Development BasicAndroid Apps Development Basic
Android Apps Development BasicMonir Zzaman
 

What's hot (20)

Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
 
Android Technology
Android TechnologyAndroid Technology
Android Technology
 
Android Development Workshop
Android Development WorkshopAndroid Development Workshop
Android Development Workshop
 
Android fundamentals and tutorial for beginners
Android fundamentals and tutorial for beginnersAndroid fundamentals and tutorial for beginners
Android fundamentals and tutorial for beginners
 
Introduction to android
Introduction to androidIntroduction to android
Introduction to android
 
Introduction to Android development - Presentation Report
Introduction to Android development - Presentation ReportIntroduction to Android development - Presentation Report
Introduction to Android development - Presentation Report
 
Android Overview
Android OverviewAndroid Overview
Android Overview
 
Android Programming
Android ProgrammingAndroid Programming
Android Programming
 
Android Training
Android TrainingAndroid Training
Android Training
 
Android Development: The Basics
Android Development: The BasicsAndroid Development: The Basics
Android Development: The Basics
 
Custom Android App Development – Web Animation India
Custom Android App Development – Web Animation IndiaCustom Android App Development – Web Animation India
Custom Android App Development – Web Animation India
 
Android mp3 player
Android mp3 playerAndroid mp3 player
Android mp3 player
 
Android Programming Basics
Android Programming BasicsAndroid Programming Basics
Android Programming Basics
 
What's new in android M(6.0)
What's new in android M(6.0)What's new in android M(6.0)
What's new in android M(6.0)
 
Android Training - Part 2
Android Training - Part 2Android Training - Part 2
Android Training - Part 2
 
Android Web app
Android Web app Android Web app
Android Web app
 
Android terminologies
Android terminologiesAndroid terminologies
Android terminologies
 
android-tutorial-for-beginner
android-tutorial-for-beginnerandroid-tutorial-for-beginner
android-tutorial-for-beginner
 
Generating efficient APK by Reducing Size and Improving Performance
Generating efficient APK by Reducing Size and Improving PerformanceGenerating efficient APK by Reducing Size and Improving Performance
Generating efficient APK by Reducing Size and Improving Performance
 
Android Apps Development Basic
Android Apps Development BasicAndroid Apps Development Basic
Android Apps Development Basic
 

Similar to Lec005 android start_program

Android introduction
Android introductionAndroid introduction
Android introductionPingLun Liao
 
01 04 - android set up and creating an android project
01  04 - android set up and creating an android project01  04 - android set up and creating an android project
01 04 - android set up and creating an android projectSiva Kumar reddy Vasipally
 
Android Workshop
Android WorkshopAndroid Workshop
Android WorkshopJunda Ong
 
Android Development project
Android Development projectAndroid Development project
Android Development projectMinhaj Kazi
 
Android Jumpstart Jfokus
Android Jumpstart JfokusAndroid Jumpstart Jfokus
Android Jumpstart JfokusLars Vogel
 
Android Development
Android DevelopmentAndroid Development
Android Developmentmclougm4
 
Android application development
Android application developmentAndroid application development
Android application developmentslidesuren
 
Android Development Made Easy - With Sample Project
Android Development Made Easy - With Sample ProjectAndroid Development Made Easy - With Sample Project
Android Development Made Easy - With Sample ProjectJoemarie Amparo
 
Synapseindia android apps introduction hello world
Synapseindia android apps introduction hello worldSynapseindia android apps introduction hello world
Synapseindia android apps introduction hello worldTarunsingh198
 
android training_material ravy ramio
android training_material ravy ramioandroid training_material ravy ramio
android training_material ravy ramioslesulvy
 
Android application development workshop day1
Android application development workshop   day1Android application development workshop   day1
Android application development workshop day1Borhan Otour
 
Part 2 android application development 101
Part 2 android application development 101Part 2 android application development 101
Part 2 android application development 101Michael Angelo Rivera
 
Android studio
Android studioAndroid studio
Android studioAndri Yabu
 
Android App development and test environment, Understaing android app structure
Android App development and test environment, Understaing android app structureAndroid App development and test environment, Understaing android app structure
Android App development and test environment, Understaing android app structureVijay Rastogi
 
Day: 2 Environment Setup for Android Application Development
Day: 2 Environment Setup for Android Application DevelopmentDay: 2 Environment Setup for Android Application Development
Day: 2 Environment Setup for Android Application DevelopmentAhsanul Karim
 

Similar to Lec005 android start_program (20)

Android introduction
Android introductionAndroid introduction
Android introduction
 
01 04 - android set up and creating an android project
01  04 - android set up and creating an android project01  04 - android set up and creating an android project
01 04 - android set up and creating an android project
 
Android Workshop
Android WorkshopAndroid Workshop
Android Workshop
 
Android Development project
Android Development projectAndroid Development project
Android Development project
 
Android Jumpstart Jfokus
Android Jumpstart JfokusAndroid Jumpstart Jfokus
Android Jumpstart Jfokus
 
Android Development
Android DevelopmentAndroid Development
Android Development
 
Android application development
Android application developmentAndroid application development
Android application development
 
Android Development Made Easy - With Sample Project
Android Development Made Easy - With Sample ProjectAndroid Development Made Easy - With Sample Project
Android Development Made Easy - With Sample Project
 
Android Lab
Android LabAndroid Lab
Android Lab
 
Synapseindia android apps introduction hello world
Synapseindia android apps introduction hello worldSynapseindia android apps introduction hello world
Synapseindia android apps introduction hello world
 
Google Android
Google AndroidGoogle Android
Google Android
 
Intro to Android Programming
Intro to Android ProgrammingIntro to Android Programming
Intro to Android Programming
 
android training_material ravy ramio
android training_material ravy ramioandroid training_material ravy ramio
android training_material ravy ramio
 
Android application development workshop day1
Android application development workshop   day1Android application development workshop   day1
Android application development workshop day1
 
Part 2 android application development 101
Part 2 android application development 101Part 2 android application development 101
Part 2 android application development 101
 
Android studio
Android studioAndroid studio
Android studio
 
Android App development and test environment, Understaing android app structure
Android App development and test environment, Understaing android app structureAndroid App development and test environment, Understaing android app structure
Android App development and test environment, Understaing android app structure
 
PPT Companion to Android
PPT Companion to AndroidPPT Companion to Android
PPT Companion to Android
 
Day: 2 Environment Setup for Android Application Development
Day: 2 Environment Setup for Android Application DevelopmentDay: 2 Environment Setup for Android Application Development
Day: 2 Environment Setup for Android Application Development
 
Android Development Basics
Android Development BasicsAndroid Development Basics
Android Development Basics
 

More from Eyad Almasri

Chapter 2 / E-Commerce: Mechanisms, Infrastructures, and Tools – Technology o...
Chapter 2 / E-Commerce: Mechanisms, Infrastructures, and Tools – Technology o...Chapter 2 / E-Commerce: Mechanisms, Infrastructures, and Tools – Technology o...
Chapter 2 / E-Commerce: Mechanisms, Infrastructures, and Tools – Technology o...Eyad Almasri
 
Chapter 8 / Electronic Payment
Chapter 8 / Electronic  PaymentChapter 8 / Electronic  Payment
Chapter 8 / Electronic PaymentEyad Almasri
 
Chapter 7/Social Computing
Chapter 7/Social ComputingChapter 7/Social Computing
Chapter 7/Social ComputingEyad Almasri
 
Chapter 6/Mobile Commerce and Ubiquitous Computing Technology of E-Business
Chapter 6/Mobile Commerce  and Ubiquitous Computing Technology of E-BusinessChapter 6/Mobile Commerce  and Ubiquitous Computing Technology of E-Business
Chapter 6/Mobile Commerce and Ubiquitous Computing Technology of E-BusinessEyad Almasri
 
Chapter 5/Innovative EC Systems: From E-Government to E-Learning, Collaborat...
Chapter 5/Innovative EC Systems: From E-Government to  E-Learning, Collaborat...Chapter 5/Innovative EC Systems: From E-Government to  E-Learning, Collaborat...
Chapter 5/Innovative EC Systems: From E-Government to E-Learning, Collaborat...Eyad Almasri
 
Chapter 4/B2B E-Commerce – Technology of E-Business
Chapter 4/B2B E-Commerce  – Technology of E-BusinessChapter 4/B2B E-Commerce  – Technology of E-Business
Chapter 4/B2B E-Commerce – Technology of E-BusinessEyad Almasri
 
Chapter 3/Retailing In Electronic Commerce: Products and Services Technology...
Chapter 3/Retailing In Electronic Commerce:  Products and Services Technology...Chapter 3/Retailing In Electronic Commerce:  Products and Services Technology...
Chapter 3/Retailing In Electronic Commerce: Products and Services Technology...Eyad Almasri
 
Chapter 1/ Overview of Electronic Commerce Technology of E-Business
Chapter 1/ Overview of Electronic Commerce Technology of E-BusinessChapter 1/ Overview of Electronic Commerce Technology of E-Business
Chapter 1/ Overview of Electronic Commerce Technology of E-BusinessEyad Almasri
 
الوحدة الثامنة - مقدمة عن أمن المعلومات - مهارات الحاسوب
الوحدة الثامنة - مقدمة عن أمن المعلومات - مهارات الحاسوبالوحدة الثامنة - مقدمة عن أمن المعلومات - مهارات الحاسوب
الوحدة الثامنة - مقدمة عن أمن المعلومات - مهارات الحاسوبEyad Almasri
 
الوحدة السابعة - شبكة الأنترنت - مهارات الحاسوب
الوحدة السابعة - شبكة الأنترنت - مهارات الحاسوبالوحدة السابعة - شبكة الأنترنت - مهارات الحاسوب
الوحدة السابعة - شبكة الأنترنت - مهارات الحاسوبEyad Almasri
 
الوحدة السادسة - شبكات الحاسوب - مهارات الحاسوب
الوحدة السادسة - شبكات الحاسوب - مهارات الحاسوبالوحدة السادسة - شبكات الحاسوب - مهارات الحاسوب
الوحدة السادسة - شبكات الحاسوب - مهارات الحاسوبEyad Almasri
 
الوحدة الرابعة - كيفية كتابة الاوراق البحثية بإستخدام طريقة APA
الوحدة الرابعة - كيفية كتابة الاوراق البحثية بإستخدام طريقة APA الوحدة الرابعة - كيفية كتابة الاوراق البحثية بإستخدام طريقة APA
الوحدة الرابعة - كيفية كتابة الاوراق البحثية بإستخدام طريقة APA Eyad Almasri
 
الوحدة الخامسة - كيفية تحضير عرض البوربوينت
الوحدة الخامسة - كيفية تحضير عرض البوربوينتالوحدة الخامسة - كيفية تحضير عرض البوربوينت
الوحدة الخامسة - كيفية تحضير عرض البوربوينتEyad Almasri
 
الوحدة الثالثة - البحث عن المعلومات في الانترنت والمصادر الالكترونية -مهارات ...
الوحدة الثالثة - البحث عن المعلومات في الانترنت والمصادر الالكترونية -مهارات ...الوحدة الثالثة - البحث عن المعلومات في الانترنت والمصادر الالكترونية -مهارات ...
الوحدة الثالثة - البحث عن المعلومات في الانترنت والمصادر الالكترونية -مهارات ...Eyad Almasri
 
الوحدة الثانية - مكونات الحاسوب المادية والبرمجية -مهارات الحاسوب
الوحدة الثانية - مكونات الحاسوب المادية والبرمجية -مهارات الحاسوبالوحدة الثانية - مكونات الحاسوب المادية والبرمجية -مهارات الحاسوب
الوحدة الثانية - مكونات الحاسوب المادية والبرمجية -مهارات الحاسوبEyad Almasri
 
أنظمة جامعة فلسطين المقدمه للطلاب - بوابة الطالب 2018
أنظمة جامعة فلسطين المقدمه للطلاب - بوابة الطالب 2018أنظمة جامعة فلسطين المقدمه للطلاب - بوابة الطالب 2018
أنظمة جامعة فلسطين المقدمه للطلاب - بوابة الطالب 2018Eyad Almasri
 
أنظمة جامعة فلسطين المقدمه للطلاب - نظام اليوبينار
أنظمة جامعة فلسطين المقدمه للطلاب - نظام اليوبينارأنظمة جامعة فلسطين المقدمه للطلاب - نظام اليوبينار
أنظمة جامعة فلسطين المقدمه للطلاب - نظام اليوبينارEyad Almasri
 
مكونات الحاسوب
مكونات الحاسوبمكونات الحاسوب
مكونات الحاسوبEyad Almasri
 

More from Eyad Almasri (18)

Chapter 2 / E-Commerce: Mechanisms, Infrastructures, and Tools – Technology o...
Chapter 2 / E-Commerce: Mechanisms, Infrastructures, and Tools – Technology o...Chapter 2 / E-Commerce: Mechanisms, Infrastructures, and Tools – Technology o...
Chapter 2 / E-Commerce: Mechanisms, Infrastructures, and Tools – Technology o...
 
Chapter 8 / Electronic Payment
Chapter 8 / Electronic  PaymentChapter 8 / Electronic  Payment
Chapter 8 / Electronic Payment
 
Chapter 7/Social Computing
Chapter 7/Social ComputingChapter 7/Social Computing
Chapter 7/Social Computing
 
Chapter 6/Mobile Commerce and Ubiquitous Computing Technology of E-Business
Chapter 6/Mobile Commerce  and Ubiquitous Computing Technology of E-BusinessChapter 6/Mobile Commerce  and Ubiquitous Computing Technology of E-Business
Chapter 6/Mobile Commerce and Ubiquitous Computing Technology of E-Business
 
Chapter 5/Innovative EC Systems: From E-Government to E-Learning, Collaborat...
Chapter 5/Innovative EC Systems: From E-Government to  E-Learning, Collaborat...Chapter 5/Innovative EC Systems: From E-Government to  E-Learning, Collaborat...
Chapter 5/Innovative EC Systems: From E-Government to E-Learning, Collaborat...
 
Chapter 4/B2B E-Commerce – Technology of E-Business
Chapter 4/B2B E-Commerce  – Technology of E-BusinessChapter 4/B2B E-Commerce  – Technology of E-Business
Chapter 4/B2B E-Commerce – Technology of E-Business
 
Chapter 3/Retailing In Electronic Commerce: Products and Services Technology...
Chapter 3/Retailing In Electronic Commerce:  Products and Services Technology...Chapter 3/Retailing In Electronic Commerce:  Products and Services Technology...
Chapter 3/Retailing In Electronic Commerce: Products and Services Technology...
 
Chapter 1/ Overview of Electronic Commerce Technology of E-Business
Chapter 1/ Overview of Electronic Commerce Technology of E-BusinessChapter 1/ Overview of Electronic Commerce Technology of E-Business
Chapter 1/ Overview of Electronic Commerce Technology of E-Business
 
الوحدة الثامنة - مقدمة عن أمن المعلومات - مهارات الحاسوب
الوحدة الثامنة - مقدمة عن أمن المعلومات - مهارات الحاسوبالوحدة الثامنة - مقدمة عن أمن المعلومات - مهارات الحاسوب
الوحدة الثامنة - مقدمة عن أمن المعلومات - مهارات الحاسوب
 
الوحدة السابعة - شبكة الأنترنت - مهارات الحاسوب
الوحدة السابعة - شبكة الأنترنت - مهارات الحاسوبالوحدة السابعة - شبكة الأنترنت - مهارات الحاسوب
الوحدة السابعة - شبكة الأنترنت - مهارات الحاسوب
 
الوحدة السادسة - شبكات الحاسوب - مهارات الحاسوب
الوحدة السادسة - شبكات الحاسوب - مهارات الحاسوبالوحدة السادسة - شبكات الحاسوب - مهارات الحاسوب
الوحدة السادسة - شبكات الحاسوب - مهارات الحاسوب
 
الوحدة الرابعة - كيفية كتابة الاوراق البحثية بإستخدام طريقة APA
الوحدة الرابعة - كيفية كتابة الاوراق البحثية بإستخدام طريقة APA الوحدة الرابعة - كيفية كتابة الاوراق البحثية بإستخدام طريقة APA
الوحدة الرابعة - كيفية كتابة الاوراق البحثية بإستخدام طريقة APA
 
الوحدة الخامسة - كيفية تحضير عرض البوربوينت
الوحدة الخامسة - كيفية تحضير عرض البوربوينتالوحدة الخامسة - كيفية تحضير عرض البوربوينت
الوحدة الخامسة - كيفية تحضير عرض البوربوينت
 
الوحدة الثالثة - البحث عن المعلومات في الانترنت والمصادر الالكترونية -مهارات ...
الوحدة الثالثة - البحث عن المعلومات في الانترنت والمصادر الالكترونية -مهارات ...الوحدة الثالثة - البحث عن المعلومات في الانترنت والمصادر الالكترونية -مهارات ...
الوحدة الثالثة - البحث عن المعلومات في الانترنت والمصادر الالكترونية -مهارات ...
 
الوحدة الثانية - مكونات الحاسوب المادية والبرمجية -مهارات الحاسوب
الوحدة الثانية - مكونات الحاسوب المادية والبرمجية -مهارات الحاسوبالوحدة الثانية - مكونات الحاسوب المادية والبرمجية -مهارات الحاسوب
الوحدة الثانية - مكونات الحاسوب المادية والبرمجية -مهارات الحاسوب
 
أنظمة جامعة فلسطين المقدمه للطلاب - بوابة الطالب 2018
أنظمة جامعة فلسطين المقدمه للطلاب - بوابة الطالب 2018أنظمة جامعة فلسطين المقدمه للطلاب - بوابة الطالب 2018
أنظمة جامعة فلسطين المقدمه للطلاب - بوابة الطالب 2018
 
أنظمة جامعة فلسطين المقدمه للطلاب - نظام اليوبينار
أنظمة جامعة فلسطين المقدمه للطلاب - نظام اليوبينارأنظمة جامعة فلسطين المقدمه للطلاب - نظام اليوبينار
أنظمة جامعة فلسطين المقدمه للطلاب - نظام اليوبينار
 
مكونات الحاسوب
مكونات الحاسوبمكونات الحاسوب
مكونات الحاسوب
 

Recently uploaded

CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceanilsa9823
 
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceanilsa9823
 
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝soniya singh
 
9892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x79892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x7Pooja Nehwal
 
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...wyqazy
 
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...Niamh verma
 
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,Pooja Nehwal
 

Recently uploaded (7)

CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
 
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
 
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝
 
9892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x79892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x7
 
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...
 
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...
 
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
 

Lec005 android start_program

  • 1. Lec 5 – Creating Your First Application Eyad M. AlMassri
  • 2. Using AndroidStudio –basic Android • Lets do a “Hello World Project” • Start up AndroidStudio (assume you have installed with Android support)
  • 3. Hello World Project – Android Studio 1. App name, package 2. Target 3. Activity type 4. Name of Activity 5. Now finish to setup
  • 5. Hello World Project---basic folders &files Folders • Number of folders and files are auto- generated when you create a project File of note AndroidManifest.xml – like a ship’s manifest it tells us the components of our app
  • 6. Project basics…. Android Studio • manifests/AndroidManifest.xml: an XML file describing the application being built and what components – activities, services, etc. – are being supplied by that application • java/: holds the Java source code for the application
  • 7. res/: contains layout files drawable =icons menus values = static files like strings.xml Gradle/: an Ant script for compiling the application and installing it on the device (integrated with IDE—don’t see it there) default.properties: a property file used by the compiler res/ – contains multiple xml files and other resources used by application
  • 8. Project basics…. Android Studio •Gradle Scripts/: an gradle script for compiling the application and installing it on the device (integrated with IDE—don’t see it there) –gradle.properties: a property file used by the compiler
  • 9. Manifest file – AndroidManifest.xml • an XML file describing the application being built and what components – activities, services, etc. – are being supplied by that application • Initially created by IDE • must declare all activities, services, broadcast receivers and content provider of the application. • must contain the required permissions for the application. – For example if the application requires network access it must be specified here.
  • 10. Hello World Project – AndroidManifest.xml Manifest file <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloworldandroid" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloWorldAndroid" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="4" /> </manifest> Here have intent created for This acitivity that is associated with launch of application Define Application –see res/strings.xml –see res/drawable-resolution/icon.png
  • 12. Interface ---2 options • Do it with XML file(s) – Many modern frameworks whether for mobile programming like Android or iOS or for other platforms have gone to specifying GUI (graphical user interface) elements in static XML files rather than programming source code (like java). – The reason –it allows separation of the look (view) from how the code works (model and controller). Have you ever heard of Model View Controller –it is a famous software engineering framework that programmers try to achieve in their software systems. • Do it with Java code. – This has similarities if you have created desktop GUI Java applications
  • 13. OPTION 1: The Layout with XML • We are going to discuss a specific resource in the res folder res/layout/activity_main.xml
  • 14. The Layout-the interface • res/layout/main.xml = contains layout for interface <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> The above will create an interface in vertical (versus portrait) mode that fills the parent Both in width and write and wraps and content as necessary.
  • 15. The Layout-the interface • res/layout/main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > • android:orientation = vertical (versus portrait) mode. • that fills the parent both in width and write and wraps and content as necessary. Landscape Vertical
  • 16. Hello World Project Layout file <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> This adds a “TextView” to the interface and this has the text referenced by @string/hello in it. Creating a Layout file—2 options 1) Do it manually –type it in 2) For Layouts corresponding to GUIs You have a drag and drop option we will learn about in a later lecture
  • 17. Using IDE to Visually Create XML file • Visual creation of XML file • AndroidStudio: New-> Layout resource file – Select layout (root) – &qualifiers as needed – drag & drop
  • 18. Visually Creating XML interface • I dragged and dropped an EditText view and a Button. Below I show you the corresponding code. res/layout/main2.xml <?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:text="@string/hello" android:id="@+id/editText1" android:inputType="textMultiLine" android:layout_width="169dp" android:layout_height="115dp" android:layout_x="11dp" android:layout_y="20dp"></EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:layout_x="27dp" android:layout_y="146dp"></Button> </AbsoluteLayout>
  • 19. LETS LOOK AT THE “MAIN” CLASS It is an Activity Activity is a class in the Android sdk (android.app.Activity) It represents us now the main application itself—we will learn more about what this means in a near future lecture –remember we are at the beginning!
  • 20. Create an Activity with simple text Interface ……first • An Android user interface is composed of hierarchies of objects called Views. • A View is a drawable object used as an element in your UI layout, such as a button, image, or – In the next code will create a text label. Each of these objects is a subclass of the View class and the subclass that handles text is TextView.
  • 21. Hello World Project – here we are creating interface with programatically/code create a TextView with the class constructor, which accepts an Android Context instance as its parameter. •Context is a handle to the system •provides services like resolving resources, •obtaining access to databases and preferences, etc. The Activity class inherits from Context, and because your HelloAndroid class is a subclass of Activity, it is also a Context. So, you can pass this as your Context reference to the TextView. text content is set with setText(CharSequence) pass the TextView to setContentView() in order to display it as the content for the Activity UI.
  • 22. Hello World Project – here we are creating interface with programatically/code Helloworld.java package com.teach.helloworld; import android.app.Activity; import android.os.Bundle; public class helloworld extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello, Android.....Lynne"); setContentView(tv); } }
  • 23. What we just learned • You will have a class created for you that is your android application, i.e. “ helloworld” and it extends Activity • Your application is an Activity and that also descends from Content • Context = class that gives us access to System • provides services like resolving resources, • obtaining access to databases and preferences, etc • .constructor needs Context instance (we
  • 24. What we just learned • View = is a GUI element, there are different kinds like the TextView we are using
  • 25. Alter your Activity to Use an XML layout file • package com.example.helloandroid; import android.app.Activity; import android.os.Bundle; public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } Now going to change the code so it uses instead an XML file for its intervace This line of code says use the xml layout file called main.xml that is pointed to inside your R.java file in memory
  • 26. Interfaces ---XML-based layout • Previous HelloWorld example has interface created in java code. • ALTERNATIVE: XML-based layout files. Example that duplicates previous. • <?xml version="1.0" encoding="utf-8"?> <LinearLayout……….> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/hello"/> Above located in res/layout/main.xml Here is our main.xml file that will be used and do the same thing as our previous java code –show a TextView
  • 27. XML interface • <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="hello"/> – xmlns:android XML namespace declaration that tells the Android tools that you are going to refer to common attributes defined in the Android namespace. The outermost tag in every Android layout file must have this attribute. – android:layout_width This attribute defines how much of the available width on the screen this View should consume. As it's the only View so you want it to take up the entire screen, which is what a value of "fill_parent" Lets understand the XML Tag used to describe our TextView
  • 28. XML interface • android:layout_height This is just like android:layout_width, except that it refers to available screen height. • android:text This sets the text that the TextView should display. In this example, you use a string resource instead of a hard-coded string value. The hello string is defined in the res/values/strings.xml file.
  • 29. MORE ON RESOURCES The res directory
  • 30. We mentioned res folder Android Studio
  • 31. res/ directory • res/layout/ : contains one or more xml files that define interfaces • res/values/strings.xml : file where you specify strings (constants) that you can use in your program
  • 32. • res/drawable-* : gives file use for different resolutions like icon.jpg. *=hdpi (high), mdpi(medium), ldpi (low). Depends on device what mode it can/may be running in. • res/menu/ : contains one or more xml files for menus res/ directory
  • 33. EVENT HANDLING So we have an interface what can we do with it. Event Handling = code that responds when “events” happen. For GUI elements we usually have events associated with them. For example, a button has the event of hitting the button. res/ directory
  • 34. Widget : Views that have events • For a list of the widgets provided by Android, see the android.widget package. • Some Examples – Button – CheckBox – DatePicker – EditText – ImageView – SearchView There are more ---here are a few.
  • 35. Event Handling • Decide what Widgets who’s events to process 3 steps: 1)Decide what events to respond to 2)Create a listener to respond to each event 3)Register the listener to the corresponding widget
  • 36. • Define an event listener and register it with the View. – View.OnClickListener (for handling "clicks" on a View), View.OnTouchListener (for handling touch screen events in a View), and View.OnKeyListener (for handling device key presses within a View) • http://developer.android.com/guid e/topics/ui/ui-events.html details more Event Handling
  • 37. Lets add a Button to a program- based interface • Step 1: Add button • Step 2: Register Event Handler • TWO OPTIONS – separate class to handle event(s), OR have the Activity containing the button do the event handling • Step 3: Implement Event Handler…for a Button means implementing the View.OnClickListener interface
  • 38. Event handling done by Activity itself –one option • Here code to handle is inside Activity itself • public class ExampleActivity extends Activity implements OnClickListener { protected void onCreate(Bundle savedValues) { ... Button button = (Button)findViewById(R.id.corky); button.setOnClickListener(this); } // Implement the OnClickListener callback public void onClick(View v) { // do something when the button is clicked } ... } This option is okay only if the event handling code is simple and you will not reuse it ever ---if the code is longer or will reuse make a separate class
  • 39. Event Handling - here have a SEPARATE class EVENT HANDLING CODE in separate object mCorkyListner // Create an anonymous implementation of OnClickListener private OnClickListener mCorkyListener = new OnClickListener() { public void onClick(View v) { // do something when the button is clicked } }; //Now inside your Activity class protected void onCreate(Bundle savedValues) { ... // STEP 1: Capture our button from layout Button button = (Button)findViewById(R.id.corky); // STEP 2: Register the onClick listener with the implementation above button.setOnClickListener(mCorkyListener); ... } Better for readability and reuse
  • 41. Running Your Android Application in the Emulator • Now you can run the MyFirstAndroidApp project using the following steps found on the next few slides.
  • 42. Running Your Android Application in the Emulator (Cont’d)1. With the Run/Debug Configuration named app already selected, click the Run icon ( ) on the toolbar. – Here is the app configuration selected:
  • 43. Running Your Android Application in the Emulator (Cont’d) 2. You are now prompted to Choose a running device. – The default emulator you launched for the previous example should be listed as a running device. – If it is not already running, select Launch emulator and choose the appropriate AVD if it is not already selected. – Then click OK.
  • 44. Running Your Android Application in the Emulator (Cont’d)
  • 45. Running Your Android Application in the Emulator (Cont’d) 3. If not already started, the Android emulator starts up, which might take a moment. 4. Unlock the emulator if it is locked. 5. The application starts, as shown in the figure on the next slide.
  • 46. Running Your Android Application in the Emulator (Cont’d)
  • 47. Running Your Android Application in the Emulator (Cont’d) 6. Click the Back button in the emulator to end the application, or click Home to suspend it. 7. Click the All Apps button found in the Favorites tray to browse all installed applications from the All Apps screen.
  • 48. Running Your Android Application in the Emulator (Cont’d) 8. Your screen should now present all the applications installed on the device. – Click the My First Android App icon to launch the application again.
  • 49. Running Your Android Application in the Emulator (Cont’d)
  • 50. Running code • TIP: Emulator can take a long time to load at first----be patient and keep it up---just re-run after changes and won’t have to relaunch emulator, will just load up new app. • Look if you have Intell Virtualization speed up, check out GPU and snapshot options – search online for current info on these tips