I have used material search view from github.(https://github.com/MiguelCatalan/MaterialSearchView).I followed the code in it.But when I try to run the app the default search view is shown instead of the search icon in the drawable.Below is the code.Screenshot is also included.
materialsearchview(xml):
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
menu_main.xml:
<item
android:id="#+id/action_search"
android:icon="#drawable/search"
android:title="Search"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="always"/>
Java code:
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
MenuItem menuItem=menu.findItem(R.id.action_search);
materialSearchView.setMenuItem(menuItem);
materialSearchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
Screenshot:
Related
I have a menu.xml and the code to show the menu in action bar. Do i have to add the code in every activity? Cause after this i use a switch case to get click, and more code, so i dont thing its the wright way to copy-paste the same code in every activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
Is there a better way to do it as to work in all app (all activities)?
Create a BaseActivity class.
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
}
Now all activities that extend your base activity will have the same menu.
this is a guide for Android Menu
this is an example menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game" />
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
And then inflating it within your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
whe item clicked:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
good luck and read the doc official for android
I want to add a searchview and checkbox into action bar menu. And this checkbox will be visible if searchview is opened. And in it's opposite case it will be hidden. How I can do this?
I do something below . But it doesnt work correctly. I want hide checkbox (In my notes) when searchview is closed.
menu.xml
<?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">
<item
android:id="#+id/search_button"
android:icon="#drawable/ic_icon_search"
android:title="Arama"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView">
</item>
<item
android:id="#+id/search_in_my_notes_checkbox"
app:showAsAction="ifRoom"
android:title="#string/search_in_my_notes"
android:checkable="true"
android:visible="false"
/>
</menu>
HomeActivity.java
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.search_button){
MenuItem searchInMyNotesCheckbox = (MenuItem)menu.findItem(R.id.search_in_my_notes_checkbox);
searchInMyNotesCheckbox.setVisible(true);
}
return super.onOptionsItemSelected(item);
}
I found an alternative solution. I will use two checkbox image with checked and unchecked. And I will override onCreateOptionsMenu like this :
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem search = (MenuItem)menu.findItem(R.id.search_button2);
final MenuItem searchInMyNotesCheckbox = (MenuItem) menu.findItem(R.id.search_in_my_notes_checkbox);
MenuItemCompat.setOnActionExpandListener(search,
new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
// Return true to allow the action view to expand
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
MenuItem searchInMyNotesCheckbox = (MenuItem) menu.findItem(R.id.search_in_my_notes_checkbox);
searchInMyNotesCheckbox.setVisible(false);
return true;
}
});
searchInMyNotesCheckbox.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getIcon() == R.drawable.checked_checkbox){
item.setIcon(R.drawable.unchecked_checkbox);
}
else {
item.setIcon(R.drawable.checked_checkbox);
}
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
I used SherlockActionBar for my ActionBar, but I want to use AppCompact for my search view. To change my code from sherlock to AppCompact, I wrote this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
//searchView.setOnQueryTextListener(this);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
});
return true;
// return super.onCreateOptionsMenu(menu);
}
xml menu:
<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=".MainActivity">
<item
android:id="#+id/action_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#android:drawable/ic_menu_search"
android:title="action_search"
app:showAsAction="always|collapseActionView" />
</menu>
but it crashed. How can I fix it?
EDIT:
change android:actionViewClass to app:actionViewClass
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.
I have a menu which is inflated from main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/act_sync"
android:showAsAction="always"
android:actionLayout="#layout/sync_action"
android:icon="#android:drawable/ic_popup_sync" />
</menu>
and here is the code in the activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
MyMessageHandler.debug("menu item selected");
switch(item.getItemId()){
case R.id.act_sync:
sync();
return true;
}
return super.onOptionsItemSelected(item);
}
But onOptionsItemSelected is not called when I touch the menu item. When I remove the actionLayout attribute of the menu item, it works fine. How can I fix this? Thanks.
you should use below snippet ( Just for reference )
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
final Menu m = menu;
final MenuItem item = menu.findItem(R.id.ActionConnection);
item.getActionView().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
sync();
}
});
return true;
}
In addition to the answer provided by Vipul Shah.
Make sure you disable the clickable option of all items in your action layout:
android:clickable="false"
Otherwise, it may steal the click so that you still cant receive onClick call back.