i want to just add searchview in my app.but i dont want to search any thing just i want the query entered by the user.
so far i tried this code but when i run my app it crashes.
Update:
I tried this one but eventhough my app crashes.
main_menu.xml code
<?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"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
app:actionViewClass="android.support.v7.widget.SerachView"
app:showAsAction="always"
/>
MainActivity.java
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.main_menu,menu);
SearchView searchView=(SearchView)findViewById(R.id.search);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// Toast.makeText(this,query,Toast.LENGTH_LONG).show();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
Can Anyone help me to solve this problem please.
ThankYou.
Lets not use SearchView directly, in your menu.xml
<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=".HomeActivity">
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
</menu>
and add this simple code to your java :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
// Not implemented here
return false;
default:
break;
}
searchView.setOnQueryTextListener(queryTextListener);
return super.onOptionsItemSelected(item);
}
Main_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/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"
android:icon="#android:drawable/ic_menu_search"
android:title="Search" />
</menu>
onCreateOptionMenu method
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_view_menu_item, menu);
MenuItem searchViewItem = menu.findItem(R.id.action_search);
final SearchView searchViewAndroidActionBar = (SearchView) MenuItemCompat.getActionView(searchViewItem);
searchViewAndroidActionBar.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
searchViewAndroidActionBar.clearFocus();
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
Related
I am trying to get a 3 dot menu to show in my app but can't get it to work.
I have created a file called mymenu.xml under res/menu and the contents are:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/level"
android:title="#string/Level"
app:showAsAction="never">
</item>
</menu>
This is what is in my MainActivity.java file:
#Override
public boolean onCreateOptionsMenu(Menu my_menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, my_menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.level:
levelMax = 41;
levelTextView.setText("Level:\nEasy");
mCountDownTimer.cancel();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
What am I doing wrong? Any help would be appreciated. Thanks.
Change app:showAsAction="never" to app:showAsAction="always"
I am currently working on an app that i implemented a toolbar and a search functionality on the toolbar with a searchview to filter list views which is working fine.
I also have a navigation drawer whereby i also want one of its items to do exactly what the search functionality is doing on the toolbar. How do implement an intent to lead me to SearchViewon toolbar when searchitem is clicked from navigation drawer.
menu_main for toolbar
<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/viewMode"
android:orderInCategory="100"
app:showAsAction="never"
android:icon="#drawable/ic_view_compact_black_24dp"
android:title="#string/view" />
<item
// my search functionality
android:id="#+id/search"
android:orderInCategory="100"
app:actionViewClass= "android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_search"
android:title="#string/search" />
</menu>
activity_navdrawer
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_notes"
android:icon="#drawable/ic_note_add_black_24dp"
android:title="#string/first_layout" />
<item
// search functionality which should lead me to searchview on toolbar
android:id="#+id/nav_search"
android:icon="#drawable/ic_search_black_24dp"
android:title="#string/third_layout" />
</group>
<item android:title="#string/app_communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_share_black_24dp"
android:title="#string/app_share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_email_black_24dp"
android:title="#string/app_send" />
</menu>
</item>
</menu>
from main activity
public boolean onNavigationItemSelected(MenuItem item) {
// Handling navigation view item clicks here.
int id = item.getItemId();
if(id == R.id.nav_notes) {
Intent noteIntent = new Intent(this, MainActivity.class);
startActivity(noteIntent);
} else if(id == R.id.nav_search) {
MenuItem searchMenuItem = mOptionsMenu.findItem(R.id.search);
searchMenuItem.expandActionView();
// what should i implement here?
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public boolean onCreateOptionsMenu(`final` Menu menu) {
mOptionsMenu = menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
ArrayAdapter<Note> arrayAdapter = (ArrayAdapter<Note>) mListNotes.getAdapter();
arrayAdapter.getFilter().filter(newText);
return false;
}
});
return true;
}
Try the following code below:
private Menu mOptionsMenu;
...
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
mOptionsMenu = menu;
...
}
...
public boolean onNavigationItemSelected(MenuItem item) {
...
} else if(id == R.id.nav_search) {
MenuItem searchMenuItem = mOptionsMenu.findItem(R.id.menu_search);
searchMenuItem.expandActionView();
}
...
Add <category android:name="android.intent.category.DEFAULT" /> to your search activity in manifest. Then do this:
else if(id == R.id.nav_search) {
startActivity(new Intent(Intent.ACTION_SEARCH));
}
If this works, courtesy - Start Activity Using Custom Action
I want to load an activity and have the search bar visible right away with a magnifying glass available to click/search based on the term entered. Here is what I want to see when I load the activity:
Code:
public class FlickRActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flick_r);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Toast.makeText(getApplicationContext(),newText,Toast.LENGTH_SHORT).show();
return false;
}
});
return true;
}
}
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"
android:title="#string/hint_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Just call searchItem.expandActionView();
I am trying to implement a SearchView in OptionsMenu of a Fragment. The onQueryTextChange and onQueryTextSubmit is not working. I am not sure where am I going wrong.
The menu file:
<?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/profile"
android:orderInCategory="10"
android:title="Profile"
app:showAsAction="never">
</item>
<item
android:id="#+id/settings"
android:orderInCategory="11"
android:title="Settings"
app:showAsAction="never">
</item>
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
The part in fragment where I am trying to access the text from SearchView:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.browse_story_fragment, menu);
MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) myActionMenuItem.getActionView();
SearchView.OnQueryTextListener listener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String query) {
query = query.toLowerCase();
Log.e("queryText",query);
return false;
}
public boolean onQueryTextSubmit(String query) {
Log.e("queryTextSubmit", query);
return false;
}
};
super.onCreateOptionsMenu(menu, inflater);
}
I do not see anything in the Logs. Where am I going wrong?
My big mistake! What I was thinking!
Here is the code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.browse_story_fragment, menu);
MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) myActionMenuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// collapse the view ?
//menu.findItem(R.id.menu_search).collapseActionView();
Log.e("queryText",query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// search goes here !!
// listAdapter.getFilter().filter(query);
Log.e("queryText",newText);
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
I have a search view in my activity. When I am trying to attach an OnQueryTextListener, it gives me a NullPointerException. Its Java code is as follows
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.allpurpose, menu);
MenuItem searchItem = menu.findItem(R.id.apmenu_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) {return true;}
public boolean onQueryTextSubmit(String query) {return true;}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
XML Code:
<?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/apmenu_search"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/srch"
android:showAsAction="ifRoom"
android:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
All "Compat" items are used to provide backward compatibility. So in this case MenuItemCompat supports action bar items for versions above 7 (instead of the normal 11). In the java code, if there is MenuItemCompat, then in the xml too there must be a "Compat" item. Now, the "app" namespace supports these "Compat" items. So change the xml like this:
<?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/apmenu_search"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/srch"
app:showAsAction="ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>