SlideShare a Scribd company logo
1 of 12
Download to read offline
Creating	
  your	
  first	
  app	
  (	
  Quiz)	
  
In	
  this	
  tutorial,	
  you	
  will	
  build	
  your	
  first	
  application	
  for	
  Android	
  
mobile	
  phones.	
  	
  You	
  probably	
  won’t understand everything that
you are doing, and you may feel lost just going through the
steps. But going through the steps is enough for now. The
details will be explained later.
Our	
  first	
  application	
  is	
  a	
  simple	
  Quiz.	
  Users	
  will	
  be	
  prompted	
  to	
  
answer	
  questions	
  and	
  the	
  app	
  (Quiz)	
  should	
  check	
  if	
  the	
  answer	
  is	
  
correct	
  and	
  display	
  the	
  appropriate	
  message.	
  A	
  quick	
  sketch	
  of	
  
how	
  the	
  app	
  might	
  look	
  like	
  is	
  shown	
  below.	
  
	
  
	
  
	
  
	
  
	
  
	
  
	
  
	
  
	
  
	
   	
  
Check Answer
Next Question
What is your best class?
Check Answer
Next Question
What is your best class?
IST380
Correct Answer
Type answer
click “Check Answer”
Create A New Android Project:
1. Click New in the toolbar.
2. In the window that appears, open
the Android folder, select Android Application
Project, and click Next.
	
  
	
  
	
  
	
  
	
  
	
  
	
  
Android	
  Project	
  File	
  Structure	
  
	
  
	
  
	
  
For	
  more	
  details	
  please	
  see	
  
