]> 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 37a45fab9d31210a29516c1acb6e8bd3c5b65704..11606bc74f59d0e191114e09e5b17f72e2c72366 100644 (file)
@@ -22,7 +22,6 @@ import android.content.Intent;
 import android.content.SharedPreferences;
 import android.graphics.Color;
 import android.graphics.Typeface;
-import android.graphics.drawable.ColorDrawable;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Handler;
@@ -45,7 +44,6 @@ import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.View.OnLongClickListener;
 import android.view.ViewGroup;
-import android.view.ViewGroup.LayoutParams;
 import android.view.WindowManager;
 import android.view.inputmethod.EditorInfo;
 import android.view.inputmethod.InputMethodManager;
@@ -53,6 +51,7 @@ 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;
@@ -147,7 +146,7 @@ public class DictionaryActivity extends SherlockListActivity {
     int fontSizeSp;
 
     SearchView searchView;
-    ImageView searchHintIcon;
+    ImageButton languageButton;
     SearchView.OnQueryTextListener onQueryTextListener;
 
     MenuItem nextWordMenuItem, previousWordMenuItem;
@@ -198,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);
 
@@ -206,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();
@@ -414,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),
@@ -441,7 +443,7 @@ public class DictionaryActivity extends SherlockListActivity {
         searchView.requestFocus();
 
         // http://stackoverflow.com/questions/2833057/background-listview-becomes-black-when-scrolling
-        getListView().setCacheColorHint(0);
+//        getListView().setCacheColorHint(0);
     }
 
     private void onCreateSetupActionBarAndSearchView() {
@@ -449,6 +451,33 @@ public class DictionaryActivity extends SherlockListActivity {
         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);
@@ -456,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 |
@@ -485,28 +513,16 @@ public class DictionaryActivity extends SherlockListActivity {
         };
         searchView.setOnQueryTextListener(onQueryTextListener);
         searchView.setFocusable(true);
+        customSearchView.addView(searchView);
 
-        searchHintIcon = (ImageView) searchView.findViewById(R.id.abs__search_mag_icon);
-        // http://stackoverflow.com/questions/2521959/how-to-scale-an-image-in-imageview-to-keep-the-aspect-ratio
-        searchHintIcon.setLayoutParams(new LinearLayout.LayoutParams(
-                application.languageButtonPixels * 3 / 4, LayoutParams.WRAP_CONTENT));
-        searchHintIcon.setScaleType(ScaleType.FIT_CENTER);
+        // 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(1, application.languageButtonPixels / 8, 1, 0);
-        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);
+        searchHintIcon.setPadding(0, 0, 0, 0);
+        
+        actionBar.setCustomView(customSearchView);
         actionBar.setDisplayShowCustomEnabled(true);
     }
 
@@ -605,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();
@@ -907,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) {
@@ -995,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()),