Android Bootcamp Santa Fe GTUG
-
Upload
matiasmolinas -
Category
Technology
-
view
2.789 -
download
2
description
Transcript of Android Bootcamp Santa Fe GTUG
![Page 1: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/1.jpg)
Android Bootcamp
Elaborado (con adaptaciones) a partir de los tutoriales:
http://developer.android.com/resources/tutorials/hello-world.htmlhttp://developer.android.com/resources/tutorials/views/index.html
![Page 2: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/2.jpg)
¿Qué es una aplicación Android?
Android Application•Java Language•Android SDK: compilar, empaquetar (.apk)
Múltiples actividades relacionadas entre sí•Alta cohesión•Bajo acoplamiento
Android Activity: A single screen with a user interface. Ej: aplicación de email
•Mostrar nuevos emails•Escribir nuevo email•Leer email
![Page 3: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/3.jpg)
Ciclo de vida Android Application
![Page 4: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/4.jpg)
Bootcamp Pen Drive
Por favor, copie desde el pen drive la carpeta BOOTCAMP en alguna carpeta de su notebook
Contenido
android-sdk: entorno de desarrollo Androideclipse: Eclipse Galileo 3.5.0txt: código del tutorialADT-10.0.0.zip: plugin de Android para Eclipse
![Page 5: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/5.jpg)
Prerrequisitos
Java SDK 1.6Android SKD (eliminar carpeta c:\Documents and Settings\...\.android)
![Page 6: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/6.jpg)
Instalación del Plugin ADT en Eclipse
En Eclipse:
Help --> Install New Software.... --> Add --> Archive…
Archive: ADT-10.0.0.zip(en la carpeta BOOTCAMP)
Name: Android Plugin
[x] Developer Tools[ ] DESMARQUE “Contact all update sites..."
Next, Next, Finish..... Restart
![Page 7: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/7.jpg)
Configurar Android SDK en Eclipse
En Eclipse:
Window -> Preferences -> Android
![Page 8: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/8.jpg)
Crear un Virtual Device
Desde Eclipse ingresar al AVD Manager:
AVD Manager -> Virtual Devices-> New
Name: MyAVDTarget: Android 2.1SD Card: -Skin: built-in (HVGA)
Create AVD
![Page 9: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/9.jpg)
Crear un Virtual Device
![Page 10: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/10.jpg)
Iniciar Virtual DeviceStart… Launch
![Page 11: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/11.jpg)
Proyecto Hello WorldEn Eclipse:
File -> New -> Other -> Android Project
Project Name: HelloWorldBuild Target: Android 2.1Properties: Application Name: Hello World Package Name: com.example.helloworld Create Activity: HelloActivity
Finish
![Page 12: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/12.jpg)
src/com/example/HelloActivity.java
- onCreate()
- Bundle savedInstanceState
- setContentView(R.layout.main);
![Page 13: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/13.jpg)
res/layout/main.xml
Define el layout de la Activity:
LinearLayout TableLayout RelativeLayout
![Page 14: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/14.jpg)
res/layout/main.xml
LinearLayout
![Page 15: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/15.jpg)
res/layout/main.xml
android:orientation="vertical"
android:layout_width / android:layout_height
"fill_parent""wrap_content"
"@string/hello"
![Page 16: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/16.jpg)
res/values/strings.xml
Cadenas de caracteres de la aplicación
![Page 17: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/17.jpg)
Prueba
Project -> Run
(Ctrl + Shift + F11)
![Page 18: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/18.jpg)
Esto es una Activity
![Page 19: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/19.jpg)
Pero es aburrido.
![Page 20: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/20.jpg)
Upgrade: Una lista
![Page 21: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/21.jpg)
Crear res/layout/list_item.xml [código 1.txt]
<?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" ></TextView>
Define el layout de cada item en la lista
![Page 22: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/22.jpg)
Modificar res/layout/main.xml [código: 2.txt]
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><ListViewandroid:id="@+id/mylistview" android:layout_width="fill_parent" android:layout_height="fill_parent" /></LinearLayout>
![Page 23: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/23.jpg)
HelloActivity.java [código: 3.txt]public class HelloActivity extends Activity implements OnItemClickListener { static final String[] COUNTRIES = { "Brazil", "Argentina","Mexico" };@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView lv = (ListView) findViewById(R.id.mylistview); lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES)); lv.setOnItemClickListener(this);}public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();}
![Page 24: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/24.jpg)
Prueba
Project -> Run
(Ctrl + Shift + F11)
![Page 25: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/25.jpg)
Upgrade: Cuadro de Texto y Botón para Añadir
![Page 26: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/26.jpg)
Modificar main.xml [Código 4.txt]
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/mytextview" android:layout_width="200sp" android:layout_height="wrap_content" android:text=""/> <Button android:id="@+id/mybutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Añadir"/> </LinearLayout> <ListView android:id="@+id/mylistview" android:layout_width="fill_parent" android:layout_height="fill_parent"/></LinearLayout>
![Page 27: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/27.jpg)
Prueba
Project -> Run
(Ctrl + Shift + F11)
![Page 28: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/28.jpg)
Ahora vamos a implementar...
![Page 29: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/29.jpg)
HelloActivity.java [Código: 5.txt]
package com.example.helloworld;
import ...
public class HelloActivity extends Activity implements OnItemClickListener, OnClickListener { private List<String> countries = new ArrayList<String>(); private ArrayAdapter<String> adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); countries.add("Mexico"); ListView lv = (ListView) findViewById(R.id.mylistview); adapter = new ArrayAdapter<String>( this, R.layout.list_item, countries); lv.setAdapter(adapter); lv.setOnItemClickListener(this); Button btn = (Button) findViewById(R.id.mybutton); btn.setOnClickListener(this);}...
![Page 30: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/30.jpg)
... HelloActivity.java [Código: 5.txt]
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) { Toast.makeText(getApplicationContext(), ((TextView)view).getText(), Toast.LENGTH_SHORT).show();}
public void onClick(View view) { EditText et = (EditText) findViewById(R.id.mytextview); countries.add(et.getText().toString()); adapter.notifyDataSetChanged();}
![Page 31: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/31.jpg)
Prueba
Project -> Run
(Ctrl + Shift + F11)
![Page 32: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/32.jpg)
Experimento
Cerrar la aplicación (oprimiendo "Home")
Arrancarla nuevamente desde el menú de aplicaciones.
¿Cuál es el resultado?
![Page 33: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/33.jpg)
Experimento
El contenido se queda en la lista!
¿Por qué pasa eso?
![Page 34: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/34.jpg)
Experimento 1
Porque Android hace"Activity Lifecycle Management"
y puede mantener activas o cerrar Activities conforme la necesidad
![Page 35: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/35.jpg)
Experimento 2
Cerrar la aplicación (oprimiendo "Home")
Hacer Force Close a traves del menúMenu -> Settings -> Applications -> Manage Apps ->
Hello World -> Force Close
Arrancarla nuevamente desde el menú de aplicaciones.
¿Cuál es el resultado?
![Page 36: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/36.jpg)
Experimento 2
Resultado:
El contenido se fué.
![Page 37: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/37.jpg)
¿Cómo grabar el contenido de unamanera que sea persistente
entre sesiones de mi aplicación?
![Page 38: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/38.jpg)
Fácil -> SharedPreferences
Completo -> Base de datos SQLite
![Page 39: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/39.jpg)
SharedPreferences
SharedPreferences sp =getPreferences(MODE_PRIVATE);
Leer:String x = sp.getString("key", "defaultvalue");
Grabar:SharedPreferences.Editor spe = sp.edit();spe.putString("key", "value");spe.commit();
![Page 40: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/40.jpg)
HelloActivity.java [Código: 6.txt]
void saveData() { SharedPreferences.Editor spe = getPreferences(MODE_PRIVATE).edit(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < countries.size(); i++) sb.append( ((i == 0) ? "" : ";") + countries.get(i)); spe.putString("countries", sb.toString()); spe.commit();}void loadData() { SharedPreferences sp = getPreferences(MODE_PRIVATE); String countryList = sp.getString("countries", "Argentina;Brazil;Chile;Mexico"); for (String country : countryList.split(";")) countries.add(country);}
![Page 41: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/41.jpg)
HelloActivity.java [Código: 6.txt]
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); loadData(); ListView lv = (ListView) findViewById(R.id.mylistview); adapter = new ArrayAdapter<String>(this, R.layout.list_item, countries); lv.setAdapter(adapter); lv.setOnItemClickListener(this); Button btn = (Button) findViewById(R.id.mybutton); btn.setOnClickListener(this);}
![Page 42: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/42.jpg)
HelloActivity.java [Código: 6.txt]
public void onClick(View view) { EditText et = (EditText) findViewById(R.id.mytextview); countries.add(et.getText().toString()); adapter.notifyDataSetChanged();
saveData();}
![Page 43: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/43.jpg)
Prueba
Project -> Run
(Ctrl + Shift + F11)
![Page 44: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/44.jpg)
El "Toast" que aparece cuando se hace click en la lista es aburrido...
¿Cómo se puede implementar una búsqueda en la Web en lugar del Toast?
![Page 45: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/45.jpg)
... HelloActivity.java [Código: 7.txt]
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) { Uri uri = Uri.parse("http://en.wikipedia.org/" + "wiki/" + Uri.encode(countries.get(pos), null)); Intent intent = new Intent( Intent.ACTION_VIEW, uri); startActivity(intent);}
![Page 46: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/46.jpg)
Prueba
Project -> Run
(Ctrl + Shift + F11)
![Page 47: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/47.jpg)
Documentación de Referencia
http://developer.android.com/index.html
•Framework Topics•Android Market Topics•Developing•Publishing•Best Practices•Web Applications•Appendix
![Page 48: Android Bootcamp Santa Fe GTUG](https://reader035.fdocuments.es/reader035/viewer/2022062405/5551e155b4c905f56f8b477e/html5/thumbnails/48.jpg)
¿Preguntas?