Neste artigo vamos tratar a pesquisa e adicionar um menu para acessar uma outra tela.
O arquivo strings.xml está assim no momento.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<resources> <string name="app_name">Onde Treinar - micheladrianomedeiros.com.br</string> <string name="app_name_reviews">Onde Treinar - Reviews</string> <string name="app_name_review">Onde Treinar - Review</string> <string name="app_short_name">Onde Treinar</string> <string name="menu_get_reviews">Reviews</string> <string name="menu_web_review">Review Completo</string> <string name="menu_map_review">Local no Mapa</string> <string name="menu_call_review">Telefone</string> <string name="menu_change_criteria">Mudar Review</string> <string name="menu_get_next_page">Próxima Página</string> <string name="intro_blurb_criteria">Acesso ao Review</string> <string name="intro_blurb_detail">Detalhes do Review</string> <string name="alert_label">Você deve digitar o nome ou cidade antes de fazer uma pesquisa</string> <string name="location_not_supplied_message"></string> <string-array name="types_gym"> <item>Todos</item> <item>Academia de Dança</item> <item>Academia de Luta</item> <item>Academia de Musculação</item> <item>Academia de Nicho</item> <item>Artes Marciais</item> <item>Ballet Fitness</item> <item>Boutique</item> <item>Crossfit</item> <item>Estúdio de Pilates</item> <item>Full Service</item> <item>Hidroginástica</item> <item>Low Cost</item> <item>Muay Thai</item> <item>Samba</item> <item>Spinning</item> <item>Zumba</item> </string-array> </resources> |
Crie uma activity com o nome ReviewList e na view da ReviewList digite o seguinte código:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?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:orientation="vertical"> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:drawSelectorOnTop="false" /> <TextView android:id="@+id/empty" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="" /> </LinearLayout> |
Agora no arquivo MainActivity.java vamos adicionar mais código.
Primeiro ao pesquisar academia vamos mostrar uma mensagem se o campo de academia ou local estiver vazio.
A MainActivity.java ficou assim:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
package br.com.micheladrianomedeiros.ondetreinar; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private Spinner spinnerTypesGym; private Button buttonSearchGym; private EditText editTextSearchGym; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextSearchGym = findViewById(R.id.search_gym); spinnerTypesGym = findViewById(R.id.spinner_type_gym); buttonSearchGym = findViewById(R.id.button_search_gym); buttonSearchGym.setOnClickListener(buttonClickListener); } private View.OnClickListener buttonClickListener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_search_gym: boolean b = handleSearch(); if (b) { Log.i("procurando", editTextSearchGym.getText().toString()); Log.i("tipo academia", spinnerTypesGym.getSelectedItem().toString()); Log.i("Buscar", "Vamos fazer a busca"); } break; case View.NO_ID: default: Log.i("erro", "nada"); break; } } }; private boolean handleSearch() { if ((editTextSearchGym.getText() == null) || editTextSearchGym.getText().toString().equals("")) { new AlertDialog.Builder(this).setTitle(R.string.alert_label). setMessage(R.string.location_not_supplied_message). setPositiveButton("Continue", new android.content.DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { // Just close alert. } }).show(); return false; } return true; } } |
Vamos agora adicionar um menu e quando clicar no item do menu abrir a tela ReviewList.
Declare essa variável: private static final int MENU_GET_REVIEWS = Menu.FIRST;
Adicione este código no MainActivity.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, this.MENU_GET_REVIEWS, 0, R.string.menu_get_reviews).setIcon( android.R.drawable.ic_menu_more); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_GET_REVIEWS: Intent intent = new Intent(this, ReviewList.class); startActivity(intent); return true; } return false; } |
Agora quando você clicar no menu na opção Reviews, você irá para a tela do ReviewList.
Código do arquivo styles.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="intro_blurb"> <item name="android:textSize">22sp</item> <item name="android:textColor">#ee7620</item> <item name="android:textStyle">bold</item> </style> <style name="label"> <item name="android:textSize">18sp</item> <item name="android:textColor">#ffffff</item> </style> <style name="edit_text"> <item name="android:textSize">16sp</item> <item name="android:textColor">#000000</item> </style> <style name="view_text"> <item name="android:textSize">16sp</item> <item name="android:textColor">#cccccc</item> </style> <style name="spinner_item"> <item name="android:textSize">16sp</item> <item name="android:textColor">#000000</item> </style> <style name="spinner_item_dropdown"> <item name="android:textSize">16sp</item> <item name="android:textColor">#000000</item> </style> <!-- this is the style for the container ListView, not the rows --> <!-- the rows are defined in code, see ReviewAdapter.ReviewListView --> <style name="review_list"> <item name="android:background">#000000</item> </style> </resources> |
Código completo do MainActivity.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
package br.com.micheladrianomedeiros.ondetreinar; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private Spinner spinnerTypesGym; private Button buttonSearchGym; private EditText editTextSearchGym; private static final int MENU_GET_REVIEWS = Menu.FIRST; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextSearchGym = findViewById(R.id.search_gym); spinnerTypesGym = findViewById(R.id.spinner_type_gym); buttonSearchGym = findViewById(R.id.button_search_gym); buttonSearchGym.setOnClickListener(buttonClickListener); } private View.OnClickListener buttonClickListener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_search_gym: boolean b = handleSearch(); if (b) { Log.i("procurando", editTextSearchGym.getText().toString()); Log.i("tipo academia", spinnerTypesGym.getSelectedItem().toString()); Log.i("Buscar", "Vamos fazer a busca"); } break; case View.NO_ID: default: Log.i("erro", "nada"); break; } } }; private boolean handleSearch() { if ((editTextSearchGym.getText() == null) || editTextSearchGym.getText().toString().equals("")) { new AlertDialog.Builder(this).setTitle(R.string.alert_label). setMessage(R.string.location_not_supplied_message). setPositiveButton("Continue", new android.content.DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { // Just close alert. } }).show(); return false; } return true; } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, this.MENU_GET_REVIEWS, 0, R.string.menu_get_reviews).setIcon( android.R.drawable.ic_menu_more); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_GET_REVIEWS: Intent intent = new Intent(this, ReviewList.class); startActivity(intent); return true; } return false; } } |
Deixe um comentário