Introduction To Google Android (Ft Rohan Bomle)
-
Upload
fafadia-tech -
Category
Technology
-
view
4.939 -
download
0
description
Transcript of Introduction To Google Android (Ft Rohan Bomle)
Google Android
Fafadia Tech Rohan Bomle ([email protected])
AndroidWhat 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
Services run in the background and have no UI for the user – they will update data, and trigger events
Intents specify what specific action should be performed
Activity is the presentation layer of your app: there will be one per screen, and the Views provide the UI to the activity
There is a common file structure for applications
code
images
files
UI layouts
constants
Autogenerated resource list
Standard components form building blocks for Android apps
Other applications
Has life-cycle
screen
App to handle content
Background appLike music player
Views
manifest
Activity
Intents
Service
Notifications
ContentProviders
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.javapackage 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 HelloWorld”,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 applicationThere are to ways to add your UI components
1. Through coding (java code)
2. Or defining in main.xml
TextView tv = new TextView();Tv.setText(“Hello”);
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
Java code Xml code
Lets create a same application with UI componentspackage 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 filepackage 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