You are on page 1of 31

Lecture 3

External Resources and


Activity

by
Zana Azeez Kakarash

Email: zana.azeez@kti.edu.krd

2021– 2022

Kurdistan Technical Institute


(www.kti.edu.krd)
CS Department
External Resource

Three
Externalizing Resource

• Android supports the externalization of resources, ranging from simple values such as strings and

colors to more complex resources such as images, animations, themes, and menus.

• By externalizing resources, you make them easier to maintain, update, and manage

• To include different resources to support variations in hardware - particularly , screen size and

resolution.

Kurdistan Technical Institute 3 Lecturer : Zana Azeez Kakarash


Externalizing Resource

• Each Android projects’ resources are located in the res directory.


• Not every project will use every type, but any resource must fit one of the available types:

Kurdistan Technical Institute 4 Lecturer : Zana Azeez Kakarash


How to Access Resource

5
Externalizing Resource

• To access resources, Android uses two ways:

First Way : accessing in Java files :

in java files android uses the following methods:

- findViewByID()

- getResources()

Second Way : accessing in XML file :

Android uses @ symbol to call all the types of access in XML files

Kurdistan Technical Institute 6 Lecturer : Zana Azeez Kakarash


Externalizing Resource
• UI entities created in the layout are assigned a unique id.
• The actual View describing the UI component can be retrieved using the findViewById()

method.

Kurdistan Technical Institute 7 Lecturer : Zana Azeez Kakarash


Example 1

Kurdistan Technical Institute 8 Lecturer : Zana Azeez Kakarash


Example 2
<LinearLayout >
<ImageView android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/android" />
<ImageView android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="107dp"
android:src="@drawable/camera" />
<ImageView android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="105dp"
android:src="@drawable/checkin" />

<ImageView android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/exit" />

</LinearLayout>

Kurdistan Technical Institute 9 Lecturer : Zana Azeez Kakarash


Android UI Layout

10
Android - UI Layouts

• The basic block for user interface is a View object which is created from the View class.

• It is responsible for drawing and event handling.

• Views are used to create interactive UI components like buttons, edittext, textview etc.

• The ViewGroup class called "layouts," which are different kinds of layout architecture, like linear,

relative and frame.

Kurdistan Technical Institute 11 Lecturer : Zana Azeez Kakarash


View & View Groups

Kurdistan Technical Institute 12 Lecturer : Zana Azeez Kakarash


Declaring Layout

• The most common way to define your layout and express the view is with an XML layout file.

• View objects are leaves in the tree, ViewGroup objects are branches in the tree.

Kurdistan Technical Institute 13 Lecturer : Zana Azeez Kakarash


Android Layout Types

Kurdistan Technical Institute 14 Lecturer : Zana Azeez Kakarash


Android Layout Types

Kurdistan Technical Institute 15 Lecturer : Zana Azeez Kakarash


1- Linear Layout

Kurdistan Technical Institute 16 Lecturer : Zana Azeez Kakarash


2- Relative Layout

Kurdistan Technical Institute 17 Lecturer : Zana Azeez Kakarash


3- Table Layout

Kurdistan Technical Institute 18 Lecturer : Zana Azeez Kakarash


4- Absolute Layout

Kurdistan Technical Institute 19 Lecturer : Zana Azeez Kakarash


5- Frame Layout

Kurdistan Technical Institute 20 Lecturer : Zana Azeez Kakarash


6- List View and Grid View

Kurdistan Technical Institute 21 Lecturer : Zana Azeez Kakarash


Layout Measurements

22
Measurements in layout

• You will use one of these constants to set the width or height :

android:layout_width = match_parent
tells your view to become as big as its parent view.

android:layout_width = wrap_content
tells your view to size itself to the dimensions required by its content.

• Here width and height can be specified in terms of dp (Density-independent Pixels), sp ( Scale-

independent Pixels), pt ( Points which is 1/72 of an inch), px( Pixels), mm ( Millimeters) and finally

in (inches).

Kurdistan Technical Institute 23 Lecturer : Zana Azeez Kakarash


View Identification

• A view object may have a unique ID assigned to it which will identify the View uniquely within the tree.

• The syntax for an ID, inside an XML tag is : android:id = " @ + id / btn "

• Following is a brief description of @ and + signs :

• The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and

expand the rest of the ID string and identify it as an ID resource.

• The plus-symbol (+) means that this is a new resource name that must be created and added to our

resources.

Kurdistan Technical Institute 24 Lecturer : Zana Azeez Kakarash


Activity in Android

25
Activity

• An Activity is an application component that provides a screen with which users can interact in

order to do something, such as dial the phone, take a photo, send an email, or view a map.

• The activity’s content view or user interface is initialized using the setContentView() method

Kurdistan Technical Institute 26 Lecturer : Zana Azeez Kakarash


Activity life cycle

• While application is running, we create new Activities and close old ones, hide the application and open it again
and so on, and Activity can process all these events it is required and it can be in one of four states :

1) Resumed - Activity is visible on the screen, it is focused and the user can interact with it. This state is
sometimes called Running.

2) Paused - Activity is not focused, user cannot interact with it, but it is visible (it is covered by another Activity,
which is transparent)

3) Stopped - Activity is not visible, that is, it is not focused and user cannot interact with it . We can wake up an
stopped activity up by using "Notification" or multi-task button.

4) Dead - The activity has been finished manually, or garbage collected by system.

Kurdistan Technical Institute 27 Lecturer : Zana Azeez Kakarash


Loops and the paths an activity

Kurdistan Technical Institute 28 Lecturer : Zana Azeez Kakarash


Why we Implementing activity lifecycle methods

• Implementing your activity lifecycle methods properly ensures your app behaves well in several
ways, including that it :

a) Does not crash if the user receives a phone call or switches to another app while using your app.

b) Does not lose the user's progress if they leave your app and return to it at a later time.

c) Does not crash or lose the user's progress when the screen rotates between landscape and

portrait orientation.

Kurdistan Technical Institute 29 Lecturer : Zana Azeez Kakarash


How to Specify Your App's Launcher Activity ?

• System calls the onCreate() method for the Activity in your app that you've declared to be the "launcher" (or

"main") activity.

• You can define which activity to use as the main activity in the Android manifest file, AndroidManifest.xml .

• The main activity for your app must be declared in the manifest with an <intent-filter> that includes the MAIN

action and LAUNCHER category.

• For example:

Kurdistan Technical Institute 30 Lecturer : Zana Azeez Kakarash


Thanks!
Any questions ?
You can find me at
◉ zana.azeez@kti.edu.krd
◉ zana.azeez.k@gmail.com

31

You might also like