package com.loopeer.codereader.ui.activity; import android.app.SearchManager; import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.widget.SearchView; import android.text.TextUtils; import android.view.Menu; import android.view.MenuItem; import com.loopeer.codereader.R; public class SearchActivity extends BaseActivity implements SearchView.OnQueryTextListener { private RepositoryFragment mRepositoryFragment; private SearchView mSearchView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); mRepositoryFragment = (RepositoryFragment) getSupportFragmentManager() .findFragmentById(R.id.fragment_repository); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_file_search, menu); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); mSearchView.onActionViewExpanded(); mSearchView.setMaxWidth(Integer.MAX_VALUE); mSearchView.setOnQueryTextListener(this); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { this.finish(); return true; } return super.onOptionsItemSelected(item); } @Override public boolean onQueryTextSubmit(String query) { if (!TextUtils.isEmpty(query) && mRepositoryFragment != null) { mRepositoryFragment.setSearchText(query); mSearchView.clearFocus(); } return true; } @Override public boolean onQueryTextChange(String newText) { return false; } }