]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryActivity.java
Merge pull request #26 from Kteby/master
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
index bd6e7c703db21b9e29f54bebccce081bc8fd5ecc..f8cca586e94f6e6cb7cb27a01a184a7570a84bb4 100644 (file)
@@ -129,6 +129,8 @@ public class DictionaryActivity extends ActionBarActivity {
 
     List<RowBase> rowsToShow = null; // if not null, just show these rows.
 
+    final Random rand = new Random();
+
     final Handler uiHandler = new Handler();
 
     private final Executor searchExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
@@ -168,7 +170,7 @@ public class DictionaryActivity extends ActionBarActivity {
     ImageButton languageButton;
     SearchView.OnQueryTextListener onQueryTextListener;
 
-    MenuItem nextWordMenuItem, previousWordMenuItem;
+    MenuItem nextWordMenuItem, previousWordMenuItem, randomWordMenuItem;
 
     // Never null.
     private File wordList = null;
@@ -638,6 +640,13 @@ public class DictionaryActivity extends ActionBarActivity {
         }
     }
 
+    private void hideKeyboard() {
+        Log.d(LOG, "Hide soft keyboard.");
+        searchView.clearFocus();
+        InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
+        manager.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
+    }
+
     void updateLangButton() {
         final LanguageResources languageResources =
                 DictionaryApplication.isoCodeToResources.get(index.shortName);
@@ -713,7 +722,6 @@ public class DictionaryActivity extends ActionBarActivity {
                     final IndexInfo indexInfo = dictionaryInfo.indexInfos.get(i);
                     final View button = application.createButton(parent.getContext(),
                             dictionaryInfo, indexInfo);
-                    button.setLayoutParams(new LinearLayout.LayoutParams(application.languageButtonPixels, application.languageButtonPixels * 2 / 3));
                     final IntentLauncher intentLauncher = new IntentLauncher(parent.getContext(),
                             getLaunchIntent(getApplicationContext(),
                                     application.getPath(dictionaryInfo.uncompressedFilename),
@@ -776,7 +784,7 @@ public class DictionaryActivity extends ActionBarActivity {
             }
         } else {
             // Down
-            destIndexEntry = Math.min(tokenRow.referenceIndex + 1, index.sortedIndexEntries.size());
+            destIndexEntry = Math.min(tokenRow.referenceIndex + 1, index.sortedIndexEntries.size() - 1);
         }
         final Index.IndexEntry dest = index.sortedIndexEntries.get(destIndexEntry);
         Log.d(LOG, "onUpDownButton, destIndexEntry=" + dest.token);
@@ -785,6 +793,14 @@ public class DictionaryActivity extends ActionBarActivity {
         defocusSearchText();
     }
 
+    void onRandomWordButton() {
+        int destIndexEntry = rand.nextInt(index.sortedIndexEntries.size());
+        final Index.IndexEntry dest = index.sortedIndexEntries.get(destIndexEntry);
+        setSearchText(dest.token, false);
+        jumpToRow(index.sortedIndexEntries.get(destIndexEntry).startRow);
+        defocusSearchText();
+    }
+
     // --------------------------------------------------------------------------
     // Options Menu
     // --------------------------------------------------------------------------
@@ -821,6 +837,15 @@ public class DictionaryActivity extends ActionBarActivity {
             });
         }
 
+        randomWordMenuItem = menu.add(getString(R.string.randomWord));
+        randomWordMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+            @Override
+            public boolean onMenuItemClick(MenuItem item) {
+                onRandomWordButton();
+                return true;
+            }
+        });
+
         application.onCreateGlobalOptionsMenu(this, menu);
 
         {
@@ -1007,7 +1032,7 @@ public class DictionaryActivity extends ActionBarActivity {
         getListView().requestFocus();
 
         // Visual indication that a new keystroke will clear the search text.
-        // Doesn't seem to work unless earchText has focus.
+        // Doesn't seem to work unless searchText has focus.
         // searchView.selectAll();
     }
 
@@ -1110,6 +1135,9 @@ public class DictionaryActivity extends ActionBarActivity {
         searchView.setIconifiedByDefault(text.length() > 0);
         searchView.setIconified(false);
 
+        // We don't want to show virtual keyboard when we're changing searchView text programatically:
+        hideKeyboard();
+
         if (triggerSearch) {
             onQueryTextListener.onQueryTextChange(text);
         }