3. Android Market
Android devices come in all shapes
and sizes. As of late November 2010,
the Android OS can be seen powering
the following types of devices:
➤ Smartphones
➤ Tablets
➤ E-reader devices
➤ Netbooks
➤ MP4 players
➤ Internet TVs
3
5. Tools to Develop Android
Apps
Eclipse IDE: to write code and design UI
Android SDK include AVD (Android Virtual Device): to
test the applications
ADT (Android Development Tools): The Plug-in includes various
wizards for creating and debugging Android projects.
5
8. Run the AVD
Select one of the created AVD and click start. This will run the AVD
where your App will be run on. (It will take a while for the AVD to load.).
You will have the option of scale the display . Screen Size 7 means
70%
8
16. Assignment 1
Implement the Hello android application:
1. Create and run an AVD
2. Create new android project
3. Configure the android project to run with the AVD
4. Test run the project
16
23. Assignment 2
Change the application name to “First Android App”
Change the TextView to “First TextView”
Center the text to the middle of the screen, by
changing the TextView properties to the following:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_gravity="center"/>
23
26. Assignment 3: new icon for the
app
Create a file (my_icon.png 72x72 pixels) and copy it to the
res/drawable directory
Change the manifest to point to this file (application icon is this
file).
Note: the file format can be png, jpg,
or gif (Not recommended)
26
28. Linear Layout
LinearLayout is a ViewGroup that displays child View elements
in a linear direction, either vertically or horizontally.
Linear Layout
Linear Layout
android:layout_weight="1">
Linear Layout
android:layout_weight="1">
28
31. Dimension Unit Measurements
Type of Measurement
Description
Unit String
Pixels
Actual screen pixels
px
Inches
Physical measurement
in
Millimeters
Physical measurement
mm
Points
Common font measurement
pt
Density-independent pixels
Pixels relative to 160dpi
dp
Scale-independent pixels
Best for scalable font display
sp
31
32. Color Formats Supported in
Android
Format
#RGB
#ARGB
Description
12-bit color
12-bit color with alpha
Example
#00F (blue)
#800F (blue, alpha 50%)
#RRGGBB
24-bit color
#FF00FF (magenta)
#80FF00FF (magenta,
alpha 50%)
#AARRGGBB 24-bit color with alpha
The following code retrieves a color resource named app_text_color using the
getColor() method:
int textColor = getResources().getColor(R.color.app_text_color);
32
38. Introduction Activity
import android.app.Activity; //hover over Activity/import Activity
import android.os.Bundle;
// Defined in AndroidManifest, when the project is created.
public class FormExample extends Activity {
/** Called when the activity is first created. */
@Override //added by eclipse
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //call parent
setContentView(R.layout.main); //show main.xml
}
}
38
39. Event OnClickListener
public class FormExample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //call parent
setContentView(R.layout.main); //show main.xml
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {// Perform action on click
Toast.makeText(FormExample.this, "button is
clicked",
Toast.LENGTH_SHORT).show();
} // a toast is a small message box that appears briefly on
the
39
}); // screen ( like a message box)
40. Event OnKeyListener
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
Toast.makeText(FormExample.this, edittext.getText(),
Toast.LENGTH_SHORT).show();
return true;
}
View:
view that generates the event
return false;
keyCode: which key is pressed
}
});
KeyEvent: KeyEvent object, where you can
access all the keyboard code
40
41. Checkbox Status
final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Checkbox status
if (((CheckBox) v).isChecked()) {
Toast.makeText(FormExample.this, "Selected",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(FormExample.this, "Not selected",
Toast.LENGTH_SHORT).show();
}
}
});
41
42. ToggleButton
final ToggleButton togglebutton = (ToggleButton)
findViewById(R.id.togglebutton);
togglebutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
if (togglebutton.isChecked()) {
Toast.makeText(FormExample.this, "Checked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(FormExample.this, "Not checked",
Toast.LENGTH_SHORT).show();
}
}
});
42
43. ChangeListener
final RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingbar);
ratingbar.setOnRatingBarChangeListener(new
OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
Toast.makeText(FormExample.this, "New Rating: " + rating,
Toast.LENGTH_SHORT).show();
}
});
43
45. ListView
ListView is a ViewGroup that creates a list of
scrollable items. The list items are automatically
inserted to the list using a ListAdapter.
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/
res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
45 </TextView>
46. ListView extends ListActivity
public class ListViewExample extends ListActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
COUNTRIES));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView)
view).getText(),
Toast.LENGTH_SHORT).show();
46 }
});
47. ListView cont.
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria", "American Samoa", “Cambodia",
"Christmas Island", "Colombia", "Comoros", "Congo",
"Dominican Republic", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam",
Marianas", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
};
Notes:
setListAdapter(ListAdapter) automatically adds a ListView to fill the entire screen
of the ListActivity. This method takes an ArrayAdapter, which manages the array of list
items that will be placed into the ListView.
eg setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
47
50. public class ExampleActivity extends Activity {
50
public void onCreate(Bundle savedInstanceState) {// The activity is being created.
super.onCreate(savedInstanceState);
}
protected void onStart() { // The activity is about to become visible.
super.onStart();
}
protected void onResume() {// The activity has become visible (it is now
"resumed").
super.onResume();
}
protected void onPause() {// Another activity is taking focus (this activity is about
//to be "paused").
super.onPause()
}
protected void onStop() {// The activity is no longer visible (it is now "stopped")
super.onStop();
}
protected void onDestroy() {// The activity is about to be destroyed.
super.onDestroy();
}
}
51. Create Layout and Views by
code
public class UICodeActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
// ---create a layout--LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
// ---create a textview--TextView tv = new TextView(this);
tv.setText("This is a TextView");
tv.setLayoutParams(params);
51
52. Create Layout and Views by
code
Button btn = new Button(this);
btn.setText("This is a Button");
btn.setLayoutParams(params);
// ---adds the textview--layout.addView(tv);
// ---adds the button--layout.addView(btn);
// ---create a layout param for the layout--LinearLayout.LayoutParams layoutParam = new
LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
this.addContentView(layout, layoutParam);
}
52