Android Project Structure, App Resources and Event Handling

Slides:



Advertisements
Similar presentations
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
Advertisements

Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.
Basic, Basic, Basic Android. What are Packages? Page 346 in text Package statement goes before any import statements Indicates that the class declared.
ANDROID PROGRAMMING MODULE 1 – GETTING STARTED
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
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.
Chapter 3 Navigating a Project Goals & Objectives 1.Get familiar with the navigation of the project. How is everything structured? What settings can you.
Chapter 2: Simplify! The Android User Interface
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.
Chapter 2 The Android User Interface. Objectives  In this chapter, you learn to:  Develop a user interface using the TextView, ImageView, and Button.
INTRODUCTION TO ANDROID. Slide 2 Application Components An Android application is made of up one or more of the following components Activities We will.
Android - Broadcast Receivers
Android Boot Camp for Developers Using Java, 3E
Resources. Application Resources Resources are strings, images, and other pieces of application information that are stored and maintained (externalized)
Programming with Android: Layouts, Widgets and Events Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Configuring Android Development Environment Nilesh Singh.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 2: Simplify! The Android User Interface.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
Creating an Example Android App in Android Studio Activity lifecycle & UI Resources.
Android View Stuff. TextViews Display text Display images???
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
Copyright© Jeffrey Jongko, Ateneo de Manila University Deconstructing HelloWorld.
ANDROID APPLICATION DEVELOPMENT. ANDROID DEVELOPMENT DEVELOPER.ANDROID.COM/INDEX.HTML THE OFFICIAL SITE FOR ANDROID DEVELOPERS. PROVIDES THE ANDROID SDK.
TCS Internal Maps. 2 TCS Internal Objective Objective :  MAPS o Integration of Maps.
Events. Slide 2©SoftMoore Consulting Events Events are generated when a user interacts with the view objects of an application. Examples –button clicked–
ANDROID LAYOUTS AND WIDGETS. Slide 2 Introduction Parts of the Android screen Sizing widgets and fonts Layouts and their characteristics Buttons, checkboxes.
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
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 –
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
CHAPTER 1 Part 2 Android Programming. Chapter objectives: Understand how to create a simple Android project Understand how to create a manifest file Understand.
Introduction to Android Chapter 1 1. Objectives Understand what Android is Learn the differences between Java and Android Java Examine the Android project.
Chapter 2: Simplify! The Android User Interface
Lab7 – Appendix.
Introduction to android
Mobile Applications (Android Programming)
Android Mobile Application Development
Mobile Application Development BSCS-7 Lecture # 2
Android N Amanquah.
CS240: Advanced Programming Concepts
GUI Programming Fundamentals
Instructor: Mazhar Hussain
Android – Event Handling
Lecture 8: Graphics By: Eliav Menachi.
S.RENUKADEVI/AP/SCD/ANDROID - Notifications
MAD.
Android Widgets 1 7 August 2018
Creation of an Android App By Keith Lynn
Mobile Application Development BSCS-7 Lecture # 11
Android 16: Input Events Kirk Scott.
Android SDK & App Development
Software Engineering for Internet Applications
Android Programming Lecture 6
CIS 470 Mobile App Development
CMPE419 Mobile Application Development
Android Developer Fundamentals V2 Lesson 1
BMI Android Application will take weight and height from the users to calculate Body Mass Index (BMI) with the information, whether user is underweight,
Activities and Intents
HNDIT2417 Mobile Application Development
CIS 470 Mobile App Development
Android Application Development
Mobile Programming Gestures in Android.
Lecture 8: Graphics By: Eliav Menachi.
Adding Components to Activity
Mobile Programming Dr. Mohsin Ali Memon.
Mobile Programmming Dr. Mohsin Ali Memon.
CMPE419 Mobile Application Development
CMPE419 Mobile Application Development
Mobile Programming Broadcast Receivers.
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

