Beruflich Dokumente
Kultur Dokumente
ACTIVITIES
Activities
Subclasses of the Activity class Managed by Activity Manager (managed components)
Creating Activities
Extend activity class Add entry to it the manifest file
open your manifest file and add an <activity> element as a child of the <application> element
The android: name attribute is a must to have attribute and Once you publish your application, you should not change this name. Implement at least onCreate() method Implement the other required call backs
Running DDMS
From Eclipse: Click Window > Open Perspective > Other... > DDMS.
Using LogCat
LogCat outputs the messages that you print out using the Log class along with other system messages such as stack traces when exceptions are thrown. Filter certain messages with the following buttons: Verbose Debug Info Warn Error You can also setup your own custom filter
How to use it
Log is a logging class that you can utilize in your code to print out messages to the LogCat. Common logging methods include: v(String, String) (verbose) d(String, String) (debug) i(String, String) (information) w(String, String) (warning) e(String, String) (error) Example Log.i(This is an information message);
Example
All in XML
GUI components and event handler assignment are written in XML E.g. android:onClick="oClickHandler" Hybrid GUI components and resources are described in XML files All other stuff are written in Java
Note
Use Java-based for building dynamic gui Otherwise, use the hybrid approach
Easy to maintain Good separation between Presentation layer and business logic
b.setText("I'm Here");
myTestLayout.addView(b); setContentView(myTestLayout);
Building UI in XML
Building UI in XML
In Java
Building UI in Hybrid
Hybrid
android:text=Hello"
android:textSize="20sp
Measurements
The small quick advice: Use sp for fonts
Measurements
Screen size Actual physical size, measured as the screen's diagonal.
View ID
Each view you use should have an id When you are going to refer to this view you will need its id.
Event Listeners
Using Separate Listener Class Using Named Inner Class
View groups
They are invisible containers that holds other views. The base for all layouts like FrameLayout, GridView, List View, Table Layout and many others
Views
are the main building blocks for user interface components The base class for all widgets
Example
Layouts
Layouts
Your layout is the architecture for the user interface in an Activity It defines the layout structure and holds all the elements that appear to the user
Example:
Linear Layout Frame Layout Table Layout Relative Layout
Linear Layout
LinearLayout is a ViewGroup that displays child View elements in a linear direction, either vertically or horizontally. Via the orientation attribute supports assigning a weight to children android:orientation="horizontal" How Can you build this ?
Weight: 1,2,3,.0
Gravity: alignment (left, center, right, top, bottom) Padding: space from boundaries to content Margin: space from boundaries to the parents Android gravity vs. layout_gravity android:gravity sets the gravity of the content of the View its used on. android:layout_gravity sets the gravity of the View or Layout in its parent.
Example
Relative Layout
Lets child views specify their position relative to the parent view or to each other (specified by ID)
Relative Layout
Referring to the container : android:layout_alignParentTop android:layout_alignParentLeft android:layout_centerInParent android:layout_centerHorizontal Referring to other widgets android:layout_alignTop android:layout_alignLeft, android:layout_alignRight android:layout_below android:layout_toLeftOf
Relative Layout
Is a very powerful utility for designing a user interface It can eliminate nested view groups and keep your layout hierarchy flat, which improves performance. If you find yourself using several nested LinearLayout groups, you may be able to replace them with a single RelativeLayout. For more about Relative layout
http://developer.android.com/guide/topics/ui/layout/relative.html
http://www.mkyong.com/android/android-relativelayout-example/
Table Layout
Views are grouped in rows and columns Used in conjunction with TableRow
Columns are determined by Android based on the maximum number of the views in the table row.
Layout_Span attribute can be used to make a view span multiple columns each column occupies space equal to the size of the largest widget in it.
Table layout
Frame Layout
designed to block out an area on the screen to display a single item. FrameLayout should be used to hold a single child view
Views
TextView
android:id="@+id/myTextView1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000ff" android:padding="3px" android:text="Enter User Name" android:textSize="16sp" android:textStyle="bold" android:gravity="center"
ImageView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:layout_width="250" android:layout_height="250" android:src="@drawable/1" android:layout_centerInParent="true"/> </RelativeLayout>
ImageView
Change Image at runtime image.setImageResource(R.drawable.myImage);
Edit Text
android:autoText, (true/false) provides automatic spelling assistance android:capitalize, (words/sentences) automatic capitalization
CheckBox
A checkbox is a specific type of two-states button that can be either checked or unchecked. <CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="Meat with rice" android:textSize="24" />
Radio Buttons
<RadioGroup android:layout_width="fill_parent" android:layout_height="fill_parent" > <RadioButton android:id="@+id/btnTea" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tea" android:textSize="30sp" android:checked="true"/> </RadioGroup>
END