Replace search field with a SearchView
app/src/main/java/eu/lepiller/nani/MainActivity.java
14 | 14 | import android.view.Menu; | |
15 | 15 | import android.view.MenuItem; | |
16 | 16 | import android.widget.Button; | |
17 | - | import android.widget.EditText; | |
18 | 17 | import android.widget.LinearLayout; | |
18 | + | import android.widget.SearchView; | |
19 | 19 | import android.widget.TextView; | |
20 | 20 | ||
21 | 21 | import com.moji4j.MojiConverter; | |
22 | 22 | import com.moji4j.MojiDetector; | |
23 | 23 | ||
24 | 24 | import java.util.ArrayList; | |
25 | - | import java.util.List; | |
26 | 25 | ||
27 | 26 | import eu.lepiller.nani.dictionary.DictionaryException; | |
28 | 27 | import eu.lepiller.nani.dictionary.DictionaryFactory; | |
… | |||
34 | 33 | ||
35 | 34 | public class MainActivity extends AppCompatActivity implements OnTaskCompleted<SearchResult>, SharedPreferences.OnSharedPreferenceChangeListener { | |
36 | 35 | private LinearLayout result_view, result_layout; | |
37 | - | private Button search_button; | |
38 | 36 | private TextView feedback_text; | |
39 | - | private EditText search_form; | |
37 | + | private SearchView search_form; | |
40 | 38 | private static ArrayList<Result> savedResults; | |
41 | 39 | private RadicalSelectorView radical_selector; | |
42 | 40 | ||
… | |||
49 | 47 | Toolbar toolbar = findViewById(R.id.toolbar); | |
50 | 48 | setSupportActionBar(toolbar); | |
51 | 49 | ||
52 | - | search_button = findViewById(R.id.search_button); | |
53 | 50 | result_view = findViewById(R.id.results_view); | |
54 | 51 | result_layout = findViewById(R.id.result_layout); | |
55 | 52 | feedback_text = findViewById(R.id.feedback); | |
… | |||
72 | 69 | e.printStackTrace(); | |
73 | 70 | } | |
74 | 71 | ||
75 | - | search_button.setOnClickListener(new View.OnClickListener() { | |
72 | + | search_form.setOnQueryTextListener(new SearchView.OnQueryTextListener() { | |
76 | 73 | @Override | |
77 | - | public void onClick(View v) { | |
74 | + | public boolean onQueryTextSubmit(String query) { | |
78 | 75 | radical_selector.setVisibility(View.INVISIBLE); | |
79 | 76 | result_layout.setVisibility(View.VISIBLE); | |
80 | 77 | ||
81 | - | String text = search_form.getText().toString(); | |
78 | + | String text = search_form.getQuery().toString(); | |
82 | 79 | if (text.isEmpty()) { | |
83 | 80 | Snackbar.make(findViewById(R.id.search_form), getString(R.string.no_search), Snackbar.LENGTH_LONG).show(); | |
84 | - | return; | |
81 | + | return false; | |
85 | 82 | } | |
86 | 83 | ||
87 | 84 | result_view.removeAllViews(); | |
88 | - | search_button.setEnabled(false); | |
85 | + | search_form.setEnabled(false); | |
89 | 86 | feedback_text.setText(R.string.feedback_progress); | |
90 | 87 | ||
91 | 88 | DictionaryFactory.prepare(getApplicationContext()); | |
92 | 89 | new SearchTask(MainActivity.this).execute(text); | |
90 | + | return true; | |
91 | + | } | |
92 | + | ||
93 | + | @Override | |
94 | + | public boolean onQueryTextChange(String newText) { | |
95 | + | return false; | |
93 | 96 | } | |
94 | 97 | }); | |
95 | 98 | ||
… | |||
118 | 121 | @Override | |
119 | 122 | public void onClick(View v) { | |
120 | 123 | String k = ((Button)v).getText().toString(); | |
121 | - | search_form.append(k); | |
124 | + | search_form.setQuery(search_form.getQuery() + k, false); | |
122 | 125 | } | |
123 | 126 | }); | |
124 | 127 | ||
… | |||
197 | 200 | ||
198 | 201 | @Override | |
199 | 202 | public void onTaskCompleted(SearchResult r) { | |
200 | - | search_button.setEnabled(true); | |
203 | + | search_form.setEnabled(true); | |
201 | 204 | if(r.isException()) { | |
202 | 205 | DictionaryException e = r.getException(); | |
203 | 206 | if (e instanceof NoDictionaryException) { | |
… | |||
239 | 242 | feedback_text.setOnClickListener(new View.OnClickListener() { | |
240 | 243 | @Override | |
241 | 244 | public void onClick(View v) { | |
242 | - | search_form.setText(moji); | |
245 | + | search_form.setQuery(moji, true); | |
243 | 246 | feedback_text.setOnClickListener(null); | |
244 | - | search_button.callOnClick(); | |
245 | 247 | } | |
246 | 248 | }); | |
247 | 249 | } |
app/src/main/res/layout/content_main.xml
9 | 9 | tools:context=".MainActivity" | |
10 | 10 | tools:showIn="@layout/activity_main"> | |
11 | 11 | ||
12 | - | <LinearLayout | |
13 | - | android:id="@+id/linearLayout4" | |
12 | + | <SearchView | |
13 | + | android:id="@+id/search_form" | |
14 | 14 | android:layout_width="match_parent" | |
15 | 15 | android:layout_height="wrap_content" | |
16 | - | android:layout_marginTop="8dp" | |
17 | - | android:orientation="horizontal"> | |
18 | - | ||
19 | - | <EditText | |
20 | - | android:id="@+id/search_form" | |
21 | - | android:layout_width="0dp" | |
22 | - | android:layout_height="wrap_content" | |
23 | - | android:layout_marginStart="16dp" | |
24 | - | android:layout_marginLeft="16dp" | |
25 | - | android:layout_marginTop="32dp" | |
26 | - | android:layout_weight="1" | |
27 | - | android:ems="10" | |
28 | - | android:hint="@string/search_hint" | |
29 | - | android:importantForAutofill="no" | |
30 | - | android:inputType="text" /> | |
31 | - | ||
32 | - | <Button | |
33 | - | android:id="@+id/search_button" | |
34 | - | android:layout_width="wrap_content" | |
35 | - | android:layout_height="wrap_content" | |
36 | - | android:layout_marginStart="8dp" | |
37 | - | android:layout_marginLeft="8dp" | |
38 | - | android:layout_marginTop="32dp" | |
39 | - | android:layout_marginEnd="16dp" | |
40 | - | android:layout_marginRight="16dp" | |
41 | - | android:text="@string/search_button" /> | |
42 | - | ||
43 | - | </LinearLayout> | |
16 | + | android:layout_marginStart="16dp" | |
17 | + | android:layout_marginLeft="16dp" | |
18 | + | android:layout_marginEnd="16dp" | |
19 | + | android:layout_marginRight="16dp" | |
20 | + | android:layout_marginTop="32dp" | |
21 | + | android:ems="10" | |
22 | + | android:hint="@string/search_hint" | |
23 | + | android:importantForAutofill="no" | |
24 | + | android:inputType="text" | |
25 | + | android:iconifiedByDefault="false" /> | |
44 | 26 | ||
45 | 27 | <FrameLayout | |
46 | 28 | android:layout_width="match_parent" |