LESSON-10-11_JAVA_EVENTLISTENER.pdf - Android Java File Structure
Document Details
Bulacan State University
Mary Rose C. Columbres
Tags
Summary
This document is a lecture or presentation on Android Java Development, focusing on the file structure, XML-based layout, use of resources, Android toasts, and event listeners, presented by Mary Rose C. Columbres at Bulacan State University.
Full Transcript
Overview of the Java File MARY ROSE C. COLUMBRES Bulacan State University - Sarmiento Campus OBJECTIVES: At the end of this lesson, you should be able to: ▪ Discover what is XML and how will you implement this in your application; ▪ Utilize the Java file struct...
Overview of the Java File MARY ROSE C. COLUMBRES Bulacan State University - Sarmiento Campus OBJECTIVES: At the end of this lesson, you should be able to: ▪ Discover what is XML and how will you implement this in your application; ▪ Utilize the Java file structure in android; ▪ Appreciate the importance of “R” or Resources and its methods; and ▪ Utilize the Android Toast. What is Android XML? XML stands for Extensible Markup Language. In Cellphone, XML data sent in cellphone that formatted using software designer to display text and images. XML is a light-weight language that is used for designing layout and it doesn’t make the layout heavy. XML Syntax Rules XML Syntax Rules XML Tags is Case Sensitive. All tags need to be written in correct case. XML Syntax Rules XML must have one root element What is View Elements in XML? Element is everything that you insert in XML with the start and end tag of the element or simply this is a parent and child of your XML. Elements and View Attributes Every Element and View has attributes that you can apply to manage the properties of each views. Example What is a Java File? A Java class file (with the.class filename extension) containing Java bytecode that can be executed on the Java Virtual Machine (JVM). A Java class file is usually delivered by a Java compiler from Java programming language. UNDERSTANDING THE JAVA FILE STRUCTURE IN ANDROID package com.example.android.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } FindViewById Method private void initializeViews(){ textView = findViewById(R.id.text_view); editText = findViewById(R.id.edit_text); checkBox = findViewById(R.id.check_box); imageView = findViewById(R.id.image_view); } Android Listeners MARY ROSE C. COLUMBRES Bulacan State University - Sarmiento Campus What is an Event Listener? An event listener is an interface in the View class that contains a single callback method. COMMON CALLBACK METHODS onClick() - From View.OnClickListener. This is called when the user touches the item (when in touch mode). onLongClick() - From View.OnLongClickListener. This is called when the user either touches and holds the item (when in touch mode). onKey() - From View.OnKeyListener. This is called when the user is focused on the item and presses or releases a hardware key on the device. onTouch() - From View.OnTouchListener. This is called when the user performs an action qualified as a touch event, including a press, a release, or any movement gesture on the screen onTextChanged() - onTextChangedListener is a method that is called when text is edited or changed inside an EditText What is an Android Toast? A toast gives straightforward feedback message about an operation in a little popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. Toasts naturally vanish after a timeout. What is an Android Toast? The basic Context context = getApplicationContext(); CharSequence text = "Hello toast!"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); Toast Syntax Toast.makeText(context, "message", duration).show(); Substitute the values: Toast.makeText(MainActivity.this, "Details Saved Successfully.", Toast.LENGTH_SHORT).show(); Positioning your Toast Positioning your Toast setGravity() method can be used. public void setGravity (int gravity, int xOffset, int yOffset) Parameters: This method accepts three parameters: Positioning your Toast gravity: This sets the position of the Toast message. Following constants can be used to specify the position of a Toast: 1. TOP 2. BOTTOM 3. LEFT 4. RIGHT 5. CENTER 6. CENTER_HORIZONTAL 7. CENTER_VERTICAL Positioning your Toast xOffset: This is the offset value that tells how much to shift the Toast message horizontally on the x axis. yOffset: This is the offset value that tells how much to shift the Toast message vertically on the y axis. Positioning your Toast Example 1. To display the Toast at the center: toast.setGravity(Gravity.CENTER, 0, 0); 2. To display the Toast at the top, centered horizontally: toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTALLY, 0, 0); 3. To display the Toast at the top, centered horizontally, but 30 pixels down from the top: toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTALLY, 0, 30); 4. To display the Toast at the bottom, rightmost horizontally: toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 0, 0); Quote of the Day