0

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

Charuක
  • 12,229
  • 5
  • 43
  • 83

3 Answers3

2

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.

xizzhu
  • 815
  • 5
  • 8
  • My bad because of asking the question wrong I guess. So I have everything set up to this point, the next step I have to take is to define Querys or something like that or a virtual table, but I feel like thats not what I am looking for if I want to search for TextViews. So basically the question is, if I should create a virtual table for my goal. – user7342339 Dec 28 '16 at 10:55
  • @user7342339 What do you mean by "search for TextViews"? search the text shown in `TextView` widget? If that's really what you want (instead of search in a model like e.g. a sqlite db), you can use `TextView.getText()` to get the content for each text view widget. – xizzhu Dec 28 '16 at 10:57
  • I have around 40 TextViews that all contain one word. What I wanna do is, to search for the words inside the TextViews, but I am not sure how to tell the SearchViews and everything that is linked to that, what I want to find. – user7342339 Dec 28 '16 at 11:03
  • 40 TextViews were stored in somewhere maybe in list or map or array you have to search there abt the keyword. – Maveňツ Dec 28 '16 at 11:04
  • So I have to set up an Array in order to make the textViews "searchable"? – user7342339 Dec 28 '16 at 11:07
1
 @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>
Deep Patel
  • 2,258
  • 2
  • 12
  • 26
  • It says, that it cannot resolve method getActivty(), am I making a rookie mistake? :D – user7342339 Dec 28 '16 at 11:06
  • getActivity() can be replaced by your context friend... try **getApplicationContext()** or pass **context** instead of getActivity() if your code is in activity then, remove **getActivity().** if you are using fragment than try solution mentioned as above. – Deep Patel Dec 28 '16 at 11:09
  • I see, and what is the goal of the last line? Because (menu, inflate) is also marked red, as onCreateOptionsMenu cannot be applied to androidViewMenuInflate. Thanks for the help btw! – user7342339 Dec 28 '16 at 11:13
  • are you using this code for activity or a fragment? can you tell me? so that i can help you accordingly – Deep Patel Dec 28 '16 at 11:14
  • I am using this code as a method inside the acitivity with the TextViews that should be searched through. So I guess it's for activity, isn't it? – user7342339 Dec 28 '16 at 11:18
  • naah! its for fragment, let me update the code for activity... check the answer now – Deep Patel Dec 28 '16 at 11:20
  • let me know if it works.. BTW i m sure it will work :) – Deep Patel Dec 28 '16 at 11:24
  • This looks pretty good, thank you! Two more questions though, what do I have to put in line 4 instead of menu_search? ^^ – user7342339 Dec 28 '16 at 11:29
  • Check updated answer now....create a menu file under the folder res > menu > menu_search.xml – Deep Patel Dec 28 '16 at 11:34
  • SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_search)); This line is giving me a NullPointerExeption now, I feel like I have to learn a lot :D – user7342339 Dec 28 '16 at 11:42
  • Please check the menu_search.xml file is correctly under , res > menu > menu_search.xml. you can check if its correctly placed or not by ctrl + click on that menu_search in line which it gives error. – Deep Patel Dec 28 '16 at 11:45
  • Yes it is, there is also a file called options_menu.xml which I created yesterday similar to the menu_search.xml file. Is it possible that this is the problem? Do you need some kind of data, like the error code or something? – user7342339 Dec 28 '16 at 11:50
  • if both are similar files having similar ids, you can change id for one of the file. Or you can remove one file and use same file for all occurrences – Deep Patel Dec 28 '16 at 11:52
  • Ok, that is not the problem. I will find a way to fix it later for sure, just another question real quick; in the public boolean onQueryTextSubmit method, what kind of operation do I put in there? Or do you know any keywords I can search for on the web? Thank you so much again! – user7342339 Dec 28 '16 at 11:59
  • in that operation part, if you have a list of content than by using loop you can compare the content with the content written in the searchview. **query** will return value in searchview and you can search from your existing bunch of data by that – Deep Patel Dec 28 '16 at 12:07
1

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)
   }
}
Suhayl SH
  • 1,183
  • 1
  • 10
  • 16
  • So I have to store my TextViews inside an ArrayList, right? – user7342339 Dec 28 '16 at 11:34
  • The "text" to be exact and not TextViews – Suhayl SH Dec 28 '16 at 11:35
  • Example: If you have a TextView with id textviewone and ArrayList textData. And you wanna display "My name is userxyz" in the TextView. You can add the text to the list like: textData.add("My name is userxyz"); and display it inside the textView as: TextView txt = (TextView) findViewById(R.id.textviewone); txt.setText(textData.get(0)); – Suhayl SH Dec 28 '16 at 11:39
  • I see, and then the search will find the text inside the ArrayList and then show the TextView that is bound to the the searched text? Thank you btw! – user7342339 Dec 28 '16 at 11:43
  • You are welcome :).. If my answer helped, do give a thumbs up! – Suhayl SH Dec 28 '16 at 11:44