Getting Started
Okkay, it wasnt easy to get eh first helloworld app running on the simulator.
Android sdk Installation
Step 1
I the Tools you launch the android app, which launches the installation manager. In the you should have the installed application of Android SDK and Google Api
Step 2
Eclipse Installation_ Galileo or Ganymede
In Software Updates - Get the Android link setup to downloaded from Site or download then ADT from Site and install as Archive.
Step 3
Create Android Virtual Device - AVD
you can do it from the installation manager of the android tool.
All set to go then
New File- Project -Android Project->Details (Package name - Min Sdk etc)
Create a hello world application. Layout is similar to IB in Iphone to create layout in a faster way.
moslty XML tweeking
Simulator takes a while to launch - but executing the app is fast.
Example of a layout Element in the main.xml would be
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:"/>
<EditText
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label"/>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="OK" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
Example of Relative Layout 1
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
Example 2
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
Example 3 - Adding a ImageView with Background Image by drag drop in the Layout - Add the image in the background parameter of the imageview
<ImageView android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon">
</ImageView>
Changes to the Code
Say adding a TextView to the code
Tip: An easy way to add import packages to your project is to press Ctrl-Shift-O
Example 4 - Time and Date Display
Time Display-- TextView - Declaration in onCreatebundle
mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
mPickTime = (Button) findViewById(R.id.pickTime);/// Time Picker
/// set Up a listener for the button
// add a click listener to the button
mPickTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
Which In turn Launches the
TimePickerDialog Dialog
Toast Message
A toast is a view containing a quick little message for the user.
Compare this to UIAlertView in Iphone
Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
Comparisons of the elements used in Android and Iphone
ImageButton - > UIButton
EditText - > UiTextField
CheckBox - >
RadioButton - >
ToggleButton - >
UIAlertVew -> UiAlertView
ListView - > UiTableView
All the above examples extends Activity Class
For List View - Extend the ListActivityView
Instead of using the R.layout.main for setting the content view. We would populate the table
view with ArrayAdapter
setListAdapter(newArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, COUNTRIES));
getListView().setTextFilterEnabled(true);
And the array.
static final String[] COUNTRIES = new String[] { "ListValue 1"};
ADDING MENU- which pops up on pressing the menu button on the phone
Import the following packages :
import android.view.Menu;
import android.view.MenuItem;
To add menu item all we need is to override 2 functions
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
/// Function 2 to receive the menu button clicks.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case MENU_ADD:
Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
break;
case MENU_QUIT:
Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
this.finish();
}
return super.onOptionsItemSelected(item);
}
/// Adding the menu items to the menu in onCreateOptionsMenu() Function
menu.add(0, MENU_ADD, 0, "Add").setIcon(android.R.drawable.ic_menu_add);
menu.add(0, MENU_QUIT, 0, "Quit").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
Getting the connectivity with SQL Database
We need to obtain here a list to display the elements
/// Creating a list
this.list = new ListView(this);
this.registerForContextMenu(this.list);
/// 2 lines of Database connection
this.db = this.openOrCreateDatabase("dic_tut5", MODE_PRIVATE, null);
this.db.execSQL("CREATE TABLE IF NOT EXISTS table_of_dics(value REAL)");
this.update_list();
setContentView(this.list);
Using ArrayList
ArrayList<String> db_results = new ArrayList<String>();
Cursor cursor = db.rawQuery("SELECT value FROM table_of_dics ORDER BY value", null);
while(cursor.moveToNext()) {
db_results.add(String.valueOf(cursor.getDouble(cursor.getColumnIndex("value"))));
}
cursor.close();
this.list.setAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_single_choice,
db_results
));
/// Insert into the database
db.execSQL("INSERT INTO table_of_dics(value) VALUES(" + String.valueOf(Math.random()) + ")");
Android sdk Installation
Step 1
I the Tools you launch the android app, which launches the installation manager. In the you should have the installed application of Android SDK and Google Api
Step 2
Eclipse Installation_ Galileo or Ganymede
In Software Updates - Get the Android link setup to downloaded from Site or download then ADT from Site and install as Archive.
Step 3
Create Android Virtual Device - AVD
you can do it from the installation manager of the android tool.
All set to go then
New File- Project -Android Project->Details (Package name - Min Sdk etc)
Create a hello world application. Layout is similar to IB in Iphone to create layout in a faster way.
moslty XML tweeking
Simulator takes a while to launch - but executing the app is fast.
Example of a layout Element in the main.xml would be
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:"/>
<EditText
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label"/>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="OK" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
Example of Relative Layout 1
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
Example 2
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
Example 3 - Adding a ImageView with Background Image by drag drop in the Layout - Add the image in the background parameter of the imageview
<ImageView android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon">
</ImageView>
Changes to the Code
Say adding a TextView to the code
Tip: An easy way to add import packages to your project is to press Ctrl-Shift-O
Example 4 - Time and Date Display
Time Display-- TextView - Declaration in onCreatebundle
mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
mPickTime = (Button) findViewById(R.id.pickTime);/// Time Picker
/// set Up a listener for the button
// add a click listener to the button
mPickTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
Which In turn Launches the
TimePickerDialog Dialog
Toast Message
A toast is a view containing a quick little message for the user.
Compare this to UIAlertView in Iphone
Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
Comparisons of the elements used in Android and Iphone
ImageButton - > UIButton
EditText - > UiTextField
CheckBox - >
RadioButton - >
ToggleButton - >
UIAlertVew -> UiAlertView
ListView - > UiTableView
All the above examples extends Activity Class
For List View - Extend the ListActivityView
Instead of using the R.layout.main for setting the content view. We would populate the table
view with ArrayAdapter
setListAdapter(newArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, COUNTRIES));
getListView().setTextFilterEnabled(true);
And the array.
static final String[] COUNTRIES = new String[] { "ListValue 1"};
ADDING MENU- which pops up on pressing the menu button on the phone
Import the following packages :
import android.view.Menu;
import android.view.MenuItem;
To add menu item all we need is to override 2 functions
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
/// Function 2 to receive the menu button clicks.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case MENU_ADD:
Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
break;
case MENU_QUIT:
Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
this.finish();
}
return super.onOptionsItemSelected(item);
}
/// Adding the menu items to the menu in onCreateOptionsMenu() Function
menu.add(0, MENU_ADD, 0, "Add").setIcon(android.R.drawable.ic_menu_add);
menu.add(0, MENU_QUIT, 0, "Quit").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
Getting the connectivity with SQL Database
We need to obtain here a list to display the elements
/// Creating a list
this.list = new ListView(this);
this.registerForContextMenu(this.list);
/// 2 lines of Database connection
this.db = this.openOrCreateDatabase("dic_tut5", MODE_PRIVATE, null);
this.db.execSQL("CREATE TABLE IF NOT EXISTS table_of_dics(value REAL)");
this.update_list();
setContentView(this.list);
Using ArrayList
ArrayList<String> db_results = new ArrayList<String>();
Cursor cursor = db.rawQuery("SELECT value FROM table_of_dics ORDER BY value", null);
while(cursor.moveToNext()) {
db_results.add(String.valueOf(cursor.getDouble(cursor.getColumnIndex("value"))));
}
cursor.close();
this.list.setAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_single_choice,
db_results
));
/// Insert into the database
db.execSQL("INSERT INTO table_of_dics(value) VALUES(" + String.valueOf(Math.random()) + ")");