Android Project Structure, App Resources and Event Handling Mobile Programming Android Project Structure, App Resources and Event Handling

Android Projects

Projects Overview A project in Android Studio contains everything that defines your workspace for an app, from source code and assets, to test code and build configurations. When you start a new project, Android Studio creates the necessary structure for all your files and makes them visible in the Project window on the left side of the IDE (click View > Tool Windows > Project). This page provides an overview of the key components inside your project.

Project Files By default, Android Studio displays your project files in the Android view. This view does not reflect the actual file hierarchy on disk, but is organized by modules and file types to simplify navigation. Within each Android app module, files are shown in the following groups: manifests Contains the AndroidManifest.xml file. java Contains the Java source code files, separated by package names, including JUnit test code. res Contains all non-code resources, such as XML layouts, UI strings, and bitmap images, divided into corresponding sub-directories. For more information about all possible resource types, see Providing Resources.

The Android project view To see the actual file structure of the project including all files hidden from the Android view, select Project from the dropdown at the top of the Project window. Following are some important files and directories: module-name/ build/ - Contains build outputs. libs/ - Contains private libraries. src/ - Contains all code and resource files for the module in the following subdirectories: androidTest/ - Contains code for instrumentation tests that run on an Android device. main/ - Contains the "main" sourceset files: AndroidManifest.xml -Describes the nature of the application and each of its components. java/ - Contains Java code sources. jni/ - Contains native code using the Java Native Interface (JNI). gen/ - Contains the Java files generated by Android Studio, such as your R.java file. res/ - Contains application resources, such as drawable files, layout files, and UI string. assets/ - Contains file that should be compiled into an .apk file as-is. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager . For example, this is a good location for textures and game data. test/ - Contains code for local tests that run on your host JVM. build.gradle (module) -This defines the module-specific build configurations. build.gradle (project) - This defines your build configuration that apply to all modules. This file is integral to the project, so you should maintain them in revision control with all other source code.

App Resources

App Resources Overview Resources are the additional files and static content that your code uses, such as bitmaps, layout definitions, user interface strings, animation instructions, and more. Externalizing Resources You should always externalize app resources such as images and strings from your code, so that you can maintain them independently. You should also provide alternative resources for specific device configurations, by grouping them in specially-named resource directories. At runtime, Android uses the appropriate resource based on the current configuration. For example, you might want to provide a different UI layout depending on the screen size or different strings depending on the language setting. Once you externalize your app resources, you can access them using resource IDs that are generated in your project's R class.

Grouping Resource Types You should place each type of resource in a specific subdirectory of your project's res/ directory. For example, here's the file hierarchy for a simple project: Such as all the images should be kept in drawable folder, layouts in layout folder, etc. As you can see in this example, the res/ directory contains all the resources (in subdirectories): an image resource two layout resources mipmap/ directories for launcher icons a string resource file. The resource directory names are important and are described in next slide.

Resource directories supported inside project res/ directory Resource Type animator/ XML files that define property animations (You can define an animation to change any object property over time). anim/ XML files that define tween animations (A tween animation can perform a series of simple transformations (position, size, rotation, and transparency) on the contents of a View object. So, if you have a TextView object, you can move, rotate, grow, or shrink the text.) color/ XML files that define a state list of colors. drawable/ Bitmap files (.png, .9.png, .jpg, .gif) or other XML files that are compiled into the following drawable resource subtypes such as Bitmap files, Nine-Patches (re-sizable bitmaps), Shapes, etc. mipmap/ Drawable files for different launcher icon densities layout/ XML files that define a user interface layout. menu/ XML files that define app menus, such as an Options Menu, Context Menu, or Sub Menu. raw/ Arbitrary files to save in their raw form, such as text files, audio and video files or database files, etc. values/ XML files that contain simple values, such as strings, integers, and colors. xml/ Arbitrary XML files that can be read at runtime by calling Resources.getXML(). font/ Font files with extensions such as .ttf, .otf, or .ttc, or XML files that include a <font-family> element.

