Android Apps Development for Mobile and Tablet Device (Level I) Lesson 2

Similar documents
MAD ASSIGNMENT NO 2. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

TextView Control. EditText Control. TextView Attributes. android:id - This is the ID which uniquely identifies the control.

Mobile Application Development Lab [] Simple Android Application for Native Calculator. To develop a Simple Android Application for Native Calculator.

MAD ASSIGNMENT NO 3. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

Create Parent Activity and pass its information to Child Activity using Intents.

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 4. Workshop

Tip Calculator. xmlns:tools=" android:layout_width="match_parent"

ANDROID PROGRAMS DAY 3

Fragments. Lecture 11


1. Location Services. 1.1 GPS Location. 1. Create the Android application with the following attributes. Application Name: MyLocation

PENGEMBANGAN APLIKASI PERANGKAT BERGERAK (MOBILE)

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Android Application Development. By : Shibaji Debnath

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

Basic GUI elements - exercises

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Android Programs Day 5

EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

CSE 660 Lab 3 Khoi Pham Thanh Ho April 19 th, 2015

Fragment Example Create the following files and test the application on emulator or device.

<uses-permission android:name="android.permission.internet"/>

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Switching UIs

Applied Cognitive Computing Fall 2016 Android Application + IBM Bluemix (Cloudant NoSQL DB)

Intents. Your first app assignment

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

1. Simple List. 1.1 Simple List using simple_list_item_1

TextView. A label is called a TextView. TextViews are typically used to display a caption TextViews are not editable, therefore they take no input

Software Practice 3 Before we start Today s lecture Today s Task Team organization

Q.1 Explain the dialog and also explain the Demonstrate working dialog in android.

ELET4133: Embedded Systems. Topic 15 Sensors

South Africa

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

Android Apps Development for Mobile Game Lesson 5

SD Module-1 Android Dvelopment

Produced by. Mobile Application Development. Eamonn de Leastar

CSE 660 Lab 7. Submitted by: Arumugam Thendramil Pavai. 1)Simple Remote Calculator. Server is created using ServerSocket class of java. Server.

android:layout_margintop="5dp" > <EditText android:layout_width="210dp"

Embedded Systems Programming - PA8001

ITU- FAO- DOA- TRCSL. Training on. Innovation & Application Development for E- Agriculture. Shared Preferences

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Hello World. Lesson 1. Android Developer Fundamentals. Android Developer Fundamentals. Layouts, and. NonCommercial

Workshop. 1. Create a simple Intent (Page 1 2) Launch a Camera for Photo Taking

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Android Beginners Workshop

Android UI Development


Android Workshop: Model View Controller ( MVC):

Our First Android Application

Mobile Software Development for Android - I397

Arrays of Buttons. Inside Android

M.A.D ASSIGNMENT # 2 REHAN ASGHAR BSSE 15126

Create a local SQL database hosting a CUSTOMER table. Each customer includes [id, name, phone]. Do the work inside Threads and Asynctasks.

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Adaptation of materials: dr Tomasz Xięski. Based on presentations made available by Victor Matos, Cleveland State University.

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Data Persistence. Chapter 10

Android - JSON Parser Tutorial

Programming with Android: Layouts, Widgets and Events. Dipartimento di Scienze dell Informazione Università di Bologna

Vienos veiklos būsena. Theory

Tutorial: Setup for Android Development

Manifest.xml. Activity.java

Time Picker trong Android

Mobile Programming Lecture 2. Layouts, Widgets, Toasts, and Event Handling

Chapter 8 Positioning with Layouts

Android - Widgets Tutorial

Android HelloWorld - Example. Tushar B. Kute,

Android Apps Development for Mobile Game Lesson Create a simple paint brush that allows user to draw something (Page 11 13)

