Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Android por onde começar? Mini Curso Erbase 2015

969 views

Published on

Minicurso sobre desenvolvimento de aplicações com android. Passo a passo do desenvolvimento de uma App de TODO. Utilizando Android Studio. CRUD + Banco de dados (SQLite)

Published in: Mobile
  • Be the first to comment

Android por onde começar? Mini Curso Erbase 2015

  1. 1. Android, por onde começar ?
  2. 2. Android, por onde comeCar ? Mario Jorge Pereira
  3. 3. 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
  4. 4. Desenvolvimento
  5. 5. Por onde começar? http://developer.android.com
  6. 6. Run APP Android application
  7. 7. manifests/ 
 (AndroidManifest.xml) java/ 
 (código fonte: .java) res/ 
 (recursos: layouts, strigs, icones …)
  8. 8. <RelativeLayout ... android:layout_width=“match_parent”android:layout_height="match_parent" ...> <TextView android:text=“@string/hello_world"android:layout_width="wrap_content"android:layout_height="wrap_content" /></RelativeLayout> activity_main.xml
  9. 9. <RelativeLayout ... > <EditText android:layout_width="match_parent"android:layout_height="30dp" /> <Button android:layout_width="match_parent"android:layout_height="30dp" /> <TextView .../> </RelativeLayout> activity_main.xml
  10. 10. <LinearLayout ... android:orientation="vertical" > <EditText android:layout_width="match_parent"android:layout_height="wrap_content" /><Button android:text="@string/txtbotao"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextView android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout> activity_main.xml
  11. 11. <?xml version="1.0" encoding=“utf-8"?> <resources> <string name="app_name">Todo</string> <string name="hello_world">Hello world!</string><string name="action_settings">Settings</string><string name=“txtbotao">Copiar</string></resources> strings.xml
  12. 12. Activity XMLpublic class OlaMundo extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ola_mundo); } }
  13. 13. package br.com.mariojp.todo; import android.os.Bundle;import android.support.v7.app.ActionBarActivity;public class MainActivity extends ActionBarActivity {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); } ... } MainActivity.java
  14. 14. /* AUTO-GENERATED FILE. DO NOT MODIFY.*/ public final class R { public static final class attr { } public static final class dimen { public static final int activity_horizontal_margin=0x7f040000; public static final int activity_vertical_margin=0x7f040001; } public static final class layout { public static final int activity_main=0x7f030000; } ... } R.java
  15. 15. package br.com.mariojp.todo; import android.support.v7.app.ActionBarActivity; import android.os.Bundle;import android.util.Log; public class MainActivity extends ActionBarActivity {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); Log.i("Rotulo","Mensagem"); } } MainActivity.java
  16. 16. Log.[nivel](“TAG”,“MENSAGEM”); Log.v (Verbose) Log.d (Debug) Log.i (Info) Log.w (Warning) Log.e (Erro) Log.wtf (...) Log
  17. 17. public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { ... }protected void onStart() { ... } protected void onResume() { ... } protected void onRestart() { ... } protected void onPause() { ... } protected void onStop() { ... } protected void onDestroy() { ... } } MainActivity.java
  18. 18. Run As.. Android application
  19. 19. ... @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); Button botao = (Button) findViewById(?); } ... MainActivity.java
  20. 20. <LinearLayout ... > <EditText android:id="@+id/campo"... /> <Button android:id="@+id/botao"android:text="@string/txtbotao"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextView android:id=“@+id/rotulo"... /> </LinearLayout> activity_main.xml
  21. 21. ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); Button botao = (Button) findViewById(R.id.botao);} ... MainActivity.java
  22. 22. ... @Override protected void onCreate(Bundle savedInstanceState) { ... Button botao = (Button) findViewById(R.id.botao); botao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {Log.i("TODO","Clicou o Botao");} }); } ... MainActivity.java
  23. 23. ... @Override protected void onCreate(Bundle savedInstanceState) { ... Button botao = (Button) findViewById(R.id.botao); final EditText campo = (EditText) findViewById(R.id.campo); final TextView rotulo = (TextView) findViewById(R.id.rotulo); botao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {rotulo.setText(campo.getText());} }); } ... MainActivity.java
  24. 24. Run As.. Android application
  25. 25. public class ListActivity extends ActionBarActivity {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.?); } } ListActivity.java
  26. 26. <LinearLayout ... > <ListView android:id="@+id/lista"android:layout_width="match_parent"android:layout_height="match_parent"> </ListView> </LinearLayout> activity_list.xml
  27. 27. ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_list); ListView lista = (ListView) findViewById(R.id.lista); } ... ListActivity.java
  28. 28. ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_list); ListView lista = (ListView) findViewById(R.id.lista); String[] dados = new String[]{"IR A ERBASE”,"ESTUDAR PARA PROVA","COMPRAR CADERNO”};ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, dados);lista.setAdapter(adapter); } ... ListActivity.java
  29. 29. Run As.. Android application
  30. 30. <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="br.com.mariojp.todo" > <application ... > <activity android:name=“.MainActivity" android:label="Todo" > </activity> <activity android:name=".ListActivity" android:label="Todo" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> AndroidManifest.xml
  31. 31. Run As.. Android application
  32. 32. ... lista.setOnItemClickListener(new AdapterView.OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> adapter, View view, int position, long id){ Toast.makeText(ListActivity.this,"Posicao "+ position,Toast.LENGTH_SHORT).show(); } }); ... ListActivity.java
  33. 33. Run As.. Android application
  34. 34. ... lista.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapter, View view, int position, long id) { Toast.makeText(ListActivity.this,adapter.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();return false; } }); ... ListActivity.java
  35. 35. Run As.. Android application
  36. 36. ... lista.setOnItemClickListener(new AdapterView.OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> adapter, View view, int position, long id){ Intent intent = new Intent(ListActivity.this,MainActivity.class); startActivity(intent); } }); ... ListActivity.java
  37. 37. Run As.. Android application
  38. 38. <LinearLayout ... android:orientation="vertical" > <TextView android:id=“@+id/rotulo”... /> <Button android:id=“@+id/botao”... /> </LinearLayout> activity_main.xml
  39. 39. ... @Override protected void onCreate(Bundle savedInstanceState) { ... Button botao = (Button) findViewById(R.id.botao); final TextView rotulo = (TextView) findViewById(R.id.rotulo); botao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {finish(); } }); } ... MainActivity.java
  40. 40. <?xml version="1.0" encoding=“utf-8"?> <resources> <string name="app_name">Todo</string> <string name="hello_world">Hello world!</string><string name="action_settings">Settings</string><string name=“txtbotao">Voltar</string></resources> strings.xml
  41. 41. ... lista.setOnItemClickListener(new AdapterView.OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> adapter, View view, int posicao, long id){ Intent intent = new Intent(ListActivity.this, MainActivity.class); String valor = adapter.getItemAtPosition(posicao).toString(); intent.putExtra("tarefa", valor);startActivity(intent);} }); ... ListActivity.java
  42. 42. ... @Override protected void onCreate(Bundle savedInstanceState) { ... Button botao = (Button) findViewById(R.id.botao); TextView rotulo = (TextView) findViewById(R.id.rotulo);Intent intent = getIntent();String valor = (String) intent.getSerializableExtra("tarefa"); rotulo.setText(valor); botao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {finish(); } }); } ... MainActivity.java
  43. 43. ... @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_list, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId();if (id == R.id.action_settings) {return true; } return super.onOptionsItemSelected(item); } ... ListActivity.java
  44. 44. <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".ListActivity" > <item android:id="@+id/action_settings" android:title="@string/action_settings" android:orderInCategory="100"app:showAsAction="never" /> </menu> menu_list.xml
  45. 45. <menu ...> <item android:id="@+id/action_settings" ... /> <item android:id="@+id/action_nova"android:title="Nova"android:orderInCategory="100"app:showAsAction="always" /> </menu> menu_list.xml
  46. 46. ... @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId();switch (id){ case R.id.action_settings:Toast.makeText(this,"OPCOES",Toast.LENGTH_SHORT).show(); return true; case R.id.action_nova:Toast.makeText(this,"NOVA",Toast.LENGTH_SHORT).show(); return true; default: return super.onOptionsItemSelected(item); } } ... ListActivity.java
  47. 47. <menu ...> <item android:id="@+id/editar"android:orderInCategory="100"android:title="Editar"/> <item android:id="@+id/excluir"android:orderInCategory="100"android:title="Excluir"/> </menu> context_menu.xml
  48. 48. ... @Override public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.context_menu, menu);} ... ListActivity.java
  49. 49. ... @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) {case R.id.editar: Toast.makeText(this,"EDITAR",Toast.LENGTH_SHORT).show(); return true; case R.id.excluir: Toast.makeText(this,"EXLUIR",Toast.LENGTH_SHORT).show(); return true; default: return super.onContextItemSelected(item); } } ... ListActivity.java
  50. 50. public class Tarefa { private Long id; private String titulo; //get's e set's } Tarefa.java
  51. 51. <LinearLayout ...> <TextView android:text="Tarefa"... /> <EditText android:id="@+id/titulo"... /> <Button android:id="@+id/botao"android:text="Salvar"android:onClick=“salvar"... /> </LinearLayout> activity_cadastro.xml
  52. 52. ... public void salvar(View view){Tarefa tarefa = new Tarefa();tarefa.setTitulo(titulo.getText().toString()); Toast.makeText(this,”Tarefa OK!",Toast.LENGTH_SHORT ); } ... CadastroActivity.java
  53. 53. Run As.. Android application
  54. 54. Banco de dados
  55. 55. public class TarefaDAO extends SQLiteOpenHelper {private static final String DB_NAME = "tarefa.db"; private static final int DB_VERSION = 1; public TarefaDAO(Context context) {super(context, DB_NAME, null, DB_VERSION);} @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) { } } TarefaDAO.java
  56. 56. ... @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE TAREFAS (id INTEGER PRIMARY KEY, titulo TEXT UNIQUE NOT NULL);");} public void insere(Tarefa tarefa) {ContentValues valores = new ContentValues(); valores.put("titulo", tarefa.getTitulo()); getWritableDatabase().insert("TAREFAS", null, valores); } ... TarefaDAO.java
  57. 57. ... public List<Tarefa> listar() {List<Tarefa> tarefas = new ArrayList<Tarefa>(); Cursor cursor = getReadableDatabase().query("TAREFAS", null, null, null, null, null, null);while (cursor.moveToNext()) {Tarefa tarefa = new Tarefa();tarefa.setId(cursor.getLong(cursor.getColumnIndex("id"))); String titulo = cursor.getString(cursor.getColumnIndex(“titulo")); tarefa.setTitulo(titulo);tarefas.add(tarefa);} return tarefas; } ... TarefaDAO.java
  58. 58. ... private ListView lista;... @Override protected void onCreate(Bundle savedInstanceState) { lista = (ListView) findViewById(R.id.lista); TarefaDAO dao = new TarefaDAO(this);List<Tarefa> dados = dao.listar();int layout = android.R.layout.simple_list_item_1; ArrayAdapter<Tarefa> adapter =new ArrayAdapter<Tarefa>(this,layout, dados); lista.setAdapter(adapter); } ... ListActivity.java
  59. 59. ... private ListView lista;... @Override protected void onResume() {super.onResume() lista = (ListView) findViewById(R.id.lista); TarefaDAO dao = new TarefaDAO(this);List<Tarefa> dados = dao.listar();int layout = android.R.layout.simple_list_item_1; ArrayAdapter<Tarefa> adapter =new ArrayAdapter<Tarefa>(this,layout, dados); lista.setAdapter(adapter);} ... ListActivity.java
  60. 60. public class Tarefa implements Serializable { ... } Tarefa.java
  61. 61. ... lista.setOnItemClickListener(new AdapterView.OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> adapter, View view, int posicao, long id){ Intent intent = new Intent(ListActivity.this, MainActivity.class); Tarefa valor = (Tarefa) adapter.getItemAtPosition(posicao); intent.putExtra("tarefa", valor);startActivity(intent);} }); ... ListActivity.java
  62. 62. ... @Override protected void onCreate(Bundle savedInstanceState) { ... Button botao = (Button) findViewById(R.id.botao); TextView rotulo = (TextView) findViewById(R.id.rotulo);Intent intent = getIntent();Tarefa tarefa = (Tarefa) intent.getSerializableExtra("tarefa"); rotulo.setText(tarefa.getTitulo); botao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {finish(); } }); } ... MainActivity.java
  63. 63. ... public void deletar(Tarefa tarefa) {String[] args = {tarefa.getId().toString()}; getWritableDatabase().delete("TAREFAS", "id=?", args); } ... TarefaDAO.java
  64. 64. ... private Tarefa self = null;... lista.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapter, View view, int position, long id) { sel = (Tarefa) adapter.getItemAtPosition(posicao); return false; } }); ... ListActivity.java
  65. 65. ... @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) {case R.id.editar: Toast.makeText(this,"EDITAR",Toast.LENGTH_SHORT).show(); return true; case R.id.excluir: TarefaDAO dao = new TarefaDAO(this); dao.deletar(tarefaSelecionada);return true; default: return super.onContextItemSelected(item); } } ... ListActivity.java
  66. 66. ... @Override protected void carregaLista() {lista = (ListView) findViewById(R.id.lista); TarefaDAO dao = new TarefaDAO(this);List<Tarefa> dados = dao.listar();int layout = android.R.layout.simple_list_item_1; ArrayAdapter<Tarefa> adapter =new ArrayAdapter<Tarefa>(this,layout, dados); lista.setAdapter(adapter);} ... ListActivity.java
  67. 67. ... @Override protected void onResume() {super.onResume() this.carregaLista();} ... ListActivity.java
  68. 68. ... @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) {case R.id.editar: Toast.makeText(this,"EDITAR",Toast.LENGTH_SHORT).show(); return true; case R.id.excluir: TarefaDAO dao = new TarefaDAO(this); dao.deletar(tarefaSelecionada);this.carregaLista();return true; default: return super.onContextItemSelected(item); } } ... ListActivity.java
  69. 69. Esta obra está licenciada sob a licença Creative Commons Atribuição-CompartilhaIgual 3.0 Não Adaptada. Para ver uma cópia desta licença, visite http://creativecommons.org/licenses/by-sa/3.0/.
  70. 70. ANDROID Mario Jorge Pereira Como me encontrar? http://www.mariojp.com.br twitter.com/@mariojp mariojp@gmail.com

×