Skip to content

Commit

Permalink
searchview added to toolbar along with onQueryTextSubmit
Browse files Browse the repository at this point in the history
  • Loading branch information
rijogeorge7 committed May 12, 2018
1 parent 5f015a9 commit 6e0c394
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.esoxjem.movieguide.R;
import com.esoxjem.movieguide.Constants;
import com.esoxjem.movieguide.details.MovieDetailsActivity;
import com.esoxjem.movieguide.details.MovieDetailsFragment;
import com.esoxjem.movieguide.Movie;
import com.esoxjem.movieguide.util.SoftKeyboardUtils;

public class MoviesListingActivity extends AppCompatActivity implements MoviesListingFragment.Callback {
public static final String DETAILS_FRAGMENT = "DetailsFragment";
Expand Down Expand Up @@ -48,6 +52,20 @@ private void setToolbar() {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
SoftKeyboardUtils.hideSoftInput(searchView);
return true;
}

@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.esoxjem.movieguide.util;

import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

public class SoftKeyboardUtils {

/**
* Hides soft keyboard
*
* @param v - view instance
*/
public static void hideSoftInput(View v) {
if (v != null) {
((InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).
hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}

//restrict instanciation
private SoftKeyboardUtils() {
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
app:showAsAction="always"/>

<item
android:id="@+id/searchView"
android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always"
Expand Down

0 comments on commit 6e0c394

Please sign in to comment.