else if(rb2.ischecked()) {

Android SQLite Database Tutorial - CRUD Operations

Practical 1.ListView example

Dynamically Create Admob Banner and Interstitial Ads

M.A.D Assignment # 1

Overview. What are layouts Creating and using layouts Common layouts and examples Layout parameters Types of views Event listeners

Group B: Assignment No 8. Title of Assignment: To verify the operating system name and version of Mobile devices.

IPN-ESCOM Application Development for Mobile Devices. Extraordinary. A Web service, invoking the SOAP protocol, in an Android application.

Android/Java Lightning Tutorial JULY 30, 2018

TPCT s College of Engineering, Osmanabad. Laboratory Manual SDL-II. Mobile Application Development (Android) For. Third Year Students (CSE)

Programming with Android: Introduction. Layouts. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

使用 TensorFlow 設計矩陣乘法計算並轉移執行在 Android 上 建國科技大學資管系 饒瑞佶 2017/8

StoppUhr. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start1"

Meniu. Create a project:

Starting Another Activity Preferences

By The Name of Allah. The Islamic University of Gaza Faculty of Engineering Computer Department Final Exam. Mobile Computing

Accelerating Information Technology Innovation

EMBEDDED SYSTEMS PROGRAMMING Android Services

Java & Android. Java Fundamentals. Madis Pink 2016 Tartu

Fragments. Lecture 10

Android Layout Types

// MainActivity.java ; Noah Spenser; Senior Design; Diabetic Breathalyzer

PROGRAMMING APPLICATIONS DECLARATIVE GUIS

Simple Currency Converter

android:orientation="horizontal" android:layout_margintop="30dp"> <Button android:text="button2"

Thread. A Thread is a concurrent unit of execution. The thread has its own call stack for methods being invoked, their arguments and local variables.

Android Navigation Drawer for Sliding Menu / Sidebar

android-espresso #androidespresso

Interface ใน View class ประกอบด วย

Android CardView Tutorial

Transcription:

Workshop 1. Compare different layout by using Change Layout button (Page 1 5) Relative Layout Linear Layout (Horizontal) Linear Layout (Vertical) Frame Layout 2. Revision on basic programming skill - control structure (Page 6 9) do while loop while loop for loop 3. Use the basic component on TextView, Button, Toast, Checkbox and Radio Button. Then identify the different InputType on EditText (Page 10 16) text number phone textmultiline textcapcharacters textpassword textautocorrect 4. It s time for you to create the first game: Guess Number. The app should pick a secret number (0 9) and let the user guess what number it is. User is only allowed to input number in the text field. If the guess number is too large or too smaller, the program should provide a hint. If the guess number is correct, the program should congratulate the user. (Page 17 20) X4-XT-CDP-0251-A @ Peter Lo 2015

1. Android Layout 1.1 Relative Layout 1. Create the Android application with the following attributes. Application Name: MyLayout Project Name: Package Name: MyLayout com.example.mylayout 2. By using drag and drop, add a button after the text Hello World. 3. Right click the layout XML activity_main.xml and select Open With Text Editor. The XML code for the layout is listed as below. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <TextView android:id="@+id/textview1" android:layout_width="wrap_content" X4-XT-CDP-0251-A @ Peter Lo 2015 1

android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_torightof="@+id/textview1" android:text="button" /> </RelativeLayout> 1.2 Horizontal Linear Layout 1. Right click the layout, and then select Change Layout. 2. Select LinearLayout (Horizontal) in the Change Layout dialog. X4-XT-CDP-0251-A @ Peter Lo 2015 2

3. The layout will be changed as follow. 4. The XML code for activity_main.xml is listed below: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/linearlayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" /> </LinearLayout> X4-XT-CDP-0251-A @ Peter Lo 2015 3

1.3 Vertical Linear Layout 1. Change the layout to LinearLayout (Vertical). 2. The XML code for activity_main.xml is listed below: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/linearlayout2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" /> </LinearLayout> X4-XT-CDP-0251-A @ Peter Lo 2015 4

1.4 Frame Layout 1. Change the layout to FrameLayout. 2. The XML code for activity_main.xml is listed below: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/framelayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" /> </FrameLayout> X4-XT-CDP-0251-A @ Peter Lo 2015 5

2. Programming Control Structure 2.1 The do while Loop 1. Create a new Android application with the following attributes. Application Name: MyDoWhileLoop Project Name: Package Name: MyDoWhileLoop com.example.mydowhileloop 2. Open the source file "MainActivity.java" and modify the code as follow: package com.example.mydowhileloop; import android.os.bundle; import android.app.activity; import android.view.menu; import android.widget.toast; public class MainActivity extends Activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); int i = 1; do { Toast.makeText(this, "i = " + i, Toast.LENGTH_LONG).show(); while ( i++ < 3 ); public boolean oncreateoptionsmenu(menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; X4-XT-CDP-0251-A @ Peter Lo 2015 6

3. Save and execute the app. How many messages do you obtain? 2.2 The while Loop 1. Create a new Android application with the following attributes. Application Name: MyWhileLoop Project Name: Package Name: MyWhileLoop com.example.mywhileloop 2. Open the source file "MainActivity.java" and modify the code as follow: package com.example.mywhileloop; import android.os.bundle; import android.app.activity; import android.view.menu; import android.widget.toast; public class MainActivity extends Activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); int i = 1; while ( i++ < 3 ) { Toast.makeText(this, "i = " + i, Toast.LENGTH_LONG).show(); public boolean oncreateoptionsmenu(menu menu) { X4-XT-CDP-0251-A @ Peter Lo 2015 7

// Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; 3. Save and execute the app. How many messages do you obtain? 2.3 The for Loop 1. Create a new Android application with the following attributes. Application Name: MyForLoop Project Name: Package Name: MyForLoop com.example.myforloop 4. Open the source file "MainActivity.java" and modify the code as follow: package com.example.myforloop; import android.os.bundle; import android.app.activity; import android.view.menu; import android.widget.toast; public class MainActivity extends Activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); int i = 1; X4-XT-CDP-0251-A @ Peter Lo 2015 8

for ( i = 1; i < 3; i++) { Toast.makeText(this, "i = " + i, Toast.LENGTH_LONG).show(); public boolean oncreateoptionsmenu(menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; 5. Save and execute the app. How many messages do you obtain? X4-XT-CDP-0251-A @ Peter Lo 2015 9

3. Simple Component 3.1 Simple Components 1. Create a new Android application with the following attributes. Application Name: MySampleUI Project Name: Package Name: MySampleUI com.example.mysampleui 2. Change the Text of TextView1 to Please input something:. 3. Drag a Plain Text to the layout. 4. Drag a Button to the layout and change the text to Submit. X4-XT-CDP-0251-A @ Peter Lo 2015 10

5. Double click the source file "MainActivity.java" under "src" folder to open the Java editor, and then modify the source code as follow: package com.example.mysampleui; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class MainActivity extends Activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // Attach the listener to the button Button mbutton = (Button) findviewbyid(r.id.button1); mbutton.setonclicklistener(new OnClickListener() { public void onclick(view arg0) { EditText medittext = (EditText) findviewbyid(r.id.edittext1); String UserInput = medittext.gettext().tostring(); // Display the message Toast.makeText(getApplicationContext(), UserInput, Toast.LENGTH_SHORT).show(); ); public boolean oncreateoptionsmenu(menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; X4-XT-CDP-0251-A @ Peter Lo 2015 11

6. Save and execute the app, can you observe the change after press the button? 7. Then change the InputType (such as text, number, phone, textmultiline, textcapcharacters, textpassword, textautocorrect) in the EditText and understand their behavior: 3.2 Radio Button 1. Create a new Android application with the following attributes. Application Name: MyRadionButton Project Name: Package Name: MyRadionButton com.example.myradionbutton 2. Drag a Radio Button Group to the layout. Then change the text for the radio buttons to Option 0, Option 1 and Option 2. 3. Modify the source file "MainActivity.java" as follow. package com.example.myradionbutton; import android.os.bundle; import android.app.activity; import android.view.menu; X4-XT-CDP-0251-A @ Peter Lo 2015 12

import android.widget.radiobutton; import android.widget.radiogroup; import android.widget.toast; public class MainActivity extends Activity { RadioGroup radiogroup1; RadioButton radio0, radio1, radio2; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // Locate the radio button radio0 = (RadioButton) findviewbyid(r.id.radio0); radio1 = (RadioButton) findviewbyid(r.id.radio1); radio2 = (RadioButton) findviewbyid(r.id.radio2); // Locate the radio group and Attach Listener radiogroup1 = (RadioGroup) findviewbyid(r.id.radiogroup1); radiogroup1.setoncheckedchangelistener(new RadioGroup.OnCheckedChangeListener() { public void oncheckedchanged(radiogroup group, int checkedid) { // Check whether the radio button is checked if (checkedid == radio0.getid()) { Toast.makeText(getApplicationContext(), radio0.gettext(), Toast.LENGTH_LONG).show(); else if (checkedid == radio1.getid()) { Toast.makeText(getApplicationContext(), radio1.gettext(), Toast.LENGTH_LONG).show(); else if (checkedid == radio2.getid()) { Toast.makeText(getApplicationContext(), radio2.gettext(), Toast.LENGTH_LONG).show(); ); X4-XT-CDP-0251-A @ Peter Lo 2015 13

public boolean oncreateoptionsmenu(menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; 4. Execute the app and select different radio button to observe the result. 3.3 Checkbox 1. Create a new Android application with the following attributes. Application Name: MyCheckBox Project Name: Package Name: MyCheckBox com.example.mycheckbox 2. Drag two checkbox to the layout, and then rename the text for the checkbox to Checkbox 1 and Checkbox 2. X4-XT-CDP-0251-A @ Peter Lo 2015 14

3. Drag a button to the layout. 4. Modify the source code for the file "MainActivity.java" as follow: package com.example.mycheckbox; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.checkbox; import android.widget.toast; public class MainActivity extends Activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // Attach the listener to the button Button button1 = (Button) findviewbyid(r.id.button1); button1.setonclicklistener(new OnClickListener() { public void onclick(view arg0) { StringBuilder result = new StringBuilder(); result.append("selected Items:"); // Check whether check box 1 is selected CheckBox checkbox1 = (CheckBox) findviewbyid(r.id.checkbox1); if(checkbox1.ischecked()) { X4-XT-CDP-0251-A @ Peter Lo 2015 15

result.append("\ncheckbox1"); // Check whether check box 2 is selected CheckBox checkbox2 = (CheckBox) findviewbyid(r.id.checkbox2); if (checkbox2.ischecked()) { result.append("\ncheckbox2"); // Displaying the message Toast.makeText(getApplicationContext(), result.tostring(), Toast.LENGTH_LONG).show(); ); public boolean oncreateoptionsmenu(menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; 5. Execute the app, select the check box and press the button to observe the result. X4-XT-CDP-0251-A @ Peter Lo 2015 16

4. Simple Game 4.1 Guess Number 1. Create the Android application with the following attributes. Application Name: MyGuessNumber Project Name: Package Name: MyGuessNumber com.example.myguessnumber 2. Select the Text View, and then change the text to I have a number between 0-9, please guess. 3. Drag the picture magic.png into the drawable-hdpi folder. Select Copy in the File Operation dialog. X4-XT-CDP-0251-A @ Peter Lo 2015 17

4. Add an ImageView to the layout. Select the image magic and press [OK] to continue. 5. Add a number text field to the layout 6. Add a button to the layout and change the text to Guess : X4-XT-CDP-0251-A @ Peter Lo 2015 18

7. Open the source file "MainActivity.java" and modify as follow: package com.example.myguessnumber; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.toast; import java.util.random; public class MainActivity extends Activity { public Button GuessButton; public int RandomNumber; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // Generate the Random Number when program start Random randomize = new Random(); RandomNumber = randomize.nextint(10); // Create the Guess button Listener GuessButton = (Button) findviewbyid(r.id.button1); GuessButton.setOnClickListener(new OnClickListener() { public void onclick(view arg0) { // Retrieve the User Input EditText txtuserinput = (EditText) findviewbyid(r.id.edittext1); int UserInput = Integer.parseInt( txtuserinput.gettext().tostring()); // Check and display the result if (UserInput == RandomNumber) Toast.makeText(MainActivity.this, "Correct", Toast.LENGTH_LONG).show(); X4-XT-CDP-0251-A @ Peter Lo 2015 19

else if (UserInput > RandomNumber) Toast.makeText(MainActivity.this, "Too Large", Toast.LENGTH_LONG).show(); else if (UserInput < RandomNumber) Toast.makeText(MainActivity.this, "Too Small", Toast.LENGTH_LONG).show(); ); public boolean oncreateoptionsmenu(menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; 8. Save and execute the app, and try to guess the number. X4-XT-CDP-0251-A @ Peter Lo 2015 20