Survey							
                            
		                
		                * Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
GATAV
2. Android-Basics, part 1
TOC
1. The anatomy of an Android project
2. The DDMS perspective
3. Important components of an Android app
•
•
•
•
•
Activities
Layouts
Fragments
Views & Widgets
Intents, part 1
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
2
The anatomy of an Android project
 The ‚src‘-folder contains the different
Java files (in most cases activities).
 The folder ‚gen‘ contains the automatic
generated file R.java .
 The ‚assets‘-folder for raw files.
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
3
The anatomy of an Android project
 Inside the ‚bin‘-folder you can find the
app itself and the used resources.
 The folder ‚res‘ holds all the resource
files (e.g. images, layout- and value-files,
sounds, …).
 The ‚AndroidManifest.xml‘
 Configuration files
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
4
The anatomy of an Android project
 The ‚res‘-folder:
 ‚anim‘ to store animations
 ‚drawable‘ for images
 png, 9.png, jpg, gif
 ‚layout‘ for layout xml-files
 ‚menu‘ for xml-files that define application
menus
 ‚raw‘ to save files in their raw form
 e.g. pdf, mp3, ...
 ‚values‘ for values like strings, colors,
dimensions, arrays, styles, …
 ‚xml‘ for any xml-files that not fit one of the
other categories
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
5
The DDMS perspective
 DDMS = Dalvik Debug Monitoring Server
 The DDMS allows
to take screenshots
to terminate processes
to get thread and heap informations
to monitor the logcat
to simulate incoming calls, SMS and geo-locations
to access the file structure on the device
...
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
6
The DDMS perspective
 The DDMS perspective
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
7
Important components of an Android app
Activities
Fragments
Layouts
Views & Widgets
Intents, part 1
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
8
Important components of an Android app
Activities
 An activity represents a single screen with a user
interface
 Example (email application):
 one activity that shows a list of new emails
 one activity to compose an email
 one activity for reading emails
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
9
Important components of an Android app
Activities
 Activities are the base of an Android application
 Activities work together, but each one is independent of
the others
 A different application can start any one of these
activities (if the application allows it)
 e.g., a camera application can start the compose-email-activity
of the email application, so that the user can mail the new
picture.
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
10
Important components of an Android app
Activities
 An activity is implemented as a subclass of Activity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
/...
}
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
11
Important components of an Android app
Activities
 The activity lifecycle
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
12
Important components of an Android app
Activities
 The activity lifecycle
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
13
Important components of an Android app
Fragments
 Fragments represent portions of a user interface (screen)
in an activity
 Fragments are modular sections of the ui of activities
with own lifecylce and own input events
 Fragments can be efficiently dynamically added and
removed
 Fragments live in the view-hierachy of an activity
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
15
Important components of an Android app
Fragments
 We use one single fragment:
public class MainActivity extends Activity {
/...
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main,
container, false);
return rootView;
}
}
}
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
16
Important components of an Android app
Layouts
 For every screen resp. activity resp. fragment a layout is stored as
a XML file in the res/layout folder
 All layouts are subclasses of View
 Each element of a layout needs specifications about width and
height
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
17
Important components of an Android app
Layouts
 LinearLayout
 in most cases the preferred layout
 the most flexible layout but also the most expensive
 two orientations: vertical | horizontal
 RelativeLayout
 elements are positioned relative to the border or other elements
 high performance
 examples:
 android:layout_alignParentRight="true"
 android:layout_alignParentBottom="true"
 android:layout_below="@id/..."
 android:layout_toLeftOf="@id/..."
 android:layout_alignTop="@id/..."
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
18
Important components of an Android app
Views & Widgets
 Views: visible elements on a screen
 Widgets: more complex and compound views
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
19
Important components of an Android app
Views & Widgets
From Code:
LinearLayout layout = new LinearLayout(this);
setContentView(layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setText("Hallo Welt!");
btn.setLayoutParams(params);
layout.addView(btn);
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
20
Important components of an Android app
Views & Widgets
From XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button android:text="Hallo Welt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
21
Important components of an Android app
Views & Widgets
 Parameter for view elements:
 android:id="@+id/id_TextView_Main"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/hello"
 android:src="@drawable/gatav_banner"
 ...
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
22
Important components of an Android app
Intents, part 1
 Intent = (german) Absicht
 With intents, activities can communicate with each other and
transmit data and information
 Two kinds of intents:
 expl. intents:
 the target of the intent is known and defined
 in most cases activities of the same application
 most used contructor: new Intent(context, class)
 impl. intents:
 the target of the intent is unknown
 "I have a task. Who can handle this?"
 most used contructor: new Intent(Action, Uri)
 those intents are handled by applications with a matching intent filter
 if more than one application has a matching intent filter, android provides a
choise
Graphical Apps - The Android View: Prof. Dr. Bender, Prof. Dr. Tronnier
24