Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add buttons to search view in android toolbar?

Tags:

android

Now I have next code:

@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_toolbar_reader, menu);

    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) searchItem.getActionView();

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    if(null!=searchManager ) {
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    }
    searchView.setIconifiedByDefault(false);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });
    return true;
}

I want to attach to search view buttons search next and search previous (search in WebView). e. g. I want to get this (drawed arrow should appear only when searchView uncollapsed):example

How can I do that?

like image 423
Vassily Avatar asked Sep 01 '15 17:09

Vassily


People also ask

How do I add a search icon to my Android toolbar?

Add the Search View to the App Bar To add a SearchView widget to the app bar, create a file named res/menu/options_menu. xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item.

How do I customize my SearchView widget?

just do your own search view, it is very simple. you then can include this layout in your activity layout file. This is a simple layout which includes a "search icon" followed by EditText, followed by "clear icon". The clear icon is shown after user types some text.

How do I set text in search view?

You can use setQuery() to change the text in the textbox. However, setQuery() method triggers the focus state of a search view, so a keyboard will show on the screen after this method has been invoked.


2 Answers

Another solution:

  public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.artykul_search_menu, menu);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        MenuItem searchItem = menu.findItem(R.id.artykul_search_menu);


        nextItem = menu.add(0, 1,1,"next");
        nextItem.setIcon(R.drawable.baseline_expand_more_black_24);
        nextItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
        nextItem.setVisible(false);

        prevItem = menu.add(0, 2,2,"prev");
        prevItem.setIcon(R.drawable.baseline_expand_less_black_24);
        prevItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
        prevItem.setVisible(false);


        searchView = (SearchView) searchItem.getActionView();
        searchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                nextItem.setVisible(true);
                prevItem.setVisible(true);
            }
        });

        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                nextItem.setVisible(false);
                prevItem.setVisible(false);
                return false;
            }
        });
like image 148
mik.ro Avatar answered Sep 28 '22 02:09

mik.ro


So, I solved it via this code in my 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_toolbar_reader, menu);


    final MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) searchItem.getActionView();

    LayoutParams navButtonsParams = new LayoutParams(toolbar.getHeight() * 2 / 3, toolbar.getHeight() * 2 / 3);

    Button btnNext = new Button(this);
    btnNext.setBackground(getResources().getDrawable(R.drawable.ic_keyboard_arrow_down_white_48pt_3x));

    Button btnPrev = new Button(this);
    btnPrev.setBackground(getResources().getDrawable(R.drawable.ic_keyboard_arrow_up_white_48pt_3x));

    searchStats = new TextView(this);

    ((LinearLayout) searchView.getChildAt(0)).addView(searchStats);
    ((LinearLayout) searchView.getChildAt(0)).addView(btnPrev, navButtonsParams);
    ((LinearLayout) searchView.getChildAt(0)).addView(btnNext, navButtonsParams);

    ((LinearLayout) searchView.getChildAt(0)).setGravity(Gravity.BOTTOM);

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            searchQuery = newText;
            if (!newText.equals("")) {
                lawTextWebView.findAllAsync(newText);
            } else {
                lawTextWebView.clearMatches();
            }
            return false;
        }
    });

    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!searchQuery.equals("")) {
                lawTextWebView.findNext(true);
            }
        }
    });

    btnPrev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!searchQuery.equals("")) {
                lawTextWebView.findNext(false);
            }
        }
    });

    return true;
}

and saving this actionbar markup res/menu/menu_toolbar_reader.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/action_search"
    android:title="@android:string/search_go"
    android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
    yourapp:showAsAction="always|collapseActionView"
    yourapp:actionViewClass="android.support.v7.widget.SearchView" />

<item
    android:id="@+id/action_contents"
    android:title="@string/contents"
    android:icon="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
    yourapp:showAsAction="always" />
</menu>
like image 33
Vassily Avatar answered Sep 28 '22 04:09

Vassily