Fix ActionBar SearchView

This commit is contained in:
Martin Fietz 2015-05-12 19:02:39 +02:00
parent e98041c7ba
commit 0f293a4d78
12 changed files with 82 additions and 34 deletions

View File

@ -8,6 +8,7 @@ import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.Menu;
@ -197,8 +198,9 @@ public class AllEpisodesFragment extends Fragment {
if (itemsLoaded) {
inflater.inflate(R.menu.new_episodes, menu);
final SearchView sv = new SearchView(getActivity());
MenuItemUtils.addSearchItem(menu, sv);
MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView sv = (SearchView) MenuItemCompat.getActionView(searchItem);
MenuItemUtils.adjustTextColor(getActivity(), sv);
sv.setQueryHint(getString(R.string.search_hint));
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override

View File

@ -10,6 +10,7 @@ import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ListFragment;
import android.support.v4.util.Pair;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.util.Log;
@ -187,8 +188,9 @@ public class ItemlistFragment extends ListFragment {
if (itemsLoaded) {
FeedMenuHandler.onCreateOptionsMenu(inflater, menu);
final SearchView sv = new SearchView(getActivity());
MenuItemUtils.addSearchItem(menu, sv);
MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView sv = (SearchView) MenuItemCompat.getActionView(searchItem);
MenuItemUtils.adjustTextColor(getActivity(), sv);
sv.setQueryHint(getString(R.string.search_hint));
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override

View File

@ -8,6 +8,7 @@ import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.ContextMenu;
@ -66,8 +67,6 @@ public class QueueFragment extends Fragment {
private TextView txtvEmpty;
private ProgressBar progLoading;
private MenuItem queueLock;
private UndoBarController<FeedItemUndoToken> undoBarController;
private List<FeedItem> queue;
@ -206,8 +205,9 @@ public class QueueFragment extends Fragment {
if (itemsLoaded) {
inflater.inflate(R.menu.queue, menu);
final SearchView sv = new SearchView(getActivity());
MenuItemUtils.addSearchItem(menu, sv);
MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView sv = (SearchView) MenuItemCompat.getActionView(searchItem);
MenuItemUtils.adjustTextColor(getActivity(), sv);
sv.setQueryHint(getString(R.string.search_hint));
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
@ -223,7 +223,7 @@ public class QueueFragment extends Fragment {
}
});
MenuItemUtils.refreshLockItem(getActivity(), menu, queueLock);
MenuItemUtils.refreshLockItem(getActivity(), menu);
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(menu, R.id.refresh_item, updateRefreshMenuItemChecker);
}

View File

@ -31,8 +31,8 @@ public class GpodnetMainFragment extends Fragment {
private static final int NUM_PAGES = 2;
private static final int POS_TAGS = 0;
private static final int POS_TOPLIST = 1;
private static final int POS_TOPLIST = 0;
private static final int POS_TAGS = 1;
private static final int POS_SUGGESTIONS = 2;
Resources resources;

View File

@ -5,10 +5,13 @@ import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
@ -19,7 +22,6 @@ import android.widget.TextView;
import java.util.List;
import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.DefaultOnlineFeedViewActivity;
import de.danoeh.antennapod.activity.MainActivity;
@ -34,6 +36,7 @@ import de.danoeh.antennapod.menuhandler.MenuItemUtils;
* Displays a list of GPodnetPodcast-Objects in a GridView
*/
public abstract class PodcastListFragment extends Fragment {
private static final String TAG = "PodcastListFragment";
private GridView gridView;
@ -50,8 +53,10 @@ public abstract class PodcastListFragment extends Fragment {
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
final android.support.v7.widget.SearchView sv = new android.support.v7.widget.SearchView(getActivity());
MenuItemUtils.addSearchItem(menu, sv);
inflater.inflate(R.menu.gpodder_podcasts, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView sv = (SearchView) MenuItemCompat.getActionView(searchItem);
MenuItemUtils.adjustTextColor(getActivity(), sv);
sv.setQueryHint(getString(R.string.gpodnet_search_hint));
sv.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
@Override
@ -95,7 +100,7 @@ public abstract class PodcastListFragment extends Fragment {
}
protected void onPodcastSelected(GpodnetPodcast selection) {
if (BuildConfig.DEBUG) Log.d(TAG, "Selected podcast: " + selection.toString());
Log.d(TAG, "Selected podcast: " + selection.toString());
Intent intent = new Intent(getActivity(), DefaultOnlineFeedViewActivity.class);
intent.putExtra(OnlineFeedViewActivity.ARG_FEEDURL, selection.getUrl());
intent.putExtra(DefaultOnlineFeedViewActivity.ARG_TITLE, getString(R.string.gpodnet_main_label));

View File

@ -1,9 +1,11 @@
package de.danoeh.antennapod.fragment.gpodnet;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import org.apache.commons.lang3.Validate;
@ -44,8 +46,11 @@ public class SearchListFragment extends PodcastListFragment {
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
final SearchView sv = new SearchView(getActivity());
MenuItemUtils.addSearchItem(menu, sv);
super.onCreateOptionsMenu(menu, inflater);
// parent already inflated menu
MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView sv = (SearchView) MenuItemCompat.getActionView(searchItem);
MenuItemUtils.adjustTextColor(getActivity(), sv);
sv.setQueryHint(getString(R.string.gpodnet_search_hint));
sv.setQuery(query, false);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

View File

@ -5,9 +5,11 @@ import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
@ -23,6 +25,7 @@ import de.danoeh.antennapod.core.gpoddernet.model.GpodnetTag;
import de.danoeh.antennapod.menuhandler.MenuItemUtils;
public class TagListFragment extends ListFragment {
private static final String TAG = "TagListFragment";
private static final int COUNT = 50;
@ -35,8 +38,10 @@ public class TagListFragment extends ListFragment {
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
final SearchView sv = new SearchView(getActivity());
MenuItemUtils.addSearchItem(menu, sv);
inflater.inflate(R.menu.gpodder_podcasts, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView sv = (SearchView) MenuItemCompat.getActionView(searchItem);
MenuItemUtils.adjustTextColor(getActivity(), sv);
sv.setQueryHint(getString(R.string.gpodnet_search_hint));
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override

View File

@ -1,15 +1,15 @@
package de.danoeh.antennapod.menuhandler;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.UserPreferences;
/**
@ -17,25 +17,19 @@ import de.danoeh.antennapod.core.preferences.UserPreferences;
*/
public class MenuItemUtils extends de.danoeh.antennapod.core.menuhandler.MenuItemUtils {
public static MenuItem addSearchItem(Menu menu, SearchView searchView) {
MenuItem item = menu.add(Menu.NONE, R.id.search_item, Menu.NONE, R.string.search_label);
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
MenuItemCompat.setActionView(item, searchView);
public static void adjustTextColor(Context context, SearchView sv) {
if(Build.VERSION.SDK_INT < 14) {
SearchView.SearchAutoComplete textField = (SearchView.SearchAutoComplete) searchView.findViewById(de.danoeh.antennapod.R.id.search_src_text);
EditText searchEditText = (EditText) sv.findViewById(R.id.search_src_text);
if(UserPreferences.getTheme() == de.danoeh.antennapod.R.style.Theme_AntennaPod_Dark) {
textField.setTextColor(Resources.getSystem().getColor(android.R.color.white));
searchEditText.setTextColor(Color.WHITE);
} else {
textField.setTextColor(Resources.getSystem().getColor(android.R.color.black));
searchEditText.setTextColor(Color.BLACK);
}
}
return item;
}
public static void refreshLockItem(Context context, Menu menu, MenuItem queueLock) {
queueLock = menu.findItem(de.danoeh.antennapod.R.id.queue_lock);
public static void refreshLockItem(Context context, Menu menu) {
final MenuItem queueLock = menu.findItem(de.danoeh.antennapod.R.id.queue_lock);
int[] lockIcons = new int[] { de.danoeh.antennapod.R.attr.ic_lock_open, de.danoeh.antennapod.R.attr.ic_lock_closed };
TypedArray ta = context.obtainStyledAttributes(lockIcons);
if (UserPreferences.isQueueLocked()) {

View File

@ -22,6 +22,14 @@
android:title="@string/load_complete_feed"
custom:showAsAction="collapseActionView">
</item>
<item
android:id="@+id/action_search"
android:icon="?attr/action_search"
custom:showAsAction="collapseActionView|ifRoom"
custom:actionViewClass="android.support.v7.widget.SearchView"
android:title="@string/search_label"/>
<item
android:id="@+id/mark_all_read_item"
android:menuCategory="container"

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="?attr/action_search"
custom:showAsAction="collapseActionView|ifRoom"
custom:actionViewClass="android.support.v7.widget.SearchView"
android:title="@string/search_label"/>
</menu>

View File

@ -3,6 +3,13 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="?attr/action_search"
custom:showAsAction="collapseActionView|ifRoom"
custom:actionViewClass="android.support.v7.widget.SearchView"
android:title="@string/search_label"/>
<item
android:id="@+id/refresh_item"
android:title="@string/refresh_label"

View File

@ -16,6 +16,13 @@
custom:showAsAction="always"
android:icon="?attr/navigation_refresh"/>
<item
android:id="@+id/action_search"
android:icon="?attr/action_search"
custom:showAsAction="collapseActionView|ifRoom"
custom:actionViewClass="android.support.v7.widget.SearchView"
android:title="@string/search_label"/>
<item
android:id="@+id/clear_queue"
android:title="Clear Queue"