Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 37

APPLICATION DEVELOPMENT AND EMERGING

TECHNOLOGIES

Lecture 2
Android Basics

Engr. Racquel A. Cortez, MIT


Creating Android Application
Android Project

An Android project is organized similar to other Java projects, with


a few important exceptions. The Android SDK automatically
generates certain features specific to an Android project.
Creating a new App
Setting Project Option

• Application Name
• Package name
• Path Location
• Language
• Minimum Required SDK
Running the Application

• Running App on emulator


1. Select Hardware
2. Select a System Image
3. Verify Configuration
Running the Application

• Running App by deploying from PC to the Android


device thru USB
1. Select USB Debugging
Settings  Applications  Development
2. Allow Unknown Sources
Settings  Applications  Unknown sources
Running the Application

• Running App on Android device by deploying to the


Android market.
User Interface

• View is the base class for all visual components. All


the controls present in an android app are derived
from this class
• A ViewGroup is an object of class
Android.view.ViewGroup. The ViewGroup class serves
as a base class-for layouts.
• To attach View Hierarcy to the screen (for rendering),
call setContentView() in the Activity
UI Layouts
UI Controls
UI Controls
UI Controls
Application Components

Application components are the essential building blocks of


an Android application.

• Activities
• Services
• Broadcast Receivers
• Content Providers
Anatomy of Android Applications

1. Java
2. res/drawable
3. Res/layout
4. Res/value
5. AndroidManifest.xml
6. Gradle Scripts
Gradle Scripts

• Gradle is a build system (open source) which is used to automate


building, testing, deployment, etc.
• Build.gradle are scripts where one can automate the tasks.
 Top-level build.gradle – defines the build configurations that will be
applied to all the modules in the project.
 Module-level build.gradle – supports various build configurations
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android=http://schemas.android.com/apk/res/android
package="org.anddev.android.hello_android">
<application android:icon="@drawable/icon">
<activity android:name=".Hello_Android"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
AndroidManifest.xml

<manifest>
This is the root node of each AndroidManifest.xml. It contains the package-attribute,
which points to any package in our Activity.

<application>
Root element containing declarations of the application-level components contained
in the package.

<activity>
An Activity is the primary thing for an application to interact with the user.
AndroidManifest.xml
<intent - filter>
It declares what kind of Intents a component supports.

<action>
An action-type that the component supports.

<category>
A category-type that the component supports
AndroidManifest.xml

<receiver>
An IntentReceiver allows an application to be told about changes to data or actions that
happen, even if it is not currently running

<service>
A Service is a component that can run in the background for an arbitrary amount of time.

<provider>
A ContentProvider is a component that manages persistent data and publishes it for
access by other applications
Main Approaches in Programming app

Define Layout Assign Event


3 main approach GUI Controls
String Window Handlers

Java based Java Java Java Java


XML based XML XML XML XML
Hybrid XML/Java XML XML Java
Application Context

 The application context is the central location for all top-level application
functionality. Use the application context to access settings and resources
shared across multiple activity instances.
 Retrieve the application context for the current process by using the
getApplicationContext() method, like this:

Context context = getApplicationContext()

 Because the Activity class is derived from the Context class, you can use the
“this” object instead of retrieving the application context explicitly when
you’re writing code inside your Activity class.
Using Toast

Toast is a passive, non-blocking user notification that shows a simple


message at the bottom of the user‘s screen. A toast typically displays for a
few seconds and disappears.

Toast tempMessage = Toast.makeText(referenceToMainActivity, text, Toast.LENGTH_SHORT);


tempMessage.show();

Toast.makeText(this, “Message Here”, Toast.LENGTH_SHORT).show();


Using Snackbar
Android Snackbar is just like s Toast except that it can provide the user with the action button
to interact with. To make use of SnackBar in the project, include the Android Design Support
Library by adding the code below in build.gradle (Module app) file.

dependencies { implementation ‘com.android.support:design:27.1.}

Snackbar snackbar = Snackbar.make(coordinatorLayout, message, duration);


snackbar.show();
Using Snackbar
Android Snackbar is just like s Toast except that it can provide the user with the action button
to interact with. To make use of SnackBar in the project, include the Android Design Support
Library by adding the code below in build.gradle (Module app) file.

dependencies { implementation ‘com.android.support:design:27.1.}

Snackbar snackbar = Snackbar.make(coordinatorLayout, message, text, duration);


snackbar.show();
Using Snackbar

Snackbar snackbar = Snackbar.make(coordinatorLayout, "Message is deleted",


snackbar.LENGTH_LONG)
.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar snackbar1 = Snackbar.make(coordinatorLayout,
"Message is restored!", Snackbar.LENGTH_SHORT);
snackbar1.show();
}
});
snackbar.show();
Commonly Used Attributes
• ID
android:id

• Size
android:layout_height,
android:layout_width

 match_constraint
 wrap_content
Commonly Used Attributes

• android:weight
It controls the resizing behavior of its
child Views. The value is either 0 or 1.
Using a weight of 1 makes the View
stretch, while using 0 will make that
View just as big as needed.
Commonly Used Attributes

• Alignment
android:layout_gravity

android:gravity

Possible values:
top, bottom, left, right, center_vertical, center_horizontal, center,
fill_vertical, fill_horizontal, fill, clip_vertical, clip_horizontal
Commonly Used Attributes
• Margins

android:layout_marginBottom, layout_marginTop
android:layout_marginRight, layout_marginLeft

• Padding

android:paddingBottom, paddingTop
android:paddingRight, paddingLeft
• Color

android:background
android:textColor=“#colorcode”

• Click Handler

android:onClick
Referring to Existing Ids
• First component  @+id for assigning a new id

<Button android:id = “@+id/button_1”



android:layout_alignParentRight=“true”
… />

• Second component  @id for referring to an existing id

<Button android:id = “@+id/button_2”



android:layout_toLeftOf=“@id/button_1”
… />

Result: Button 2 Button 1


Organizing The Screen

 Create a landscape layout with the wizard


• Right Click on the res folder
• Select New  Android Resource file
• Select the Layout radio Button
• Type same filename with your layout in portrait
• For the folder name: /res/layout-land
Organizing The Screen
 Create a small screen only layout with the wizard
• Create layout
• File NewAndroid XML file
• Select the Layout radio Button
• Type same filename with your layout in portrait
• Add the size by selecting size available from the Available
Qualifiers
• Select scree size “Small” from the dropdown on the right

 
Organizing The Screen
 Targeting different screen sizes
• In Android Manifest.xml

<supports-screens>
android:smallScreens=”false”
android:normalScreens=”true”
android:largeScreens=””
  android:xlargeScreens=”true”
android:anyDensity=”true”
</supports-screens>
Multiple Screen Support

Reference: https://developer.android.com/guide/practices/screens_support.html

You might also like