16. Android applications have common structure Activity is the presentation layer of your app: there will be one per screen, and the Views provide the UI to the activity Intents specify what specific action should be performed Services run in the background and have no UI for the user – they will update data, and trigger events
17. There is a common file structure for applications code Autogenerated resource list files images UI layouts constants
18. Standard components form building blocks for Android apps Notifications Has life-cycle Activity screen Views App to handle content Intents Background app Like music player Service manifest ContentProviders Other applications
19. 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); } }
22. 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.
23. Creating HelloWorld.java package matos.demo; import android.app.Activity; import android.widget.Toast; public class HelloWorldextends 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(); } }
24. Run HelloWorld application To run android application 1.Run 2.Run > Select Android Application 3. Press ok
25. Adding more UI components to your application There are to ways to add your UI components Through coding (java code) Or defining in main.xml Xml code Java code TextViewtv = new TextView(); Tv.setText(“Hello”); <TextViewandroid:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
26. 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); ScrollViewsv = new ScrollView(this); LinearLayoutll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); sv.addView(ll); this.setContentView(sv); } }
27. Adding components to Layout final TextViewtv = 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”); });
28.
29. 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;
31. /* 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();
32. if (c != null) { // Loop through all Results do { String Name = c.getString(Column1); int Age = c.getInt(Column2); Data =Data +Name+"/"+Age+""; }while(c.moveToNext()); } TextViewtv = 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(); } } }