Interaction with Android

Similar documents
Basic GUI elements - exercises

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Class Library java.lang Package. Bok, Jong Soon

Strings in Java String Methods. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings.

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

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

Android UI: Overview

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

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

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

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

ANDROID USER INTERFACE

Programming with Java

CS 1301 Ch 8, Part A

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

EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches


Arrays of Buttons. Inside Android

String. Other languages that implement strings as character arrays

Handout 3 cs180 - Programming Fundamentals Fall 17 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

Android HelloWorld - Example. Tushar B. Kute,

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

CST242 Strings and Characters Page 1

OOP-Lecture Java Loop Controls: 1 Lecturer: Hawraa Sh. You can use one of the following three loops: while Loop do...while Loop for Loop

Java Foundations: Unit 3. Parts of a Java Program

Lab 1 - Setting up the User s Profile UI

More non-primitive types Lesson 06

CS251L REVIEW Derek Trumbo UNM

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

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

JAVASCRIPT BASICS. JavaScript String Functions. Here is the basic condition you have to follow. If you start a string with

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents

Vienos veiklos būsena. Theory

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

5/23/2015. Core Java Syllabus. VikRam ShaRma

Tłumaczenie i adaptacja materiałów: dr Tomasz Xięski. Na podstawie prezentacji udostępnionych przez Victor Matos, Cleveland State University.

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

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

CS 116x Winter 2015 Craig S. Kaplan. Module 09 Text Processing. Topics

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Creating Strings. String Length

Lab 14 & 15: String Handling

Lab 3: Call to Order CSCI 2101 Fall 2018

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

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

Eng. Mohammed Abdualal

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

Chapter 12 Strings and Characters. Dr. Hikmat Jaber

User Interface: Layout. Asst. Prof. Dr. Kanda Runapongsa Saikaew Computer Engineering Khon Kaen University

"Hello" " This " + "is String " + "concatenation"

Introduction to Programming Using Java (98-388)

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Using Java Classes Fall 2018 Margaret Reid-Miller

App Development for Smart Devices. Lec #18: Advanced Topics

Android Beginners Workshop

Let s take a display of HTC Desire smartphone as an example. Screen size = 3.7 inches, resolution = 800x480 pixels.

CHAPTER 6 MOST COMMONLY USED LIBRARIES

EECS 4443 Mobile User Interfaces. More About Layouts. Scott MacKenzie. York University. Overview (Review)

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

Introductory Mobile Application Development

Embedded Systems Programming - PA8001

Figure 2.10 demonstrates the creation of a new project named Chapter2 using the wizard.

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Switching UIs

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

MATHEMATICAL FUNCTIONS CHARACTERS, AND STRINGS. INTRODUCTION IB DP Computer science Standard Level ICS3U

Intents. Your first app assignment

