Use SearchView directly in layout instead of option menu

This commit is contained in:
Stefan Niedermann 2020-06-05 10:05:09 +02:00
parent 66e158d438
commit 061094a13e
3 changed files with 86 additions and 103 deletions

View file

@ -8,9 +8,8 @@ import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewTreeObserver; import android.view.ViewTreeObserver;
import android.widget.LinearLayout; import android.widget.LinearLayout;
@ -74,6 +73,8 @@ import it.niedermann.owncloud.notes.persistence.NoteServerSyncHelper.ViewProvide
import it.niedermann.owncloud.notes.persistence.NotesDatabase; import it.niedermann.owncloud.notes.persistence.NotesDatabase;
import it.niedermann.owncloud.notes.util.NoteUtil; import it.niedermann.owncloud.notes.util.NoteUtil;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static it.niedermann.owncloud.notes.util.ColorUtil.contrastRatioIsSufficient; import static it.niedermann.owncloud.notes.util.ColorUtil.contrastRatioIsSufficient;
import static it.niedermann.owncloud.notes.util.SSOUtil.askForNewAccount; import static it.niedermann.owncloud.notes.util.SSOUtil.askForNewAccount;
@ -123,7 +124,6 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
private Category navigationSelection = new Category(null, null); private Category navigationSelection = new Category(null, null);
private String navigationOpen = ""; private String navigationOpen = "";
private ActionMode mActionMode; private ActionMode mActionMode;
private SearchView searchView = null;
private final ISyncCallback syncCallBack = () -> { private final ISyncCallback syncCallBack = () -> {
adapter.clearSelection(listView); adapter.clearSelection(listView);
if (mActionMode != null) { if (mActionMode != null) {
@ -164,7 +164,7 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
db = NotesDatabase.getInstance(this); db = NotesDatabase.getInstance(this);
setupActionBar(); setupToolbars();
setupNavigationList(categoryAdapterSelectedItem); setupNavigationList(categoryAdapterSelectedItem);
setupNavigationMenu(); setupNavigationMenu();
setupNotesList(); setupNotesList();
@ -241,19 +241,58 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
notAuthorizedAccountHandled = true; notAuthorizedAccountHandled = true;
} }
private void setupActionBar() { private void setupToolbars() {
Toolbar toolbar = binding.activityNotesListView.toolbar; Toolbar toolbar = binding.activityNotesListView.toolbar;
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
activityBinding.homeToolbar.setOnClickListener((v) -> { activityBinding.homeToolbar.setOnClickListener((v) -> {
if (searchView == null || searchView.isIconified()) { if (activityBinding.toolbar.getVisibility() == GONE) {
activityBinding.homeToolbar.setVisibility(View.GONE); updateToolbars(false);
activityBinding.toolbar.setVisibility(View.VISIBLE);
searchView.setIconified(false);
ViewCompat.setElevation(activityBinding.appBar, getResources().getDimension(R.dimen.design_appbar_elevation));
} }
}); });
activityBinding.menuButton.setOnClickListener((v) -> binding.drawerLayout.openDrawer(GravityCompat.START)); activityBinding.menuButton.setOnClickListener((v) -> binding.drawerLayout.openDrawer(GravityCompat.START));
final LinearLayout searchEditFrame = activityBinding.searchView.findViewById(R.id
.search_edit_frame);
searchEditFrame.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
int oldVisibility = -1;
@Override
public void onGlobalLayout() {
int currentVisibility = searchEditFrame.getVisibility();
if (currentVisibility != oldVisibility) {
if (currentVisibility == VISIBLE) {
fabCreate.hide();
} else {
new Handler().postDelayed(() -> fabCreate.show(), 150);
}
oldVisibility = currentVisibility;
}
}
});
activityBinding.searchView.setOnCloseListener(() -> {
if (activityBinding.toolbar.getVisibility() == VISIBLE && TextUtils.isEmpty(activityBinding.searchView.getQuery())) {
updateToolbars(true);
return true;
}
return false;
});
activityBinding.searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
refreshLists();
return true;
}
});
} }
private void setupNotesList() { private void setupNotesList() {
@ -311,8 +350,8 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
fabCreate.setOnClickListener((View view) -> { fabCreate.setOnClickListener((View view) -> {
Intent createIntent = new Intent(getApplicationContext(), EditNoteActivity.class); Intent createIntent = new Intent(getApplicationContext(), EditNoteActivity.class);
createIntent.putExtra(EditNoteActivity.PARAM_CATEGORY, navigationSelection); createIntent.putExtra(EditNoteActivity.PARAM_CATEGORY, navigationSelection);
if (searchView != null && !searchView.isIconified() && searchView.getQuery().length() > 0) { if (activityBinding.searchView.getQuery().length() > 0) {
createIntent.putExtra(EditNoteActivity.PARAM_CONTENT, searchView.getQuery().toString()); createIntent.putExtra(EditNoteActivity.PARAM_CONTENT, activityBinding.searchView.getQuery().toString());
invalidateOptionsMenu(); invalidateOptionsMenu();
} }
startActivityForResult(createIntent, create_note_cmd); startActivityForResult(createIntent, create_note_cmd);
@ -399,11 +438,8 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
@Override @Override
public boolean onSupportNavigateUp() { public boolean onSupportNavigateUp() {
if (searchView != null && !searchView.isIconified()) { if (activityBinding.toolbar.getVisibility() == VISIBLE) {
activityBinding.homeToolbar.setVisibility(View.VISIBLE); updateToolbars(true);
activityBinding.toolbar.setVisibility(View.GONE);
searchView.setIconified(true);
ViewCompat.setElevation(activityBinding.appBar, 0);
return true; return true;
} else { } else {
return super.onSupportNavigateUp(); return super.onSupportNavigateUp();
@ -548,8 +584,8 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
return; return;
} }
View emptyContentView = binding.activityNotesListView.emptyContentView.getRoot(); View emptyContentView = binding.activityNotesListView.emptyContentView.getRoot();
emptyContentView.setVisibility(View.GONE); emptyContentView.setVisibility(GONE);
binding.activityNotesListView.progressCircular.setVisibility(View.VISIBLE); binding.activityNotesListView.progressCircular.setVisibility(VISIBLE);
fabCreate.show(); fabCreate.show();
String subtitle; String subtitle;
if (navigationSelection.category != null) { if (navigationSelection.category != null) {
@ -565,19 +601,19 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
} }
activityBinding.searchText.setText(subtitle); activityBinding.searchText.setText(subtitle);
CharSequence query = null; CharSequence query = null;
if (searchView != null && !searchView.isIconified() && searchView.getQuery().length() != 0) { if (activityBinding.searchView.getQuery().length() != 0) {
query = searchView.getQuery(); query = activityBinding.searchView.getQuery();
} }
NotesLoadedListener callback = (List<Item> notes, boolean showCategory, CharSequence searchQuery) -> { NotesLoadedListener callback = (List<Item> notes, boolean showCategory, CharSequence searchQuery) -> {
adapter.setShowCategory(showCategory); adapter.setShowCategory(showCategory);
adapter.setHighlightSearchQuery(searchQuery); adapter.setHighlightSearchQuery(searchQuery);
adapter.setItemList(notes); adapter.setItemList(notes);
binding.activityNotesListView.progressCircular.setVisibility(View.GONE); binding.activityNotesListView.progressCircular.setVisibility(GONE);
if (notes.size() > 0) { if (notes.size() > 0) {
emptyContentView.setVisibility(View.GONE); emptyContentView.setVisibility(GONE);
} else { } else {
emptyContentView.setVisibility(View.VISIBLE); emptyContentView.setVisibility(VISIBLE);
} }
if (scrollToTop) { if (scrollToTop) {
listView.scrollToPosition(0); listView.scrollToPosition(0);
@ -587,62 +623,10 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
new LoadCategoryListTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); new LoadCategoryListTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
/**
* Adds the Menu Items to the Action Bar.
*
* @param menu Menu
* @return boolean
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_list_view, menu);
// Associate searchable configuration with the SearchView
final MenuItem item = menu.findItem(R.id.search);
searchView = (SearchView) item.getActionView();
final LinearLayout searchEditFrame = searchView.findViewById(R.id
.search_edit_frame);
searchEditFrame.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
int oldVisibility = -1;
@Override
public void onGlobalLayout() {
int currentVisibility = searchEditFrame.getVisibility();
if (currentVisibility != oldVisibility) {
if (currentVisibility == View.VISIBLE) {
fabCreate.hide();
} else {
new Handler().postDelayed(() -> fabCreate.show(), 150);
}
oldVisibility = currentVisibility;
}
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
refreshLists();
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
@Override @Override
protected void onNewIntent(Intent intent) { protected void onNewIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) { if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
searchView.setQuery(intent.getStringExtra(SearchManager.QUERY), true); activityBinding.searchView.setQuery(intent.getStringExtra(SearchManager.QUERY), true);
} }
super.onNewIntent(intent); super.onNewIntent(intent);
} }
@ -700,7 +684,7 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
Log.i(TAG, capabilities.toString()); Log.i(TAG, capabilities.toString());
runOnUiThread(() -> { runOnUiThread(() -> {
selectAccount(ssoAccount.name); selectAccount(ssoAccount.name);
binding.accountNavigation.setVisibility(View.VISIBLE); binding.accountNavigation.setVisibility(VISIBLE);
binding.drawerLayout.closeDrawer(GravityCompat.START); binding.drawerLayout.closeDrawer(GravityCompat.START);
}); });
} catch (SQLiteConstraintException e) { } catch (SQLiteConstraintException e) {
@ -708,16 +692,16 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
runOnUiThread(() -> { runOnUiThread(() -> {
BrandedSnackbar.make(coordinatorLayout, R.string.account_already_imported, Snackbar.LENGTH_LONG).show(); BrandedSnackbar.make(coordinatorLayout, R.string.account_already_imported, Snackbar.LENGTH_LONG).show();
selectAccount(ssoAccount.name); selectAccount(ssoAccount.name);
binding.accountNavigation.setVisibility(View.VISIBLE); binding.accountNavigation.setVisibility(VISIBLE);
binding.drawerLayout.closeDrawer(GravityCompat.START); binding.drawerLayout.closeDrawer(GravityCompat.START);
}); });
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
runOnUiThread(() -> { runOnUiThread(() -> {
binding.accountNavigation.setVisibility(View.GONE); binding.accountNavigation.setVisibility(GONE);
binding.drawerLayout.openDrawer(GravityCompat.START); binding.drawerLayout.openDrawer(GravityCompat.START);
binding.activityNotesListView.progressCircular.setVisibility(View.GONE); binding.activityNotesListView.progressCircular.setVisibility(GONE);
ExceptionDialogFragment.newInstance(e).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()); ExceptionDialogFragment.newInstance(e).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
}); });
} }
@ -792,7 +776,7 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
if (selected) { if (selected) {
v.setSelected(true); v.setSelected(true);
mActionMode = startSupportActionMode(new MultiSelectedActionModeCallback( mActionMode = startSupportActionMode(new MultiSelectedActionModeCallback(
this, this, db, mActionMode, adapter, listView, this::refreshLists, getSupportFragmentManager(), searchView this, this, db, mActionMode, adapter, listView, this::refreshLists, getSupportFragmentManager(), activityBinding.searchView
)); ));
int checkedItemCount = adapter.getSelected().size(); int checkedItemCount = adapter.getSelected().size();
mActionMode.setTitle(getResources().getQuantityString(R.plurals.ab_selected, checkedItemCount, checkedItemCount)); mActionMode.setTitle(getResources().getQuantityString(R.plurals.ab_selected, checkedItemCount, checkedItemCount));
@ -802,14 +786,20 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
@Override @Override
public void onBackPressed() { public void onBackPressed() {
if (searchView == null || searchView.isIconified()) { if (activityBinding.toolbar.getVisibility() == VISIBLE) {
super.onBackPressed(); updateToolbars(true);
} else { } else {
activityBinding.homeToolbar.setVisibility(View.VISIBLE); super.onBackPressed();
activityBinding.toolbar.setVisibility(View.GONE); }
searchView.setIconified(true); }
ViewCompat.setElevation(activityBinding.appBar, 0);
invalidateOptionsMenu(); private void updateToolbars(boolean disableSearch) {
activityBinding.homeToolbar.setVisibility(disableSearch ? VISIBLE : GONE);
activityBinding.toolbar.setVisibility(disableSearch ? GONE : VISIBLE);
activityBinding.searchView.setIconified(disableSearch);
ViewCompat.setElevation(activityBinding.appBar, disableSearch ? 0 : getResources().getDimension(R.dimen.design_appbar_elevation));
if (disableSearch) {
activityBinding.searchView.setQuery(null, true);
} }
} }
@ -873,7 +863,6 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
} }
mActionMode.finish(); mActionMode.finish();
searchView.setIconified(true);
refreshLists(); refreshLists();
} }
} }

View file

@ -26,7 +26,13 @@
app:contentInsetStartWithNavigation="0dp" app:contentInsetStartWithNavigation="0dp"
app:navigationIcon="@drawable/ic_arrow_back_grey600_24dp" app:navigationIcon="@drawable/ic_arrow_back_grey600_24dp"
app:titleMarginStart="0dp" app:titleMarginStart="0dp"
tools:title="@string/app_name" /> tools:title="@string/app_name">
<androidx.appcompat.widget.SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.card.MaterialCardView <com.google.android.material.card.MaterialCardView
android:id="@+id/home_toolbar" android:id="@+id/home_toolbar"

View file

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.owncloudnotes.NotesListViewActivity">
<item
android:id="@+id/search"
android:title="@string/action_search"
android:icon="@drawable/ic_search_grey600_24dp"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="collapseActionView|always" />
</menu>