Android : How to show the cursor when Button is clicked - android

I have the search view at the top of my layout I want when I( clicked the button the cursor should show in the search bar.
here is the image enter image description here

Create an ID for the SearchView and use this code to create actions within it.
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.beneficiary_search, menu);
MenuItem search = menu.findItem(R.id.searchicon);
SearchView searchView = (SearchView) search.getActionView();
searchView.requestFocus()
searchView.setOnQueryTextListener(this);
return true;
}

Related

Make the SearchView do searches

I have a SearchView up and ready, here is the code I use in the java file of the page that has the SearchView:
#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(
new ComponentName(getApplicationContext(), SearchableActivity.class)));
return true;
}
My goal is now to let this SearchView search through a site full of TextViews. Every TextView contains one word. Is it possible to make the SearchView find the TextView that contains the word i just typed in? If so, how? If not, how can I make it happen in another way?
Kind regards,
Julian
The SearchView doesn't do any search at all, it's a simple widget for inputting the keywords, show recently used keywords, etc. You should implement your "search model" to find the content, and display it somewhere you want.
I guess what you are trying to do is search through text inside TextViews defined inside your layout.
I am assuming here that the text source is a String Array/ArrayList (TextViewData) in a file "Data.java.
You can give this a try:
1) Make the activity/fragment implement SearchView.OnQueryTextListener
2) Create an empty ArrayList to store search entry results.
private ArrayList<Data> mSearchItems
3) Add this line to the code where you initialize your SearchView
searchView.setOnQueryTextListener(this);
4) Override the following methods:
i) Method called on submit:
#Override
public boolean onQueryTextSubmit(String query) {
//Call a user defined mathod to handle the search.
handleSearch(query, Data.TextViewData)
}
ii) Method called on changing the text in the search box
#Override
public boolean onQueryTextChange(String query) {
handleSearch(query, Data.TextViewData)
}
5)
private void handleSearch(String Query, ArrayList<Data> queryList){
mSearchItems.clear();
for(Data data : queryList){
if(data.textviewone.toLowerCase().contains(Query.toLowerCase()) ||
data.textviewtwo.toLowerCase().contains(Query.toLowerCase()))
mSearchItems.add(data);
//You can then swap the recycler view (if being used) or hide other text views and display only the one being searched for (in case of a match)
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_search));
SearchManager searchManager = (SearchManager) getSystemService(Activity.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
// Setting up custom search button icon
int searchImgId = android.support.v7.appcompat.R.id.search_button; // I used the explicit layout ID of searchview's ImageView
ImageView v = (ImageView) searchView.findViewById(searchImgId);
v.setImageResource(R.drawable.ic_search_black);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// Do operation
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
menu_search.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_search"
style="#android:style/Widget.ActionButton"
android:icon="#drawable/ic_search_black"
android:title="#string/search"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="always" />
</menu>

Show dropdown when onQueryTextChange

I have added a searchView in my toolbar and I would like to show a dropdown as android does with the AutoCompleteTextView.
So basically I want to display a list of result and change it while the user is typing. I have added already a function to filter my results, but I have no clue how to show the dropdown in the list using my searchView.
Take a look the code below:
#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_subs, menu);
MenuItem searchItem = menu.findItem(R.id.action_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) {
//TODO: Implement dropdown
_list = _repository.filterItems(newText);
return false;
}
});
return true;
}
NOTE: something like in the image, I want to show my results(list of strings) in the dropdown.
any idea?
Thanks!
you should add this code in your onCreateOptionsMenu
SearchAutoComplete autoCompleteTextView = (SearchAutoComplete) searchView.findViewById(R.id.search_src_text);
if (autoCompleteTextView != null) {
autoCompleteTextView.setDropDownBackgroundResource(R.drawable.abc_search_dropdown_light);
}
For more information check this link
How to build Gmail like search box in the action bar?

Android SearchView: Show suggestions in a custom view, not listview

I am implementing a searchview for my app. Here is how i set my searchview:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
this.menu = menu;
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String query) {
loadHistory(query);
return true;
}
});
return true;
}
I want to show search suggestions to user when user starts typing. I can do this using a listview:
private void loadHistory(String query) {
// query db etc...
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView search = (SearchView) menu.findItem(R.id.search).getActionView();
search.setSuggestionsAdapter(new ExampleAdapter(this, cursor, items));
}
But suppose I do not want to show search suggestions as a listview, and show them in a custom view, where I can add some more stuff to my layout other then just search suggestions. For example, I want to show the following custom view instead of suggested searches listview:
How can I do that? There is a function for setting suggestions adapter, setSuggestionsAdapter(adapter), but I could not find a function like setCustomSuggestionsView(view).
Thanks.
I think what you need is a recycler view for your results. Please see this answer for more information. A recycler view gives you all the freedom layout wise. To use more viewtypes see this post. Hope this helps. I am not familliar enough with your case from your description.

No submit button for searchView Android app

I use searchview in my Android app and I would like to add a button that user presses to start the search. Based on what I read on the Internet, I can use setSubmitButtonEnabled to invoke a submit button instead of putting a button in the layout file. Here's my code:
public void setSubmitButtonEnabled (boolean enabled) {
}
I put my setSubmitButtonEnabled in the menu inflater as below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mylist, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
setSubmitButtonEnabled(true);
return true;
}
Apparently I am not doing it right because when I launch my app, I don't see any submit button on the screen. What's missing or what's wrong in my code? Is the submit button supposed to appear on the keypad or on the screen? Thank you.
You need to call
searchView.setSubmitButtonEnabled(true)
Why would you create your own version with no body and expect it to do anything?
For this use
searchView=findViewById(R.id.search_box);
searchView.setSubmitButtonEnabled(true);
For submit button icon use
app:goIcon="#drawable/ic_arrow_forward_black_24dp"
For SearchView default expanded
app:iconifiedByDefault="false"

How can I make my SearchView remember previous searches like how it is in the Play Market?

I want a drop down to display under the search view when clicked containing a list of previous searches
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.categories, menu);
// Get the SearchView and set the searchable configuration
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search)
.getActionView();
searchView.setQueryHint("Search All Categories");
final AppListAdapter adapterAllApps = new AppListAdapter(
CategoryListActivity.this, R.layout.app_row, catList);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
....
You might want to use a
AutoCompleteTextView
to do this and then add a searchable content provider.
have a look at this :
Turn AutoCompleteTextView into a SearchView in ActionBar instead

Categories

Resources