You are on page 1of 23

Google Android

Fafadia Tech
Rohan Bomle (rohan@fafadia-tech.com)
Android
What is Android?
• A software platform and operating system for mobile
devices

• Based on the Linux kernel

• Developed by Google and later the Open Handset Alliance


(OHA)

• Allows writing managed code in the Java language

• C/C++ also possible but not supported


IDE and Tools

• Android SDK
• Class Library
• Developer Tools
 dx – Dalvik Cross-Assembler
 aapt – Android Asset Packaging Tool
 adb – Android Debug Bridge
 ddms – Dalvik Debug Monitor Service
• Emulator and System Images
• Documentation and Sample Code

• Eclipse IDE + ADT (Android Development Tools)


• Reduces Development and Testing Time
• Makes User Interface-Creation easier
• Makes Application Description Easier
How to install ADT in Eclipse
• Start Eclipse, then select Help > Software Updates > Find and
Install.... In the resulting dialog box, enter a name for the remote site
(e.g. Android Plugin) and enter this as its URL:
https://dl-ssl.google.com/android/eclipse/. Press OK.

• You should now see the new site added to the search list (and
checked). Press Finish.
In the subsequent Search Results dialog box, select the checkbox
for Android Plugin > Eclipse Integration > Android Development
Tools and press Next.

• Read the license agreement and then select Accept terms of the
license agreement, if appropriate. Press Next. Press Finish.
The ADT plugin is not signed; you can accept the installation
anyway by pressing Install All.
Restart Eclipse.
Android applications have common structure
Activity
Activity is
is the
the presentation
presentation
layer
layer of
of your
your app:
app: there
there will
will
be
be one
one per
per screen,
screen, and
and the
the
Views
Views provide
provide the
the UI
UI to
to the
the
activity
activity

Intents
Intents specify
specify what
what specific
specific
action
action should
should be
be performed
performed

Services
Services run
run in
in the
the
background
background andand have
have no
no
UI
UI for
for the
the user
user –– they
they will
will
update
update data,
data, and
and trigger
trigger
events
events
There is a common file structure for
applications

code
Autogenerated
files resource list
images

UI layouts

constants
Standard components form building blocks for
Android apps

Notifications
Has life-cycle
Activity
screen
Views
App to handle content
Intents
Background app
Service Like music player

manifest

ContentProviders Other applications


Simple Android Application (java program)
package com.google.android.helloactivity;

import android.app.Activity;
import android.os.Bundle;

public class HelloActivity extends Activity {


public HelloActivity() {
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.hello_activity);
}
}
The AndroidManifest lists application details
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my_domain.app.helloactivity">

<application android:label="@string/app_name">
<activity android:name=".HelloActivity">

<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>

</activity>
</application>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000044">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
Creating and Running simple HelloWorld application

To create a new project:


1. Start Eclipse
2. Select File > New > Project.
3. Select Android > Android Project,
and click Next.
4. Enter Project name: HelloWorld.
5. Select Target Android 1.5.
6. Application name: Hello.
7. Package name: fafadia.tech.
8. Create Activity: HelloWorld.
9. Min SDK Version: 3.
10. Click Finish.
Creating HelloWorld.java
package matos.demo;
import android.app.Activity;
import android.widget.Toast;

public class HelloWorld extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(getBaseContext(),“Wlecome to Hello
World”,Toast.LENGTH_SHORT).show();
}
}
Run HelloWorld application

To run android application


1.Run
2.Run > Select Android Application
3. Press ok
Adding more UI components to your application
There are to ways to add your UI components
1. Through coding (java code)
2. Or defining in main.xml

Java code Xml code

TextView tv = new TextView(); <TextView


Tv.setText(“Hello”); android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
Lets create a same application with UI components
package org.HelloWorld.com;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ScrollView;
public class HelloWorld extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
this.setContentView(sv);
}
}
Adding components to Layout
final TextView tv = new TextView(this);
tv.setText(“Text Box");
tv.setTextSize(20);
tv.setTextColor(Color.BLACK);
ll.addView(tv);

Button b1 = new Button(this);


b1.setText("Submit");
b1.setTextSize(15);
ll.addView(b1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
tv.setText(“Submit button Clicked”);
});
Creating simple database application
• First we create a simple class which create a database, table. And
the we perform insert and select query on table.

• Android uses SQLite for its database needs. SQLite is a very fast
and lightweight database.

• Create New Project as MYDatabase and in that create new file


named it as create_database.java.

• Import two files


import android.database.Cursor;
importandroid.database.sqlite.SQLiteDatabase;
Create_database file
package org.example.mydatabase;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class create_database extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
SQLiteDatabase myDB= null;
String TableName = "myTable";
String Data="";
/* Create a Database. */
try
{
myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);

/* Create a Table in the Database. */


myDB.execSQL("CREATE TABLE IF NOT EXISTS “+ TableName
+ " (Field1 VARCHAR, Field2 INT(3));");

/* Insert data to a Table*/


myDB.execSQL("INSERT INTO “ + TableName+ " (Field1, Field2)"
+ " VALUES ('katrina', 25);");

/*retrieve data from database */


Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null);
int Column1 = c.getColumnIndex("Field1");
int Column2 = c.getColumnIndex("Field2");
// Check if our result was valid.
c.moveToFirst();
if (c != null)
{ // Loop through all Results
do
{
String Name = c.getString(Column1);
int Age = c.getInt(Column2);
Data =Data +Name+"/"+Age+"\n";
}while(c.moveToNext());
}
TextView tv = new TextView(this);
tv.setText(Data);
setContentView(tv);
}//end try
catch(Exception e)
{ System.out.println("Error", "Error", e);}
finally { if (myDB != null) myDB.close(); }
}
}
Thank You

You might also like