Download presentation
Presentation is loading. Please wait.
Published byมณี เคนเนะดิ Modified over 5 years ago
1
Android Project Structure, App Resources and Event Handling
Mobile Programming Android Project Structure, App Resources and Event Handling
2
Android Projects
3
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.
4
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.
5
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.
6
App Resources
7
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.
8
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.
9
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.
10
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.
11
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:
12
Some configuration quantifiers
13
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=" xmlns:app=" xmlns: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>
14
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
15
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.
16
Event Handling
17
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.
18
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.
19
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)
20
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.
21
Event Listener Registration using an Anonymous Inner Class
22
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"); } } }
23
Event Listener Registration using Layout file
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android=" xmlns:app=" xmlns: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" /> <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" </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"); } }
24
Thank You!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.