Move search view to app bar
CHANGELOG.md
| 27 | 27 | ### UI | |
| 28 | 28 | ||
| 29 | 29 | * Added support for dark theme. | |
| 30 | + | * Moved search bar to the app bar, leaving more space for results. | |
| 30 | 31 | ||
| 31 | 32 | Changes in 0.3.2.1 | |
| 32 | 33 | ------------------ |
app/src/main/java/eu/lepiller/nani/MainActivity.java
| 18 | 18 | import android.view.MenuItem; | |
| 19 | 19 | import android.widget.Button; | |
| 20 | 20 | import android.widget.LinearLayout; | |
| 21 | - | import android.widget.SearchView; | |
| 21 | + | import androidx.appcompat.widget.SearchView; | |
| 22 | 22 | import android.widget.TextView; | |
| 23 | 23 | ||
| 24 | 24 | import com.google.android.material.tabs.TabLayout; | |
… | |||
| 59 | 59 | ||
| 60 | 60 | result_layout = findViewById(R.id.result_layout); | |
| 61 | 61 | feedback_text = findViewById(R.id.feedback); | |
| 62 | - | search_form = findViewById(R.id.search_form); | |
| 63 | 62 | radical_selector = findViewById(R.id.radical_selector); | |
| 64 | 63 | TabLayout tabLayout = findViewById(R.id.tab_layout); | |
| 65 | 64 | viewPager2 = findViewById(R.id.pager); | |
… | |||
| 98 | 97 | e.printStackTrace(); | |
| 99 | 98 | } | |
| 100 | 99 | ||
| 101 | - | search_form.setOnQueryTextListener(new SearchView.OnQueryTextListener() { | |
| 102 | - | @Override | |
| 103 | - | public boolean onQueryTextSubmit(String query) { | |
| 104 | - | radical_selector.setVisibility(View.INVISIBLE); | |
| 105 | - | result_layout.setVisibility(View.VISIBLE); | |
| 106 | - | ||
| 107 | - | String text = search_form.getQuery().toString(); | |
| 108 | - | if (text.isEmpty()) { | |
| 109 | - | Snackbar.make(findViewById(R.id.search_form), getString(R.string.no_search), Snackbar.LENGTH_LONG).show(); | |
| 110 | - | return false; | |
| 111 | - | } | |
| 112 | - | ||
| 113 | - | pagerAdapter.setKanjiResults(new ArrayList<KanjiResult>()); | |
| 114 | - | pagerAdapter.setResults(new ArrayList<Result>()); | |
| 115 | - | pagerAdapter.notifyDataSetChanged(); | |
| 116 | - | ||
| 117 | - | search_form.setEnabled(false); | |
| 118 | - | feedback_text.setText(R.string.feedback_progress); | |
| 119 | - | ||
| 120 | - | DictionaryFactory.prepare(getApplicationContext()); | |
| 121 | - | new SearchTask(MainActivity.this).execute(text); | |
| 122 | - | return true; | |
| 123 | - | } | |
| 124 | - | ||
| 125 | - | @Override | |
| 126 | - | public boolean onQueryTextChange(String newText) { | |
| 127 | - | return false; | |
| 128 | - | } | |
| 129 | - | }); | |
| 130 | - | ||
| 131 | 100 | radical_button.setOnClickListener(new View.OnClickListener() { | |
| 132 | 101 | @Override | |
| 133 | 102 | public void onClick(View v) { | |
… | |||
| 135 | 104 | DictionaryFactory.getRadicalDictionary(getApplicationContext()); | |
| 136 | 105 | radical_selector.setVisibility(View.VISIBLE); | |
| 137 | 106 | result_layout.setVisibility(View.INVISIBLE); | |
| 107 | + | search_form.setIconified(false); | |
| 138 | 108 | } catch (NoDictionaryException e) { | |
| 139 | - | Snackbar.make(findViewById(R.id.search_form), getString(R.string.no_radical_dict), Snackbar.LENGTH_LONG).show(); | |
| 109 | + | Snackbar.make(search_form, getString(R.string.no_radical_dict), Snackbar.LENGTH_LONG).show(); | |
| 140 | 110 | } | |
| 141 | 111 | } | |
| 142 | 112 | }); | |
… | |||
| 144 | 114 | radical_selector.setOnCloseEventListener(new View.OnClickListener() { | |
| 145 | 115 | @Override | |
| 146 | 116 | public void onClick(View v) { | |
| 147 | - | radical_selector.setVisibility(View.INVISIBLE); | |
| 148 | - | result_layout.setVisibility(View.VISIBLE); | |
| 117 | + | closeRadicals(); | |
| 149 | 118 | } | |
| 150 | 119 | }); | |
| 151 | 120 | ||
… | |||
| 166 | 135 | }); | |
| 167 | 136 | } | |
| 168 | 137 | ||
| 138 | + | private void closeRadicals() { | |
| 139 | + | radical_selector.setVisibility(View.INVISIBLE); | |
| 140 | + | result_layout.setVisibility(View.VISIBLE); | |
| 141 | + | } | |
| 142 | + | ||
| 169 | 143 | @Override | |
| 170 | 144 | public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { | |
| 171 | 145 | Log.d("MAIN", key + " preference changed."); | |
… | |||
| 279 | 253 | if(r.isException()) { | |
| 280 | 254 | DictionaryException e = r.getException(); | |
| 281 | 255 | if (e instanceof NoDictionaryException) { | |
| 282 | - | Snackbar.make(findViewById(R.id.search_form), getString(R.string.no_dic), | |
| 256 | + | Snackbar.make(search_form, getString(R.string.no_dic), | |
| 283 | 257 | Snackbar.LENGTH_LONG).show(); | |
| 284 | 258 | feedback_text.setText(getString(R.string.no_dic)); | |
| 285 | 259 | } else if(e instanceof IncompatibleFormatException) { | |
| 286 | - | Snackbar.make(findViewById(R.id.search_form), String.format(getString(R.string.incompatible_format), | |
| 260 | + | Snackbar.make(search_form, String.format(getString(R.string.incompatible_format), | |
| 287 | 261 | ((IncompatibleFormatException) e).getName()), Snackbar.LENGTH_LONG).show(); | |
| 288 | 262 | feedback_text.setText(String.format(getString(R.string.incompatible_format), | |
| 289 | 263 | ((IncompatibleFormatException) e).getName())); | |
… | |||
| 340 | 314 | public boolean onCreateOptionsMenu(Menu menu) { | |
| 341 | 315 | // Inflate the menu; this adds items to the action bar if it is present. | |
| 342 | 316 | getMenuInflater().inflate(R.menu.menu_main, menu); | |
| 343 | - | return true; | |
| 317 | + | ||
| 318 | + | MenuItem searchItem = menu.findItem(R.id.action_search); | |
| 319 | + | search_form = (SearchView) searchItem.getActionView(); | |
| 320 | + | ||
| 321 | + | search_form.setQueryHint(getResources().getString(R.string.search_hint)); | |
| 322 | + | ||
| 323 | + | search_form.setOnCloseListener(new SearchView.OnCloseListener() { | |
| 324 | + | @Override | |
| 325 | + | public boolean onClose() { | |
| 326 | + | closeRadicals(); | |
| 327 | + | return false; | |
| 328 | + | } | |
| 329 | + | }); | |
| 330 | + | ||
| 331 | + | search_form.setOnQueryTextListener(new SearchView.OnQueryTextListener() { | |
| 332 | + | @Override | |
| 333 | + | public boolean onQueryTextSubmit(String query) { | |
| 334 | + | radical_selector.setVisibility(View.INVISIBLE); | |
| 335 | + | result_layout.setVisibility(View.VISIBLE); | |
| 336 | + | ||
| 337 | + | String text = search_form.getQuery().toString(); | |
| 338 | + | if (text.isEmpty()) { | |
| 339 | + | Snackbar.make(search_form, getString(R.string.no_search), Snackbar.LENGTH_LONG).show(); | |
| 340 | + | return false; | |
| 341 | + | } | |
| 342 | + | ||
| 343 | + | pagerAdapter.setKanjiResults(new ArrayList<KanjiResult>()); | |
| 344 | + | pagerAdapter.setResults(new ArrayList<Result>()); | |
| 345 | + | pagerAdapter.notifyDataSetChanged(); | |
| 346 | + | ||
| 347 | + | search_form.setEnabled(false); | |
| 348 | + | feedback_text.setText(R.string.feedback_progress); | |
| 349 | + | ||
| 350 | + | DictionaryFactory.prepare(getApplicationContext()); | |
| 351 | + | new SearchTask(MainActivity.this).execute(text); | |
| 352 | + | return true; | |
| 353 | + | } | |
| 354 | + | ||
| 355 | + | @Override | |
| 356 | + | public boolean onQueryTextChange(String newText) { | |
| 357 | + | return false; | |
| 358 | + | } | |
| 359 | + | }); | |
| 360 | + | ||
| 361 | + | return super.onCreateOptionsMenu(menu); | |
| 344 | 362 | } | |
| 345 | 363 | ||
| 346 | 364 | @Override | |
app/src/main/res/layout/content_main.xml
| 9 | 9 | tools:context=".MainActivity" | |
| 10 | 10 | tools:showIn="@layout/activity_main"> | |
| 11 | 11 | ||
| 12 | - | <SearchView | |
| 13 | - | android:id="@+id/search_form" | |
| 14 | - | android:layout_width="match_parent" | |
| 15 | - | android:layout_height="wrap_content" | |
| 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" /> | |
| 26 | - | ||
| 27 | 12 | <FrameLayout | |
| 28 | 13 | android:layout_width="match_parent" | |
| 29 | 14 | android:layout_height="match_parent"> |
app/src/main/res/menu/menu_main.xml
| 3 | 3 | xmlns:tools="http://schemas.android.com/tools" | |
| 4 | 4 | tools:context="eu.lepiller.nani.MainActivity"> | |
| 5 | 5 | <item | |
| 6 | + | android:id="@+id/action_search" | |
| 7 | + | android:title="@string/action_search" | |
| 8 | + | android:icon="@android:drawable/ic_menu_search" | |
| 9 | + | app:showAsAction="ifRoom" | |
| 10 | + | app:actionViewClass="androidx.appcompat.widget.SearchView" /> | |
| 11 | + | <item | |
| 6 | 12 | android:id="@+id/action_dictionaries" | |
| 7 | 13 | android:title="@string/action_dictionaries" /> | |
| 8 | 14 | <item |
app/src/main/res/values/strings.xml
| 1 | 1 | <resources> | |
| 2 | 2 | <string name="app_name" translatable="false">Nani?</string> | |
| 3 | + | <string name="action_search">Search</string> | |
| 3 | 4 | <string name="action_settings">Settings</string> | |
| 4 | 5 | <string name="action_dictionaries">Manage Dictionaries</string> | |
| 5 | 6 | <string name="action_help">Help</string> |