]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryActivity.java
Fixed background of actionbar color in dark theme for 2.3.x Android
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
index 5d0c9c6c34e1ea30204b14491e84ec181cea1567..11606bc74f59d0e191114e09e5b17f72e2c72366 100644 (file)
@@ -37,6 +37,7 @@ import android.util.Log;
 import android.util.TypedValue;
 import android.view.ContextMenu;
 import android.view.ContextMenu.ContextMenuInfo;
+import android.view.Gravity;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
 import android.view.View;
@@ -50,7 +51,9 @@ import android.widget.AdapterView.AdapterContextMenuInfo;
 import android.widget.BaseAdapter;
 import android.widget.Button;
 import android.widget.FrameLayout;
+import android.widget.ImageButton;
 import android.widget.ImageView;
+import android.widget.ImageView.ScaleType;
 import android.widget.LinearLayout;
 import android.widget.ListAdapter;
 import android.widget.ListView;
@@ -143,7 +146,7 @@ public class DictionaryActivity extends SherlockListActivity {
     int fontSizeSp;
 
     SearchView searchView;
-    ImageView searchHintIcon;
+    ImageButton languageButton;
     SearchView.OnQueryTextListener onQueryTextListener;
 
     MenuItem nextWordMenuItem, previousWordMenuItem;
@@ -194,6 +197,11 @@ public class DictionaryActivity extends SherlockListActivity {
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
+        // This needs to be before super.onCreate, otherwise ActionbarSherlock
+        // doesn't makes the background of the actionbar white when you're
+        // in the dark theme.
+        setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
+
         Log.d(LOG, "onCreate:" + this);
         super.onCreate(savedInstanceState);
 
@@ -202,7 +210,6 @@ public class DictionaryActivity extends SherlockListActivity {
         // Don't auto-launch if this fails.
         prefs.edit().remove(C.DICT_FILE).commit();
 
-        setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
 
         application = (DictionaryApplication) getApplication();
         theme = application.getSelectedTheme();
@@ -410,8 +417,7 @@ public class DictionaryActivity extends SherlockListActivity {
         registerForContextMenu(getListView());
 
         // Cache some prefs.
-        wordList = new File(prefs.getString(getString(R.string.wordListFileKey),
-                new File(application.getDictDir(), "wordList.txt").getAbsolutePath()));
+        wordList = application.getWordListFile();
         saveOnlyFirstSubentry = prefs.getBoolean(getString(R.string.saveOnlyFirstSubentryKey),
                 false);
         clickOpensContextMenu = prefs.getBoolean(getString(R.string.clickOpensContextMenuKey),
@@ -435,11 +441,43 @@ public class DictionaryActivity extends SherlockListActivity {
 
         updateLangButton();
         searchView.requestFocus();
+
+        // http://stackoverflow.com/questions/2833057/background-listview-becomes-black-when-scrolling
+//        getListView().setCacheColorHint(0);
     }
 
     private void onCreateSetupActionBarAndSearchView() {
         ActionBar actionBar = getSupportActionBar();
         actionBar.setDisplayShowTitleEnabled(false);
+        actionBar.setDisplayShowHomeEnabled(false);
+        actionBar.setDisplayHomeAsUpEnabled(false);
+        
+        final LinearLayout customSearchView = new LinearLayout(getSupportActionBar().getThemedContext());
+        
+        final int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300,
+                getResources().getDisplayMetrics());
+        final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
+                width, ViewGroup.LayoutParams.WRAP_CONTENT);
+        customSearchView.setLayoutParams(layoutParams);
+
+        languageButton = new ImageButton(customSearchView.getContext());
+        languageButton.setMinimumWidth(application.languageButtonPixels);
+        languageButton.setMinimumHeight(application.languageButtonPixels * 2 / 3);
+        languageButton.setScaleType(ScaleType.FIT_CENTER);
+        languageButton.setOnClickListener(new OnClickListener() {
+            @Override
+            public void onClick(View arg0) {
+                onLanguageButtonClick();
+            }
+        });
+        languageButton.setOnLongClickListener(new OnLongClickListener() {
+            @Override
+            public boolean onLongClick(View v) {
+                onLanguageButtonLongClick(v.getContext());
+                return true;
+            }
+        });
+        customSearchView.addView(languageButton);
 
         searchView = new SearchView(getSupportActionBar().getThemedContext());
         searchView.setIconifiedByDefault(false);
@@ -447,10 +485,9 @@ public class DictionaryActivity extends SherlockListActivity {
         // wrong place.
         searchView.setQueryHint(getString(R.string.searchText));
         searchView.setSubmitButtonEnabled(false);
-        final int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300,
-                getResources().getDisplayMetrics());
-        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(width,
+        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,
                 FrameLayout.LayoutParams.WRAP_CONTENT);
+        lp.weight = 1;
         searchView.setLayoutParams(lp);
         searchView.setImeOptions(
                 EditorInfo.IME_ACTION_SEARCH |
@@ -476,22 +513,16 @@ public class DictionaryActivity extends SherlockListActivity {
         };
         searchView.setOnQueryTextListener(onQueryTextListener);
         searchView.setFocusable(true);
-
-        searchHintIcon = (ImageView) searchView.findViewById(R.id.abs__search_mag_icon);
-        searchHintIcon.setOnClickListener(new OnClickListener() {
-            @Override
-            public void onClick(View arg0) {
-                onLanguageButtonClick();
-            }
-        });
-        searchHintIcon.setOnLongClickListener(new OnLongClickListener() {
-            @Override
-            public boolean onLongClick(View v) {
-                onLanguageButtonLongClick(v.getContext());
-                return true;
-            }
-        });
-        actionBar.setCustomView(searchView);
+        customSearchView.addView(searchView);
+
+        // Clear the searchHint icon so that it takes as little space as possible.
+        ImageView searchHintIcon = (ImageView) searchView.findViewById(R.id.abs__search_mag_icon);
+        searchHintIcon.setBackgroundResource(android.R.color.transparent);
+        searchHintIcon.setLayoutParams(new LinearLayout.LayoutParams(1, 1));
+        searchHintIcon.setAdjustViewBounds(true);
+        searchHintIcon.setPadding(0, 0, 0, 0);
+        
+        actionBar.setCustomView(customSearchView);
         actionBar.setDisplayShowCustomEnabled(true);
     }
 
@@ -590,12 +621,12 @@ public class DictionaryActivity extends SherlockListActivity {
         final LanguageResources languageResources =
                 Language.isoCodeToResources.get(index.shortName);
         if (languageResources != null && languageResources.flagId != 0) {
-            searchHintIcon.setImageResource(languageResources.flagId);
+            languageButton.setImageResource(languageResources.flagId);
         } else {
             if (indexIndex % 2 == 0) {
-                searchHintIcon.setImageResource(android.R.drawable.ic_media_next);
+                languageButton.setImageResource(android.R.drawable.ic_media_next);
             } else {
-                searchHintIcon.setImageResource(android.R.drawable.ic_media_previous);
+                languageButton.setImageResource(android.R.drawable.ic_media_previous);
             }
         }
         updateTTSLanguage();
@@ -684,6 +715,7 @@ public class DictionaryActivity extends SherlockListActivity {
                 layoutParams.width = 0;
                 layoutParams.weight = 1.0f;
                 nameView.setLayoutParams(layoutParams);
+                nameView.setGravity(Gravity.CENTER_VERTICAL);
                 result.addView(nameView);
                 return result;
             }
@@ -891,6 +923,8 @@ public class DictionaryActivity extends SherlockListActivity {
                             return false;
                         }
                     });
+            // Rats, this won't be shown:
+            searchForSelection.setIcon(R.drawable.abs__ic_search);
         }
 
         if (row instanceof TokenRow && ttsReady) {
@@ -979,7 +1013,7 @@ public class DictionaryActivity extends SherlockListActivity {
             final PrintWriter out = new PrintWriter(new FileWriter(wordList, true));
             out.println(rawText.toString());
             out.close();
-        } catch (IOException e) {
+        } catch (Exception e) {
             Log.e(LOG, "Unable to append to " + wordList.getAbsolutePath(), e);
             Toast.makeText(this,
                     getString(R.string.failedAddingToWordList, wordList.getAbsolutePath()),
@@ -1352,7 +1386,10 @@ public class DictionaryActivity extends SherlockListActivity {
             result.setClickable(true);
             result.setFocusable(true);
             result.setLongClickable(true);
-            result.setBackgroundResource(android.R.drawable.menuitem_background);
+//            result.setBackgroundResource(android.R.drawable.menuitem_background);
+            
+            result.setBackgroundResource(theme.normalRowBg);
+
             result.setOnClickListener(new TextView.OnClickListener() {
                 @Override
                 public void onClick(View v) {