I add ActionBarCompat to my project based on this tutorial. But I get confused on how to give event for user input when search button is clicked.
public class MainActivity extends ActionBarActivity {
ActionBar ab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ab = getSupportActionBar();
ab.setTitle("Test");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return super.onCreateOptionsMenu(menu);
}
}
And I found out that to get the input from user is with implements SearchView.OnQueryTextListener, as I found from here. Don't forget to add this listener to the SearchView at onCreateOptionsMenu(Menu menu) method, likt this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
mSearchView.setOnQueryTextListener(this);
return true;
}
Related
I have followed some steps on hiding and showing menu items. But unfortunately my app crashes. What could I be doing wrong?
This is my Activity:
private MenuItem menuItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alcohol_type_page);
invalidateOptionsMenu();
menuItem.setVisible(false);
}
//TOOLBAR SETTINGS
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_menu_icon, menu);
menuItem = menu.findItem(R.id.view_orders);
return super.onCreateOptionsMenu(menu);
}
Thanks in advance for any help! :D
Update
private MenuItem menuItem;
private boolean menuItemShow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alcohol_type_page);
}
//TOOLBAR SETTINGS
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_menu_icon, menu);
menuItem = menu.findItem(R.id.view_orders);
menuItem.setVisible(menuItemShow);
return super.onCreateOptionsMenu(menu);
}
onCreateOptionsMenu is invoked after onCreate,you shouldn't set menu item visibility in onCreate method.
I have a MainActivity on which Fragment is placed. Fragment having SearchView ( app:actionViewClass="android.support.v7.widget.SearchView"
) and MainActivity has a SearchView icon in Toolbar.
I want to hide/show the icon from Toolbar when Searchview of Fragment is Show/Hide (like Flipkart app). It is working properly when one of Hiding or Showing method is used, But when i use both of them then it gives error like
java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.Menu.findItem(int)' on a null object reference
My code snippets are..
MainActivity
#Override
public boolean onCreateOptionsMenu(final Menu menu)
{
_menu = menu;
final Menu m= menu;
getMenuInflater().inflate(R.menu.menus, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) DrawerActivity.this.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = null;
if (searchItem != null) {
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
searchView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
m.performIdentifierAction(searchItem.getItemId(), 0);
}
});
searchItem.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
public void show_search(){
final MenuItem searchItem = _menu.findItem(R.id.action_search);
searchItem.setVisible(true);
// invalidateOptionsMenu(); //if i use this it not show the icon.
}
public void hide_search() {
final MenuItem sitem = _menu.findItem(R.id.action_search); // gives NullPointerException in this line.
sitem.setVisible(false);
invalidateOptionsMenu();
}
Fragment code :
public class Fragment1 extends Fragment
{
scroll = (ScrollView) v.findViewById(R.id.scroll_view);
// Total Height of layout which contains searchview
ViewTreeObserver vas = lin.getViewTreeObserver();
vas.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
ViewGroup.MarginLayoutParams vlp = (ViewGroup.MarginLayoutParams) lin.getLayoutParams();
search_size = lin.getMeasuredHeight() + vlp.topMargin;
lin.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
scroll.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
float y = scroll.getScrollY();
DrawerActivity d = (DrawerActivity) getActivity();
if (y >= search_size)
{
d.show_search();
}
else
{
d.hide_search();
}
}
});
}
Good morning, why do not you inflates other menu.xml the fragment?
For example .. in your fragment insert
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
}
private Menu mMenu;
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_fragment, menu);
mMenu = menu;
MenuItem searchViewItem = menu.findItem(R.id.search);
final android.support.v7.widget.SearchView searchView =
(android.support.v7.widget.SearchView) MenuItemCompat.getActionView(searchViewItem);
super.onCreateOptionsMenu(menu, inflater);
}
I have helped
I've followed this code to implement SearchView inside ActionBar (as an item inside the menu).
But when i reach this line
mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
getActionView() return a null value.
I can't figure out what is wrong.
Try replacing
mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
with
mSearchMenuItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchMenuItem);
Try this..
private SearchView mSearchView;
....
....
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
setupSearchView(searchItem);
return super.onCreateOptionsMenu(menu);
}
private void setupSearchView(MenuItem searchItem) {
if (isAlwaysExpanded()) {
mSearchView.setIconifiedByDefault(false);
} else {
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}
mSearchView.setOnQueryTextListener(this);
}
public boolean onQueryTextChange(String newText) {
//Query
return false;
}
public boolean onQueryTextSubmit(String query) {
//query
return false;
}
public boolean onClose() {
return false;
}
protected boolean isAlwaysExpanded() {
return false;
}
And don't forget to implement the searchview...
implements SearchView.OnQueryTextListener
I have a SearchView in a ActionBar. Now I want to use it but I don't know how to use it. In the custom SearchView I use it like this:
SearchView sear = (SearchView) findViewById(R.id.searchView);
sear.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
#Override
public boolean onQueryTextSubmit (String query){
return false;
}
#Override
public boolean onQueryTextChange (String newText){
listDataAdapter.getFilter().filter(newText);
return true;
}
}
);
Now I have SearchView in an action bar which I show through code:
public class DataListActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_list_layout);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
android.support.v7.widget.SearchView searchView =
(android.support.v7.widget.SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(getApplicationContext(),MainMenu.class);
startActivity(intent);
return true;
case R.id.action_Exit:
openExit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openExit() {
}
How can I use SearchView ? I don't know how to use SearchView in action bar.
Set the onQueryTextListener(..) similar to what you have mentioned in your question on the searchView.
I am trying to implement an Action bar search on a listview which is nested inside a Fragment.
For now I am just trying to print the search text onto console, but its not working.
Following is my code:
main.xml:
<item
android:id="#+id/search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:title="search"/>
Class Def:
public class FragmentTab1 extends Fragment{
this is my onCreateOptionsMenu:
public boolean onCreateOptionsMenu(Menu menu) {
System.out.println("menu inflater");
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchView searchView = (SearchView)menu.findItem(R.id.search).getActionView();
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
System.out.println("Text"+newText);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
System.out.println("on query submit: "+query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
return true;
}
However when I type in the search box, nothing gets written onto the console, any hints what I am missing?
Try This Code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
The problem here is with the method Declaration, in a fragment we have to use:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
With this everything is working as should be, however I have to look for a way that my MainActivity menu gets invalidated as soon as the Fragment is up.