Alternative Resources Almost every app should provide alternative resources to support specific device configurations. For instance, you should include alternative drawable resources for different screen densities and alternative string resources for different languages. At runtime, Android detects the current device configuration and loads the appropriate resources for your app.

Contd. To specify configuration-specific alternatives for a set of resources: Create a new directory in res/ named in the form <resources_name>-<config_qualifier>. <resources_name> is the directory name of the corresponding default resources (defined in table 1). <qualifier> is a name that specifies an individual configuration for which these resources are to be used (defined in table 2). You can append more than one <qualifier>. Separate each one with a dash. Save the respective alternative resources in this new directory. The resource files must be named exactly the same as the default resource files. For Example:

Some configuration quantifiers

Defining/Creating resources values/strings.xml values/colors.xml layout/activity_main.xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>

Accessing App Resources Once you provide a resource in your application, you can apply it by referencing its resource ID. All resource IDs are defined in your project's R class. Accessing resources in code Syntax: R.<resource_type>.<resource_name> Accessing resources in XML Syntax: @<resource_type>/<resource_name>

How Android finds the best-matching resource When user requests a resource for which you have provided alternatives Android selects which alternative resource to use at runtime, depending on the current device configuration. 

Event Handling

Event Handling Events are a useful way to collect data about a user's interaction with interactive components of Applications. Like button presses or screen touch etc. The Android framework maintains an event queue as first-in, first-out (FIFO) basis. You can capture these events in your program and take appropriate action as per requirements.

Contd. There are following three concepts related to Android Event Management − Event Listeners − An event listener is an interface that has some methods you need to override in order to do something when an event occurs. Event Listeners Registration − Event Registration is the process by which an Event Handler gets registered with an Event Listener so that the handler is called when the Event Listener fires the event. Event Handlers − When an event happens and we have registered an event listener for the event, the event listener calls the Event Handlers, which is the method that actually handles the event.

Event Listeners and Event Handlers Event Listener & Description onClick() OnClickListener() This is called when the user either clicks or touches or focuses upon any widget like button, text, image etc. You will use onClick() event handler to handle such event. onLongClick() OnLongClickListener() This is called when the user either clicks or touches or focuses upon any widget like button, text, image etc. for one or more seconds. You will use onLongClick() event handler to handle such event. onFocusChange() OnFocusChangeListener() This is called when the widget looses its focus ie. user goes away from the view item. You will use onFocusChange() event handler to handle such event. onKey() This is called when the user is focused on the item and presses or releases a hardware key on the device. You will use onKey() event handler to handle such event. onTouch() OnTouchListener() This is called when the user presses the key, releases the key, or any movement gesture on the screen. You will use onTouch() event handler to handle such event. onMenuItemClick() OnMenuItemClickListener() This is called when the user selects a menu item. You will use onMenuItemClick() event handler to handle such event. onCreateContextMenu() onCreateContextMenuItemListener() This is called when the context menu is being built(as the result of a sustained "long click)

Event Listeners Registration Event Registration is the process by which an Event Handler gets registered with an Event Listener so that the handler is called when the Event Listener fires the event. Using an Anonymous Inner Class Activity class implements the Listener interface. Using Layout file activity_main.xml to specify event handler directly.

Event Listener Registration using an Anonymous Inner Class

Event Listener Registration by implementing Listener Interface public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button b1; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = findViewById(R.id.textView); b1 = findViewById(R.id.button); b1.setOnClickListener(this); } @Override public void onClick(View v) { if(v.getId() == R.id.button){ tv.setText("Hello"); } } }

Event Listener Registration using Layout file <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button" android:onClick="btClick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView"/> </android.support.constraint.ConstraintLayout> public class MainActivity extends AppCompatActivity { Button b1; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = findViewById(R.id.textView); } public void btClick(View v) { tv.setText("Hello"); } }

Thank You!