How to add buttons to search view in android toolbar?

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;
            }
        });

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>

Tags:

Android