GUA DE LABORATORIO 1
Entorno de desarrollo para Android
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 1
LABORATORIO
Objetivos:
Instalar y configurar el entorno para desarrollar aplicaciones mviles en Android. Identificar la estructura de un proyecto Android en Eclipse. Desarrollar una aplicacin en Android utilizando las clases principales de la librera.
Equipos, Materiales, Programas y Recursos: PC con Sistema Operativo Windows o Linux Eclipse IDE Android Development Tool para Eclipse Android SDK Introduccin: En la presente sesin se detalla los fundamentos para el diseo de interfaces Web para mviles. Seguridad:
Ubicar maletines y/o mochilas en el gabinete al final de aula de laboratorio. No ingresar con lquidos ni comida al aula de laboratorio. Al culminar la sesin de laboratorio, apagar correctamente la computadora y el
monitor. Preparacin: Durante el desarrollo de los temas de clase se tendrn ejercicios explicativos en cada uno de los puntos, ello le dar a la sesin una interaccin de la teora y la parte prctica, ya que en todo el momento el alumno podr comprobar en su propia PC, todos los tems del manual. Procedimiento y Resultados:
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 2
PUESTA EN MARCHA
1) Configuraciones iniciales del proyecto Android
En el AndroidManifest.xml configurar la aplicacin:
allowBackup: Por defecto est en true. Cuando est en true los datos de la aplicacin
pueden ser copiados para seguridad y luego restaurados. Esto puede traer
consecuencias de seguridad porque si el Smartphone tiene habilitado el USB
Debugging entonces se podr copiar esos datos fuera del dispositivo.
icon: Puede cambiar el cono de la aplicacin que aparece en el escritorio cambiando la imagen en la carpeta /res/drawable. Puede configurar diversos conos que se
mostrarn dependiendo del tamao de la pantalla.
label: Determina el ttulo de la aplicacin theme: Es posible personalizar configuraciones de diseo para toda la aplicacin en el
archivo /res/values/styles.xml :
match_parentwrap_content#ff0000serif
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 3
2) Configuraciones del Activity
En el AndroidManifest.xml tenemos la etiqueta . Esta etiqueta lleva
dentro al conjunto de de la aplicacin.
name: Determina la clase Java del Activity. label: Determina el nombre del Activity intent-filter: El ACTION MAIN indica que el Activity va a ser el primero en
iniciarse en el proyecto, sin ninguna data de entrada y no retorna nada. El
CATEGORY LAUNCHER tambin indica que el activity ser el primero en
iniciarse. Fuente: http://developer.android.com/guide/components/intents-filters.html
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 4
3) Clase Activity El Activity es una Clase de Java subclase de Activity y que controla el ciclo de vida de la aplicacin.
MainActivity.java package mod1.lab1; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Desde el Activity se llaman a dos XML: /res/layout/activity_main.xml /res/menu/main.xml : Contiene al men de la aplicacin
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 5
4) Archivo /res/layout/activity_main.xml
activity_main.xml
5) Archivo /res/menu/activity_main.xml
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 6
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 7
LAYOUTS
En el material terico se detallan los tipos de Layouts:
a) GridLayout b) LinearLayout (vertical y horizontal) c) RelativeLayout (Layout usado por defecto en Eclipse) d) FrameLayout (Layout contenedor usado por defecto en Eclipse) e) TableLayout f) Fragment
Para modificar el tipo de Layout le damos click derecho en la siguiente opcin:
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 8
GridLayout GridLayoutActivity.java package mod1.lab1; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class GridLayoutActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_grid_layout); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.grid_layout, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 9
activity_grid_layout.xml
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 10
EJERCICIO Disear la siguiente pantalla con GridLayout:
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 11
LinearLayout
Disear una pantalla que permita buscar canciones. Configurar los valores de orientation: vertical y horizontal. Programar el mtodo de ejecucin del botn.
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 12
LinearLayoutActivity.java package mod1.lab1; import android.app.Activity; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class LinearLayoutActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); setContentView(R.layout.activity_linear_layout); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.linear_layout, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 13
activity_linear_layout.xml
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 14
EJERCICIO Disear las siguientes pantallas:
Para iniciar otro Activiy, colocar en el mtodo del botn: Intent intent = new Intent(this, BienvenidaActivity.class); startActivity(intent);
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 15
TableLayout
TableLayoutActivity.java package mod1.lab1; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class TableLayoutActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_table_layout); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.table_layout, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 16
activity_table_layout.xml
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 17
android:layout_span="2" android:text="Ingresar" />
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 18
EJERCICIO
Disear la siguiente pantalla:
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 19
Fragment
FragmentsActivity.java package mod1.lab1; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class FragmentsActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragments); } @Override public boolean onCreateOptionsMenu(Menu menu) {
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 20
// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.fragments, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void cambiarFragmento(View view) { Fragment fr; if(view == findViewById(R.id.button2)) { fr = new FragmentoDos(); }else { fr = new FragmentoUno(); } FragmentManager fm = getFragmentManager(); FragmentTransaction fragmentTransaction = fm.beginTransaction(); fragmentTransaction.replace(R.id.fragmento_principal, fr); fragmentTransaction.commit(); } }
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 21
activity_fragments.xml
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 22
Crear las siguientes dos clases Java: packagecurso.android.layouts.fragments;importandroid.app.Fragment;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importcurso.android.R;publicclassFragmentoUnoextendsFragment{ @Override publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer, BundlesavedInstanceState){ returninflater.inflate(R.layout.fragmento_uno,container,false); }} packagecurso.android.layouts.fragments;importandroid.app.Fragment;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importcurso.android.R;publicclassFragmentoDosextendsFragment{ @Override publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer, BundlesavedInstanceState){ returninflater.inflate(R.layout.fragmento_dos,container,false); }}
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 23
Crear los siguientes dos XML: fragmento_uno.xml fragmento_dos.xml
Tecsup
Dpto. de Informtica David Rodrguez - [email protected] Pg. 24
Conclusiones: En la presente sesin, se detall el entorno para el desarrollo de aplicaciones mviles en Android.
Recommended