/* * This program is part of the OpenLMIS logistics management information * system platform software. * * Copyright © 2015 ThoughtWorks, Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. This program is distributed in the * hope that it will be useful, but WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Affero General Public License for more details. You should * have received a copy of the GNU Affero General Public License along with * this program. If not, see http://www.gnu.org/licenses. For additional * information contact info@OpenLMIS.org */ package org.openlmis.core.view.activity; import android.support.v7.widget.SearchView; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.widget.LinearLayout; import org.apache.commons.lang3.StringUtils; import org.openlmis.core.R; import org.openlmis.core.utils.DisplayUtil; public abstract class SearchBarActivity extends BaseActivity { protected SearchView searchView; @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_search_bar, menu); searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); searchView.setQueryHint(getResources().getString(R.string.search_hint)); searchView.setMaxWidth(DisplayUtil.getScreenWidth()); changeSearchButtonUI(); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { return onSearchStart(newText); } }); searchView.setOnCloseListener(new SearchView.OnCloseListener() { @Override public boolean onClose() { return true; } }); return super.onCreateOptionsMenu(menu); } private void changeSearchButtonUI() { final View searchButton = searchView.findViewById(R.id.search_button); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT ); params.width = (int) getResources().getDimension(R.dimen.search_button_width); params.setMargins(0, 0, (int) getResources().getDimension(R.dimen.search_button_right_margin), 0); searchButton.setLayoutParams(params); } protected void clearSearch() { if (searchView != null) { searchView.setQuery(StringUtils.EMPTY, true); } } @Override public void onBackPressed() { if (isSearchViewActivity()) { searchView.onActionViewCollapsed(); } else { super.onBackPressed(); } } protected boolean isSearchViewActivity() { return !searchView.isIconified(); } public abstract boolean onSearchStart(String query); }