else if(rb2.ischecked()) {

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Index COPYRIGHTED MATERIAL

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

Android Application Model I. CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr.

GUI Design for Android Applications

GUI Widget. Lecture6

A Crash Course to Android Mobile Platform

EECS 4443 Mobile User Interfaces. More About Layouts. Scott MacKenzie. York University

COMP4521 EMBEDDED SYSTEMS SOFTWARE

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

App Development for Smart Devices. Lec #7: Audio, Video & Telephony Try It Out

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

J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS

Programming Android UI. J. Serrat Software Design December 2017

Android Basics. Android UI Architecture. Android UI 1

Chapter 10: Text Processing and More about Wrapper Classes

Programming Techniques

String Theory. Chapter Cut and Paste

Android Application Model I

Java s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"

Lab 3: Call to Order CSCI 2101 Fall 2017

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

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. YÉÇàá. Victor Matos Cleveland State University

M.A.D Assignment # 1

appreciate the difference between a char and a string understand and use the String class methods

USING LIBRARY CLASSES

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

We now start exploring some key elements of the Java programming language and ways of performing I/O

UI (User Interface) overview Supporting Multiple Screens Touch events and listeners

Transcription:

Interaction with Android The Android program can output data by working with the xml files. The main use of the java files is to get some data, make decisions and change the output based on the results of those decisions. To keep the problem simple we will use 2 EditText widgets so 2 things can be a compared A TextView for the output At least 1 Button to fire the code. The following code shows the XML to set up a table layout of rows and columns. The basic objects have been set up in the graphical editor followed by some heavy tweaking of the xml. The xml tags need to be properly closed and nested or the layout will not work. <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android " android:id="@+id/tablelayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchcolumns="1" > <TableRow > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:id="@+id/txtout"/> <TableRow > <EditText android:id="@+id/tleft"> <requestfocus></requestfocus> </EditText> <EditText android:id="@+id/tright" > </EditText> <TableRow> <Button android:text="maths" android:id="@+id/bmaths" android:layout_height="wrap_content" /> </TableLayout>

The EditText Objects are added to the xml and instantiated in the java in the same way as the TextView. The Button is set up in the same way but needs registering with an onclicklistener to be able to act on a click. The following lines appear to throw an error in Android because they are missing some linked objects bmaths =(Button)findViewById(R.id.bMaths); bmaths.setonclicklistener(this); The imports section needs expanding to include a view. import android.view.view; The line that declares that the java program is an activity needs expanding to state that it is also (inherits from) a View.OnClickListener. public class inputoutput extends Activity implements View.OnClickListener This code will ask for an onclick method to be implemented. public void onclick(view v) { This method can then pick up the click event on the Button. public void onclick(view v) { if (v == findviewbyid(r.id.bmaths)) { toutput.settext("ok so far"); This code will output a test line to the TextField toutput.

The data from the EditText objects can be assigned to String variables: String left, right, result; left = eleft.gettext().tostring(); This is not a lot of use if any maths is going to have to take place. Integer.ParseInt can solve that problem. ileft = Integer.parseInt(left); There are similar classes for Float and Double float fright = Float.valueOf(right); double dright = Double.valueOf(right); There is not a llot of call for doing maths on Strings (although the + oerator will concatenate them) but Android has a pile of String manipulation methods to compare Strings or find Strings within other Strings. The following are amongst the most useful: char charat (int index) int compareto (String string) int comparetoignorecase (String string) String copyvalueof (char[] data, int start, int length) boolean endswith (String suffix) int indexof (int c) int length () String replace (char oldchar, char newchar) String substring (int start, int end) public char[] tochararray () String tolowercase () String touppercase () String trim () Returns the character at the specified offset in this string. Returns 0 if two Strings exactly the same. This is a better plan than == which can cause problems A useful variant of the above Copy some or all of a char array into a String Does a String end in a set suffix? Find a char in a String, -1 is returned if it is not present How long is a String? Copies this string replacing occurrences of the specified character with another character. Returns a string containing a subsequence of characters from this string Copies the characters in this string to a character array. Make the String lower case Make the String upper case Trim leading and tailing whitespace

For example, adding up is easy: String left, right; left = eleft.gettext().tostring(); String result = "words"; result = left + right; toutput.settext(result); Some String method examples int num = left.length(); toutput.settext(string.valueof(num)); One more if (left.length()>5) result = left.substring(1,4); result ="too short"; toutput.settext(result); This is the way to go rather than using = with Strings int num = left.compareto(right); if (num == 0) result = "left and right are the same"; result = "left and right are different"; Another if (input.indexof("a")!= -1) txtout.settext("a is at position " + input.indexof("a") + " in " + input); txtout.settext("there is no 'a' in " + input); This is all fine but we cannot be sure that the user has entered a word or a number. The easiest way is to set the input type of the EditText in xml. The settings can be browsed from the properties list of the visual designer. <EditText android:id="@+id/tleft" android:inputtype="number">

Of course the java code can check what a char data type contains. We know that a String is a string of char data so can work from there. String right; String result="data"; char[] letters = right.tochararray(); for (int x=0;x<letters.length;x++) { if(! Character.isDigit(letters[x])) result = "not a number"; The Chararcter class has a number of similar methods including isletter(), isletterordigit() and iswhitespace(),