Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside.

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 6 Overview Programming Tutorial 3: Sending/Receiving SMS Messages.
Advertisements

Android User Interface
Programming with Android: Widgets and Events Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
User Interface Classes.  Design Principles  Views & Layouts  Event Handling  Menus  Dialogs.
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Button click handlers Maarten Pennings. Introduction An activity has one or more views – view is also known as widget or control – examples include Button,
@2010 Mihail L. Sichitiu1 Android Introduction Hello Views Part 2.
Android Form Elements. Views Provide common UI functionality Form elements: text area, button, radio button, checkbox, dropdown list, etc. Date and time.
Android Development (Basics)
Lesson 8 Creating Forms with JavaScript
Introducing the Sudoku Example
Mobile Programming Lecture 2 Layouts, Widgets, Toasts, and Event Handling.
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.
ASP.NET Controls. Slide 2 Lecture Overview Identify the types of controls supported by ASP.NET and the differences between them.
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.
INTRODUCTION TO ANDROID. Slide 2 Application Components An Android application is made of up one or more of the following components Activities We will.
Programming Mobile Applications with Android September, Albacete, Spain Jesus Martínez-Gómez.
Android Boot Camp for Developers Using Java, 3E
Programming with Android: Layouts, Widgets and Events Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Presented By: Muhammad Tariq Software Engineer Android Training course.
Android Hello World 1. Click on Start and type eclipse into the textbox 2.
Field Trip #32 Digital Alarm Clock By Keith Lynn.
New Perspectives on XML, 2nd Edition Tutorial 9B1 USING XML AS A DATA SOURCE TUTORIAL 9B.
Copyright© Jeffrey Jongko, Ateneo de Manila University Basic Views and Layouts.
ANDROID – DRAWING IMAGES – SIMPLE EXAMPLE IN INTERFACE AND EVENT HANDLING L. Grewe.
User Interface Android Club Agenda Button OnClickListener OnLongClickListener ToggleButton Checkbox RatingBar AutoCompleteTextView.
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.
Form Components and Elements
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
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.
HTML FORMS The TEXT Object Presented By: Ankit Gupta.
Designing user interfaces using: Simple views 1. Views Basic views – TextView – EditText – Button – ImageButton – CheckBox – ToggleButton – RadioButton.
GUI Components 1 Fall 2012 CS2302: Programming Principles.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
User Interface Layout Interaction. EventsEvent Handlers/Listeners Interacting with a user.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
Events. Slide 2©SoftMoore Consulting Events Events are generated when a user interacts with the view objects of an application. Examples –button clicked–
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
Android 基本 I/O. 基本 I/O 介面元件 在此節中主要介紹常見的 I/O 使用者介 面元件 – Button, TextView, 以及 EditText , 學習者可以學會: – Android 的視窗表單設計 res/layout/main.xml – Android SDK –
HTML FORM. Form HTML Forms are used to select different kinds of user input. HTML forms are used to pass data to a server. A form can contain input elements.
Http :// developer. android. com / guide / topics / fundamentals. html.
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.
Cosc 5/4730 Support design library. Support Design library Adds (API 9+) back support to a number of 5.0 lollipop widgets and material design pieces –
Instructor Notes To the instructor: This slide set has been prepared with both the highlights from the student text as well as notes from the text. The.
XHTML Forms.
Chapter 9 Programming Based on Events
Android N Amanquah.
Android Programming Lecture 3.
Android – Event Handling
Mobile Applications (Android Programming)
Picasso Revisted.
Android Introduction Hello Views Part 2.
Software Engineering for Internet Applications
Chapter 3: Coding the GUI Programmatically, Layout Managers
Android Programming Lecture 4
Chapter 9: Fragments.
CIS 470 Mobile App Development
Chapter 2: Model View Controller, GUI Components, Events
GUI Components.
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
Exception Handling and Event Handling
CIS 470 Mobile App Development
Android Developer Fundamentals V2
Mobile Programmming Dr. Mohsin Ali Memon.
User Interface Screen Elements
Android Sensor Programming
Presentation transcript:

Handling View Events

Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside the onCreate method will bind the "activity_main.xml" layout to the *MainActivity.java*.

How It Works To handle the events fired by each view, you first have to programmatically locate the view that you created during the onCreate() event. You do so using the findViewById() method (belonging to the Activity base class), supplying it with the ID of the view:

Botton //---Button view--- Button btnOpen = (Button) findViewById(R.id.btnOpen); The unique id of the botton is accessed as parameter. The setOnClickListener() method registers a callback to be invoked later when the view is clicked: btnOpen.setOnClickListener(new view.OnClickListener() { public void onClick(View v) { DisplayToast(“You have clicked the Open button”); } }); The onClick() method is called when the view is clicked.

Check Box To determine the state of the CheckBox, you have to typecast the argument of the onClick() method to a CheckBox and then check its isChecked() method to see if it is checked: CheckBox checkBox = (CheckBox) findViewById(R.id.chkAutosave); checkBox.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (((CheckBox)v).isChecked()) DisplayToast(“CheckBox is checked”); else DisplayToast(“CheckBox is unchecked”); } });

Radio Button For the RadioButton, you need to use the setOnCheckedChangeListener() method on the RadioGroup to register a callback to be invoked when the checked RadioButton changes in this group: //---RadioButton--- RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rdbGp1); radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton rb1 = (RadioButton) findViewById(R.id.rdb1); if (rb1.isChecked()) { DisplayToast(“Option 1 checked!”); } else { DisplayToast(“Option 2 checked!”); } });

When a RadioButton is selected, the onCheckedChanged() method is fired. Within it, you locate individual RadioButtons and then call their isChecked() method to determine which RadioButton is selected. Alternatively, the onCheckedChanged() method contains a second argument that contains a unique identifier of the RadioButton selected. The ToggleButton works just like the CheckBox.

Edit Text findViewById(int) access the edit text. EditText edtText=(EditText)findViewById(R.id.editText1); Use the getText() method to get the text entered by the user. String enteredText=editText1.getText().toString(); Can set listener for various action on EditText. Example:- To change focus away from view. The event,listener and handler associated with this event are FocusChange,OnfocusChangeListener & onFocusChange().

Reminder for various event, listener & handler of views Event ObjectEvent ListenerEvent Handler FocusChangeOnFocusChangeListeneronFocusChange() CheckedChangeOnCheckedChangeListeneronCheckedChange() ItemClickOnItemClickLicteneronItemClick() Focus away from view Checkbox and radio group List view

So far, to handle the events on the views, you first had to get a reference to the view and then register a callback to handle the event. There is another way to handle view events. Using the Button as an example, you can add an attribute called onClick to it: <Button android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:onClick=”btnSaved_clicked”/>

The onClick attribute specifies the click event of the button. The value of this attribute is the name of the event handler. Therefore, to handle the click event of the button, you simply need to create a method called btnSaved_clicked, as shown in the example.