(http://developer.android.com/guide/developing/projects/index.html)	
  
	
  
Application Fundamentals
There	
  are	
  four	
  different	
  types	
  of	
  components	
  that	
  make	
  an	
  android	
  application.	
  Each	
  
type	
  serves	
  a	
  distinct	
  purpose	
  and	
  has	
  a	
  distinct	
  lifecycle	
  that	
  defines	
  how	
  the	
  
component	
  is	
  created	
  and	
  destroyed.	
  Please	
  see	
  
http://developer.android.com/guide/topics/fundamentals.html	
  
Here	
  are	
  the	
  four	
  types	
  of	
  application	
  components:	
  activities,	
  services,	
  content	
  
providers	
  and	
  broadcast	
  receivers.	
  For	
  the	
  scope	
  of	
  this	
  tutorial,	
  we	
  will	
  focus	
  on	
  
activities.	
  	
  
(From Android Developer Website)
Activities
An activity represents a single screen with a user interface. For example, an email
application might have one activity that shows a list of new emails, another activity to
compose an email, and another activity for reading emails. Although the activities work
together to form a cohesive user experience in the email application, each one is
independent of the others. As such, a different application can start any one of these
activities (if the email application allows it). For example, a camera application can start
the activity in the email application that composes new mail, in
order for the user to share a picture.
	
  
	
  
Creating the user interface
In android, user interface components can be created and
stored in an xml file which can be loaded to an activity at
runtime. For example, the Quiz application will start from
the main activity (Quiz class) which loads the interface
component stored in main.xml.
To create the required interface for our application, expand the folders “res” ,
then “layout” and double click on main.xml.
In the right, you can see a toolbox with many
view and elements you can add to your
interface. Select the “Hello World, Quiz!” text
in the black screen by clicking on it and then
delete it.
Now let’s add a text element to display the
questions. locate a TextView element from the
toolbox and drag it to the black screen. Right
click on it and select layout Width and set it to
“Match parent”. Then right click on it again and
select Show In -> Properties. When the
Properties window opens locate the “Id”
property and change its value
from @+id/TextView01
to @+id/QuestionTextView
Also, locate the Text property and empty its
value.
Drag an EditText element from the toolbox and place it below the TextView you
just added. Change its layout width to match parent and change its id
from @+id/EditText01
to @+id/AnswerText
Drag a button from the toolbox and place it below the AnswerText element you
just added. Change its layout width to match parent and change its id
from @+id/Button01
to @+id/AnswerButton
Change its text to “Check Answer”
Drag a TextView element from the toolbox and place it below the button you just
added. Change its layout width to match parent and change its id
from @+id/TextView01
to @+id/AnswerTextView
Locate the Text property and empty its value.
Drag a button from the toolbox and place it below the AnswerTextView element
you just added. Change its layout width to match parent and change its id
from @+id/Button01
to @+id/QuestionButton
Change its text to “Show Next
Question”.
Now that you are done creating the
interface for the quiz application, your
application should look similar to the
screenshot to right.
Creating Events and Functions
After creating the interface, we need to create functions to:
1. Initialize the questions and answers.
2. Handle use click on the buttons.
In Android, your application usually starts from your main Activity that you named
when creating the project. Our main activity is Quiz class located in the
scr/edu.cgu.ist380.[abedy].quiz folder. Double click the Quiz.java file. You should
see the following code:
This simply means that when this activity is created, it will load “main” as its
content. “main” here refers to the main.xml file we just modified when creating
our interface.
The first thing we need to do is to create properties and variables for :
3. Storing the questions and answers and current question index
4. Change the text for Question and Answer TextViews
5. Reading text on the answer TextEdit.
6. Registering events to the buttons
	
  
To accomplish this, copy and paste the following code before to OnCreate
method :
private int currentQuestion;
private String [] questions;
private String [] answers;
private Button answerButton;
private Button questionButton;
private TextView questionView;
private TextView answerView;
private EditText answerText;
Now, let’s create an init function that initializes
7. the two arrays that holds/stores our questions and answers.
8. Create reference to UI elements
9. Register click events to both buttons
To do this, copy and past the following method under the OnCreate method.
public void init()
{
questions = new String[]{"What is the capital of Egypt?",
"What class are you in right now?"};
answers = new String[]{"Cairo","IST380"};
currentQuestion = -1;
answerButton = (Button)findViewById(R.id.AnswerButton);
questionButton = (Button)findViewById(R.id.QuestionButton);
questionView = (TextView)
findViewById(R.id.QuestionTextView);
answerView = (TextView) findViewById(R.id.AnswerTextView);
answerText = (EditText) findViewById(R.id.AnswerText);
answerButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
checkAnswer();
}});
questionButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
showQuestion();
}});
}
/*
* This method
* 1: increment currentQuestion index
* 2: check if it is equal to the size of the array and rest
if necessary
* 3: display the question at currentQuestion index in
question view
* 4: Empty answer view
*/
public void showQuestion()
{
currentQuestion++;
if(currentQuestion == questions.length)
currentQuestion =0;
questionView.setText(questions[currentQuestion]);
answerView.setText("");
answerText.setText("");
}
/*
* This method return true if the answer equals to correct
answer
* (Ignoring case)
*/
public boolean isCorrect(String answer)
{
return (answer.equalsIgnoreCase(answers[currentQuestion]));
}
/* this method :
* 1: Read the text ( answer) from the answerTextEdit
* 2: call the isCorrect method
* 3: display the appropriate message.
*/
public void checkAnswer()
{
String answer = answerText.getText().toString();
if(isCorrect(answer))
answerView.setText("You're right!");
else
answerView.setText("Sorry, the correct answer is
"+answers[currentQuestion]);
}
Add a call to the init function inside the OnCreate method as shown below
Important Notes:
• How we find elements in the interface by using findViewById method.
• How text can be set and read from TextView and EditText elements.
	
  
Build and Run
To run your Android application, you can either install it on a
compatible android phone or through the Android Emulator
or AVD (Android Virtual Device). For this tutorial, we will run
our quiz application on the emulator.
Create an android emulator (ADT)
On Eclipse, click “Window” -> “Android SDK and ADT
Manager”. Then, click “New” to create an emulator as shown
in the screen below, and start your AVD.
	
  
Running the Quiz Application
On Eclipse, right click on the Quiz project in “Package
Explorer” and select Run As -> Android Application. Select
your AVD from the device list and click run. Your application
will start on the Emulator.
Don’t forget to commit your changes to your repository by
right clicking the Quiz project in “Package Explorer” and
select Team-> Remote -> Push…
	
  
	
  
	
  
	
  
	
  
	
  
	
  
	
  
	
  
	
  
	
  
	
  
	
  
 
Test you knowledge!
Now that you have created your first application, try to :
• Answer some questions and check your answers
• Change the number of questions and answers
• Show the number of questions in the screen
• Find a way to calculate user scores
	
  

More Related Content

What's hot

Android User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAndroid User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAhsanul Karim
 
Android technical quiz app
Android technical quiz appAndroid technical quiz app
Android technical quiz appJagdeep Singh
 
Android MapView and MapActivity
Android MapView and MapActivityAndroid MapView and MapActivity
Android MapView and MapActivityAhsanul Karim
 
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver TutorialAndroid Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver TutorialAhsanul Karim
 
Beta testing guidelines for developer
Beta testing guidelines for developerBeta testing guidelines for developer
Beta testing guidelines for developerKetan Raval
 
MobileAppDev Handout#9
MobileAppDev Handout#9MobileAppDev Handout#9
MobileAppDev Handout#9trupti1976
 
Write an application that draws basic graphical primitives.pptx
Write an application that draws basic graphical primitives.pptxWrite an application that draws basic graphical primitives.pptx
Write an application that draws basic graphical primitives.pptxvishal choudhary
 
Android task manager project presentation
Android task manager project presentationAndroid task manager project presentation
Android task manager project presentationAkhilesh Jaiswal
 
B041130610
B041130610B041130610
B041130610IOSR-JEN
 
Online News Portal System
Online News Portal SystemOnline News Portal System
Online News Portal SystemRajib Roy
 
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
 
android layouts
android layoutsandroid layouts
android layoutsDeepa Rani
 
Synopsis on android application
Synopsis on android applicationSynopsis on android application
Synopsis on android applicationJawed akhtar
 

What's hot (20)

Android User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAndroid User Interface: Basic Form Widgets
Android User Interface: Basic Form Widgets
 
Android technical quiz app
Android technical quiz appAndroid technical quiz app
Android technical quiz app
 
Android MapView and MapActivity
Android MapView and MapActivityAndroid MapView and MapActivity
Android MapView and MapActivity
 
Android Layout.pptx
Android Layout.pptxAndroid Layout.pptx
Android Layout.pptx
 
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver TutorialAndroid Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
 
.Net presentation
.Net presentation.Net presentation
.Net presentation
 
Beta testing guidelines for developer
Beta testing guidelines for developerBeta testing guidelines for developer
Beta testing guidelines for developer
 
MobileAppDev Handout#9
MobileAppDev Handout#9MobileAppDev Handout#9
MobileAppDev Handout#9
 
Write an application that draws basic graphical primitives.pptx
Write an application that draws basic graphical primitives.pptxWrite an application that draws basic graphical primitives.pptx
Write an application that draws basic graphical primitives.pptx
 
Android task manager project presentation
Android task manager project presentationAndroid task manager project presentation
Android task manager project presentation
 
B041130610
B041130610B041130610
B041130610
 
Online News Portal System
Online News Portal SystemOnline News Portal System
Online News Portal System
 
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
 
Cs 6611 mad lab manual
Cs 6611 mad lab manualCs 6611 mad lab manual
Cs 6611 mad lab manual
 
Android web service client
Android web service clientAndroid web service client
Android web service client
 
Quiz
QuizQuiz
Quiz
 
android layouts
android layoutsandroid layouts
android layouts
 
Synopsis on android application
Synopsis on android applicationSynopsis on android application
Synopsis on android application
 
Create New Android Layout
Create New Android LayoutCreate New Android Layout
Create New Android Layout
 
Create New Android Activity
Create New Android ActivityCreate New Android Activity
Create New Android Activity
 

Viewers also liked

Building app profile
Building app profileBuilding app profile
Building app profileTM Forum
 
What Makes Cities Smart?
What Makes Cities Smart?What Makes Cities Smart?
What Makes Cities Smart?TM Forum
 
Music magazine evaluation
Music magazine evaluationMusic magazine evaluation
Music magazine evaluationjoshware
 
Contraception slides
Contraception slidesContraception slides
Contraception slidesJoel Irons
 
Producing empathic responses
Producing empathic responsesProducing empathic responses
Producing empathic responsesShamimi Jamudin
 
PDHPE in Primary Schools
PDHPE in Primary SchoolsPDHPE in Primary Schools
PDHPE in Primary SchoolsJessica Goodier
 
Muscles year 10 Sport Science
Muscles year 10 Sport Science Muscles year 10 Sport Science
Muscles year 10 Sport Science Joel Irons
 
ประวัติส่วนตัว
ประวัติส่วนตัวประวัติส่วนตัว
ประวัติส่วนตัวOomAmm Oom
 
Sdn presentation ux-beers
Sdn presentation ux-beersSdn presentation ux-beers
Sdn presentation ux-beersAnnie Stewart
 
Kumpulan 3 (minggu 3) Sifat Bahasa
Kumpulan 3 (minggu 3) Sifat BahasaKumpulan 3 (minggu 3) Sifat Bahasa
Kumpulan 3 (minggu 3) Sifat BahasaShamimi Jamudin
 
CMM Presentation (Silver+Gold)
CMM Presentation (Silver+Gold)CMM Presentation (Silver+Gold)
CMM Presentation (Silver+Gold)Muhammad Sami Khan
 
QuickPayGroup (Qpay Presentation)
QuickPayGroup (Qpay Presentation)QuickPayGroup (Qpay Presentation)
QuickPayGroup (Qpay Presentation)Muhammad Sami Khan
 
Masalah penguasaan pembelajaran dan cara mengesan kesukaran pembelajaran mimi
Masalah penguasaan pembelajaran dan cara mengesan kesukaran pembelajaran mimiMasalah penguasaan pembelajaran dan cara mengesan kesukaran pembelajaran mimi
Masalah penguasaan pembelajaran dan cara mengesan kesukaran pembelajaran mimiShamimi Jamudin
 
болортуяа
болортуяаболортуяа
болортуяаSoko5-3
 
Minggu 4 (sastera) pembinaan plot
Minggu 4 (sastera) pembinaan plotMinggu 4 (sastera) pembinaan plot
Minggu 4 (sastera) pembinaan plotShamimi Jamudin
 

Viewers also liked (20)

Building app profile
Building app profileBuilding app profile
Building app profile
 
What Makes Cities Smart?
What Makes Cities Smart?What Makes Cities Smart?
What Makes Cities Smart?
 
Perkembangan bahasa
Perkembangan bahasaPerkembangan bahasa
Perkembangan bahasa
 
M5 kriteria pemilihan
M5   kriteria pemilihanM5   kriteria pemilihan
M5 kriteria pemilihan
 
Music magazine evaluation
Music magazine evaluationMusic magazine evaluation
Music magazine evaluation
 
Writing help2
Writing help2Writing help2
Writing help2
 
Contraception slides
Contraception slidesContraception slides
Contraception slides
 
Producing empathic responses
Producing empathic responsesProducing empathic responses
Producing empathic responses
 
PDHPE in Primary Schools
PDHPE in Primary SchoolsPDHPE in Primary Schools
PDHPE in Primary Schools
 
Aterosclerosis
AterosclerosisAterosclerosis
Aterosclerosis
 
Muscles year 10 Sport Science
Muscles year 10 Sport Science Muscles year 10 Sport Science
Muscles year 10 Sport Science
 
ประวัติส่วนตัว
ประวัติส่วนตัวประวัติส่วนตัว
ประวัติส่วนตัว
 
Sdn presentation ux-beers
Sdn presentation ux-beersSdn presentation ux-beers
Sdn presentation ux-beers
 
Kumpulan 3 (minggu 3) Sifat Bahasa
Kumpulan 3 (minggu 3) Sifat BahasaKumpulan 3 (minggu 3) Sifat Bahasa
Kumpulan 3 (minggu 3) Sifat Bahasa
 
CMM Presentation (Silver+Gold)
CMM Presentation (Silver+Gold)CMM Presentation (Silver+Gold)
CMM Presentation (Silver+Gold)
 
QuickPayGroup (Qpay Presentation)
QuickPayGroup (Qpay Presentation)QuickPayGroup (Qpay Presentation)
QuickPayGroup (Qpay Presentation)
 
Jantung
JantungJantung
Jantung
 
Masalah penguasaan pembelajaran dan cara mengesan kesukaran pembelajaran mimi
Masalah penguasaan pembelajaran dan cara mengesan kesukaran pembelajaran mimiMasalah penguasaan pembelajaran dan cara mengesan kesukaran pembelajaran mimi
Masalah penguasaan pembelajaran dan cara mengesan kesukaran pembelajaran mimi
 
болортуяа
болортуяаболортуяа
болортуяа
 
Minggu 4 (sastera) pembinaan plot
Minggu 4 (sastera) pembinaan plotMinggu 4 (sastera) pembinaan plot
Minggu 4 (sastera) pembinaan plot
 

Similar to Create yourfirstandroidapppdf

Android tutorials7 calculator
Android tutorials7 calculatorAndroid tutorials7 calculator
Android tutorials7 calculatorVlad Kolesnyk
 
Ios actions and outlets
Ios actions and outletsIos actions and outlets
Ios actions and outletsveeracynixit
 
Ios actions and outlets
Ios actions and outletsIos actions and outlets
Ios actions and outletsveeracynixit
 
Hello android example.
Hello android example.Hello android example.
Hello android example.Rahul Rana
 
MAD mobile application development you can learn from here , we perform all c...
MAD mobile application development you can learn from here , we perform all c...MAD mobile application development you can learn from here , we perform all c...
MAD mobile application development you can learn from here , we perform all c...harshalpatil183931
 
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAndroid User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAhsanul Karim
 
Gui builder
Gui builderGui builder
Gui builderlearnt
 
Build an App with JavaScript & jQuery
Build an App with JavaScript & jQuery Build an App with JavaScript & jQuery
Build an App with JavaScript & jQuery Aaron Lamphere
 
bawawjspdx082117
bawawjspdx082117bawawjspdx082117
bawawjspdx082117Thinkful
 
Web app-la-jan-2
Web app-la-jan-2Web app-la-jan-2
Web app-la-jan-2Thinkful
 
How to Detect a Click Outside a React Component.pptx
How to Detect a Click Outside a React Component.pptxHow to Detect a Click Outside a React Component.pptx
How to Detect a Click Outside a React Component.pptxBOSC Tech Labs
 
Software engineering modeling lab lectures
Software engineering modeling lab lecturesSoftware engineering modeling lab lectures
Software engineering modeling lab lecturesmarwaeng
 
Build an App with JavaScript and jQuery - LA - July 18
Build an App with JavaScript and jQuery - LA - July 18Build an App with JavaScript and jQuery - LA - July 18
Build an App with JavaScript and jQuery - LA - July 18Thinkful
 
Baawjsajq109
Baawjsajq109Baawjsajq109
Baawjsajq109Thinkful
 
IOS Swift language 1st Tutorial
IOS Swift language 1st TutorialIOS Swift language 1st Tutorial
IOS Swift language 1st TutorialHassan A-j
 
AndroidLab_IT.pptx
AndroidLab_IT.pptxAndroidLab_IT.pptx
AndroidLab_IT.pptxAhmedKedir9
 

Similar to Create yourfirstandroidapppdf (20)

Android tutorials7 calculator
Android tutorials7 calculatorAndroid tutorials7 calculator
Android tutorials7 calculator
 
Ios actions and outlets
Ios actions and outletsIos actions and outlets
Ios actions and outlets
 
Ios actions and outlets
Ios actions and outletsIos actions and outlets
Ios actions and outlets
 
Hello android example.
Hello android example.Hello android example.
Hello android example.
 
MAD mobile application development you can learn from here , we perform all c...
MAD mobile application development you can learn from here , we perform all c...MAD mobile application development you can learn from here , we perform all c...
MAD mobile application development you can learn from here , we perform all c...
 
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAndroid User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
 
Gui builder
Gui builderGui builder
Gui builder
 
Build an App with JavaScript & jQuery
Build an App with JavaScript & jQuery Build an App with JavaScript & jQuery
Build an App with JavaScript & jQuery
 
Deck 6-456 (1)
Deck 6-456 (1)Deck 6-456 (1)
Deck 6-456 (1)
 
bawawjspdx082117
bawawjspdx082117bawawjspdx082117
bawawjspdx082117
 
Web app-la-jan-2
Web app-la-jan-2Web app-la-jan-2
Web app-la-jan-2
 
Introduction
IntroductionIntroduction
Introduction
 
How to Detect a Click Outside a React Component.pptx
How to Detect a Click Outside a React Component.pptxHow to Detect a Click Outside a React Component.pptx
How to Detect a Click Outside a React Component.pptx
 
Lab1-android
Lab1-androidLab1-android
Lab1-android
 
Software engineering modeling lab lectures
Software engineering modeling lab lecturesSoftware engineering modeling lab lectures
Software engineering modeling lab lectures
 
Build an App with JavaScript and jQuery - LA - July 18
Build an App with JavaScript and jQuery - LA - July 18Build an App with JavaScript and jQuery - LA - July 18
Build an App with JavaScript and jQuery - LA - July 18
 
Baawjsajq109
Baawjsajq109Baawjsajq109
Baawjsajq109
 
IOS Swift language 1st Tutorial
IOS Swift language 1st TutorialIOS Swift language 1st Tutorial
IOS Swift language 1st Tutorial
 
AndroidLab_IT.pptx
AndroidLab_IT.pptxAndroidLab_IT.pptx
AndroidLab_IT.pptx
 
Android app development guide for freshers by ace web academy
Android app development guide for freshers  by ace web academyAndroid app development guide for freshers  by ace web academy
Android app development guide for freshers by ace web academy
 

Recently uploaded

Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 

Recently uploaded (20)

Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Create yourfirstandroidapppdf

  • 1. Creating  your  first  app  (  Quiz)   In  this  tutorial,  you  will  build  your  first  application  for  Android   mobile  phones.    You  probably  won’t understand everything that you are doing, and you may feel lost just going through the steps. But going through the steps is enough for now. The details will be explained later. Our  first  application  is  a  simple  Quiz.  Users  will  be  prompted  to   answer  questions  and  the  app  (Quiz)  should  check  if  the  answer  is   correct  and  display  the  appropriate  message.  A  quick  sketch  of   how  the  app  might  look  like  is  shown  below.                         Check Answer Next Question What is your best class? Check Answer Next Question What is your best class? IST380 Correct Answer Type answer click “Check Answer”
  • 2. Create A New Android Project: 1. Click New in the toolbar. 2. In the window that appears, open the Android folder, select Android Application Project, and click Next.              
  • 3. Android  Project  File  Structure         For  more  details  please  see   (http://developer.android.com/guide/developing/projects/index.html)     Application Fundamentals There  are  four  different  types  of  components  that  make  an  android  application.  Each   type  serves  a  distinct  purpose  and  has  a  distinct  lifecycle  that  defines  how  the   component  is  created  and  destroyed.  Please  see   http://developer.android.com/guide/topics/fundamentals.html   Here  are  the  four  types  of  application  components:  activities,  services,  content   providers  and  broadcast  receivers.  For  the  scope  of  this  tutorial,  we  will  focus  on   activities.     (From Android Developer Website) Activities An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others. As such, a different application can start any one of these activities (if the email application allows it). For example, a camera application can start the activity in the email application that composes new mail, in order for the user to share a picture.     Creating the user interface In android, user interface components can be created and stored in an xml file which can be loaded to an activity at runtime. For example, the Quiz application will start from the main activity (Quiz class) which loads the interface component stored in main.xml.
  • 4. To create the required interface for our application, expand the folders “res” , then “layout” and double click on main.xml. In the right, you can see a toolbox with many view and elements you can add to your interface. Select the “Hello World, Quiz!” text in the black screen by clicking on it and then delete it. Now let’s add a text element to display the questions. locate a TextView element from the toolbox and drag it to the black screen. Right click on it and select layout Width and set it to “Match parent”. Then right click on it again and select Show In -> Properties. When the Properties window opens locate the “Id” property and change its value from @+id/TextView01 to @+id/QuestionTextView Also, locate the Text property and empty its value. Drag an EditText element from the toolbox and place it below the TextView you just added. Change its layout width to match parent and change its id from @+id/EditText01 to @+id/AnswerText Drag a button from the toolbox and place it below the AnswerText element you just added. Change its layout width to match parent and change its id from @+id/Button01 to @+id/AnswerButton Change its text to “Check Answer” Drag a TextView element from the toolbox and place it below the button you just added. Change its layout width to match parent and change its id from @+id/TextView01 to @+id/AnswerTextView Locate the Text property and empty its value. Drag a button from the toolbox and place it below the AnswerTextView element you just added. Change its layout width to match parent and change its id
  • 5. from @+id/Button01 to @+id/QuestionButton Change its text to “Show Next Question”. Now that you are done creating the interface for the quiz application, your application should look similar to the screenshot to right.
  • 6. Creating Events and Functions After creating the interface, we need to create functions to: 1. Initialize the questions and answers. 2. Handle use click on the buttons. In Android, your application usually starts from your main Activity that you named when creating the project. Our main activity is Quiz class located in the scr/edu.cgu.ist380.[abedy].quiz folder. Double click the Quiz.java file. You should see the following code: This simply means that when this activity is created, it will load “main” as its content. “main” here refers to the main.xml file we just modified when creating our interface. The first thing we need to do is to create properties and variables for : 3. Storing the questions and answers and current question index 4. Change the text for Question and Answer TextViews 5. Reading text on the answer TextEdit. 6. Registering events to the buttons  
  • 7. To accomplish this, copy and paste the following code before to OnCreate method : private int currentQuestion; private String [] questions; private String [] answers; private Button answerButton; private Button questionButton; private TextView questionView; private TextView answerView; private EditText answerText; Now, let’s create an init function that initializes 7. the two arrays that holds/stores our questions and answers. 8. Create reference to UI elements 9. Register click events to both buttons To do this, copy and past the following method under the OnCreate method. public void init() { questions = new String[]{"What is the capital of Egypt?", "What class are you in right now?"}; answers = new String[]{"Cairo","IST380"}; currentQuestion = -1; answerButton = (Button)findViewById(R.id.AnswerButton); questionButton = (Button)findViewById(R.id.QuestionButton); questionView = (TextView) findViewById(R.id.QuestionTextView); answerView = (TextView) findViewById(R.id.AnswerTextView); answerText = (EditText) findViewById(R.id.AnswerText); answerButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { checkAnswer(); }}); questionButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { showQuestion(); }}); } /* * This method
  • 8. * 1: increment currentQuestion index * 2: check if it is equal to the size of the array and rest if necessary * 3: display the question at currentQuestion index in question view * 4: Empty answer view */ public void showQuestion() { currentQuestion++; if(currentQuestion == questions.length) currentQuestion =0; questionView.setText(questions[currentQuestion]); answerView.setText(""); answerText.setText(""); } /* * This method return true if the answer equals to correct answer * (Ignoring case) */ public boolean isCorrect(String answer) { return (answer.equalsIgnoreCase(answers[currentQuestion])); } /* this method : * 1: Read the text ( answer) from the answerTextEdit * 2: call the isCorrect method * 3: display the appropriate message. */ public void checkAnswer() { String answer = answerText.getText().toString(); if(isCorrect(answer)) answerView.setText("You're right!"); else answerView.setText("Sorry, the correct answer is "+answers[currentQuestion]); } Add a call to the init function inside the OnCreate method as shown below
  • 9. Important Notes: • How we find elements in the interface by using findViewById method. • How text can be set and read from TextView and EditText elements.  
  • 10. Build and Run To run your Android application, you can either install it on a compatible android phone or through the Android Emulator or AVD (Android Virtual Device). For this tutorial, we will run our quiz application on the emulator. Create an android emulator (ADT) On Eclipse, click “Window” -> “Android SDK and ADT Manager”. Then, click “New” to create an emulator as shown in the screen below, and start your AVD.  
  • 11. Running the Quiz Application On Eclipse, right click on the Quiz project in “Package Explorer” and select Run As -> Android Application. Select your AVD from the device list and click run. Your application will start on the Emulator. Don’t forget to commit your changes to your repository by right clicking the Quiz project in “Package Explorer” and select Team-> Remote -> Push…                          
  • 12.   Test you knowledge! Now that you have created your first application, try to : • Answer some questions and check your answers • Change the number of questions and answers • Show the number of questions in the screen • Find a way to calculate user scores