Android – Event Handling

Slides:



Advertisements
Similar presentations
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Advertisements

Basic Functionality in Android. Functionality in Android Events in Java – mouse related mouse clicked button down or up mouse entered – many others key.
Button click handlers Maarten Pennings. Introduction An activity has one or more views – view is also known as widget or control – examples include Button,
@2011 Mihail L. Sichitiu1 Android Introduction Hello Views Part 1.
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
Android Development (Basics)
Android Application Development Tutorial. Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet.
Android development the first app. Andoid vs iOS which is better? Short answer: neither Proponents on both sides For an iOS side, see this article on.
PROG Mobile Java Application Development PROG Mobile Java Application Development Event Handling Creating Menus.
Mobile Programming Lecture 2 Layouts, Widgets, Toasts, and Event Handling.
Favorite Twitter® Searches App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Simplify! The Android User Interface
Tip Calculator App Building an Android App with Java © by Pearson Education, Inc. All Rights Reserved.
Chapter 5 Creating User Interfaces GOALS and OBJECTIVES Begin our application by creating our user interface. More than one way to create a user interface.
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
1/29/ Android Programming: FrameLayout By Dr. Ramji M. Makwana Professor and Head, Computer Engineering Department A.D. Patel.
Chapter 2 The Android User Interface. Objectives  In this chapter, you learn to:  Develop a user interface using the TextView, ImageView, and Button.
Cosc 5/4730 Android App Widgets. App Widgets App Widgets are miniature application views that can be embedded in other applications (such as the Home.
Create Navigation Drawer Team 2 Zhong Wang Jiaming Dong Philip Wu Lingduo Kong.
Android Boot Camp for Developers Using Java, 3E
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 2: Simplify! The Android User Interface.
Field Trip #32 Digital Alarm Clock By Keith Lynn.
ANDROID – DRAWING IMAGES – SIMPLE EXAMPLE IN INTERFACE AND EVENT HANDLING L. Grewe.
Android Boot Camp Demo Application – Part 1. Development Environment Set Up Download and install Java Development Kit (JDK) Download and unzip Android.
Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside.
Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.
Video Games list lab 6  At the end of this lab you will be expected to know:  What Views, View Groups, Layouts, and Widgets are and how they relate to.
ANDROID – A FIRST PROGRAM L. Grewe Using AndroidStudio –basic Android  Lets do a “Hello World Project”  Start up AndroidStudio (assume you have installed.
Mobile Programming Lecture 7 Dialogs, Menus, and SharedPreferences.
MOBILE COMPUTING D10K-7D02 MC05: Android UI Design Dr. Setiawan Hadi, M.Sc.CS. Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran.
Copyright© Jeffrey Jongko, Ateneo de Manila University Deconstructing HelloWorld.
Android - SQLite Database 12/10/2015. Introduction SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with.
Designing user interfaces using: Simple views 1. Views Basic views – TextView – EditText – Button – ImageButton – CheckBox – ToggleButton – RadioButton.
User Interface Layout Interaction. EventsEvent Handlers/Listeners Interacting with a user.
Android Alert Dialog. Alert Dialog Place Button to open the dialog. public class MainActivity extends ActionBarActivity { private static Button button_sbm;
Events. Slide 2©SoftMoore Consulting Events Events are generated when a user interacts with the view objects of an application. Examples –button clicked–
Mobile Programming Lecture 4 Resources, Selection, Activities, Intents.
Android 基本 I/O. 基本 I/O 介面元件 在此節中主要介紹常見的 I/O 使用者介 面元件 – Button, TextView, 以及 EditText , 學習者可以學會: – Android 的視窗表單設計 res/layout/main.xml – Android SDK –
ANDROID LAYOUTS AND WIDGETS. Slide 2 Introduction Parts of the Android screen Sizing widgets and fonts Layouts and their characteristics Buttons, checkboxes.
Java for android Development Nasrullah Khan. Using instanceof in Android Development the classes such as Button, TextView, and CheckBox, which represent.
Chapter 2: Simplify! The Android User Interface
Google VR (gvr) CardBoard and DayDream With OpenGL
Android Programming - Features
Mobile Development Workshop
several communicating screens
GUI Programming Fundamentals
Android Widgets 1 7 August 2018
Creation of an Android App By Keith Lynn
ITEC535 – Mobile Programming
Android Introduction Camera.
Android 16: Input Events Kirk Scott.
Picasso Revisted.
Chapter 3: Coding the GUI Programmatically, Layout Managers
EE 422C Java FX.
UNIT 08 그림책 만들기 2/2 로봇 SW 콘텐츠 교육원 조용수.
BMI Android Application will take weight and height from the users to calculate Body Mass Index (BMI) with the information, whether user is underweight,
Android Topics Custom ArrayAdapters Creating an Event Listener
Android Developer Fundamentals V2
Constructors, GUI’s(Using Swing) and ActionListner
Tonga Institute of Higher Education
ארועים ומאזינים android.
Mobile Programming Gestures in Android.
Android Project Structure, App Resources and Event Handling
Mobile Programmming Dr. Mohsin Ali Memon.
Cosc 4730 An Introduction.
CMPE419 Mobile Application Development
Activities and Fragments
CS 240 – Advanced Programming Concepts
CIS 470 Mobile App Development
Presentation transcript:

Android – Event Handling L. Grewe

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 Spinner

Event Handling STEP 1: Decide what Widgets who’s events to process STEP 2: Define an event listener STEP 3: Register event listener 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/guide/topics/ui/ui- events.html details more

Lets add a Button to a program-based interface Step 1: Button specified in XML layout we want to process/handle Step 2: Implement Event Handler TWO OPTIONS – separate class to handle event(s), OR have the Activity containing the button do the event handling for a Button means implementing the View.OnClickListener interface Step 3: Register Event Handler

Event handling done by Activity itself 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.button_DO_IT); //STEP 1         button.setOnClickListener(this); //STEP 3 - registration     }     // Implement the OnClickListener callback //STEP 2 –event handler     public void onClick(View v) {       // do something when the button is clicked     }     ... } NOTE: in this example, the Activity is the Event Handler as it is implementing the OnClickListener interface ---often you may do an anonymous inner class instead or a regular class if you want to reuse it

Event Handling - here have a SEPARATE anonymous inner class EVENT HANDLING CODE in separate object mCorkyListner // Create an anonymous implementation of OnClickListener STEP 2 private OnClickListener mDoIT_Listener = 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 3: Register the onClick listener with the implementation above button.setOnClickListener(mDoIT_Listener); ... } Creating instance of anonymous inner class to implement the OnClickListener

Lets do it step by step In AndroidStudio

STEP1: Create a Button Create button using XML GUI interface Set property so label on button is “DO IT” Also, added text view with instructions telling user to hit button

STEP2: Register the main Application Activity as EventHandler for button Grab button associated with its id, store in local variable b. Call setonClickListener(this) public class MainActivity extends Activity implements OnClickListener { int hits =0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b = (Button)findViewById(R.id.button_DO_IT); b.setOnClickListener(this);//SETP 3: register handler } \

STEP2: Register the main Application Activity as EventHandler for button Have the main class “MainActivity” implement OnClickListener for button In method onClick increment hits and use in display string. public class MainActivity extends Activity implements OnClickListener { int hits =0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { //see previous slide…….. } // Implement the OnClickListener callback //STEP 3 –event handler  for button being hit public void onClick(View v) { // do something when the button is clicked  this.hits++; TextView text = (TextView)findViewById(R.id.text_Message); String s = "You hit me " + this.hits; text.setText(s);

Run your code Lets run the previous code.

After running it—before hit button

After hitting button a few times

What can you do with Event Handling ….your imagination is the limit…..

Remember …….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 Spinner

Don’t forget….Intents and Intent Recievers …less coupled ways to have things happen. ….but, that is another lecture

Explore our Website, Book and… Look at our website, book and Android developer websites for examples of event handling