How to add buttons to search view in android toolbar?

Vassily

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?

Vassily

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to add buttons like refresh and search in ToolBar in Android?

From Dev

How to use free jqgrid search and view toolbar buttons outside its toolbar

From Dev

How to add buttons to grid view?- Android

From Dev

How to replace container view via toolbar buttons?

From Dev

How to add/remove buttons to Nautilus toolbar?

From Dev

When add a custom view in Android Toolbar, there will be a marginLeft

From Dev

How to add search button on toolbar in material design

From Dev

How to properly add custom view to the toolbar?

From Dev

How to properly add Custom View in toolbar?

From Dev

How to use toolbar search to search in a fragment's expandable list view

From Dev

how to add button left to cross sign in search view android

From Dev

How do I add toolbar buttons to a custom tinymce dropdown menu?

From Dev

android search view cursor not visible as same color of toolbar

From Dev

how to add buttons dynamically in android?

From Dev

Add search toolbar over google map like in native android app

From Dev

How to add multiple buttons in accessory view of UITableView?

From Dev

How to create a view with buttons and add them to a UITableViewController

From Dev

How to add multiple buttons in accessory view of UITableView?

From Dev

How to hide all toolbar/windows and buttons in Android Studio with a keyboard shortcut?

From Dev

How to add more shortcuts to android studio toolbar?

From Dev

Android Toolbar Action Buttons not showing

From Dev

Android Adding Buttons to Toolbar Programmatically

From Dev

How to highlight buttons in a GTK toolbar

From Dev

How to highlight buttons in a GTK toolbar

From Dev

Add toolbar in android app

From Dev

Add items to android toolbar

From Dev

Add icon to Toolbar in Android

From Dev

Add an image to the toolbar in android

From Dev

Add custom view to the right of toolbar

Related Related

  1. 1

    How to add buttons like refresh and search in ToolBar in Android?

  2. 2

    How to use free jqgrid search and view toolbar buttons outside its toolbar

  3. 3

    How to add buttons to grid view?- Android

  4. 4

    How to replace container view via toolbar buttons?

  5. 5

    How to add/remove buttons to Nautilus toolbar?

  6. 6

    When add a custom view in Android Toolbar, there will be a marginLeft

  7. 7

    How to add search button on toolbar in material design

  8. 8

    How to properly add custom view to the toolbar?

  9. 9

    How to properly add Custom View in toolbar?

  10. 10

    How to use toolbar search to search in a fragment's expandable list view

  11. 11

    how to add button left to cross sign in search view android

  12. 12

    How do I add toolbar buttons to a custom tinymce dropdown menu?

  13. 13

    android search view cursor not visible as same color of toolbar

  14. 14

    how to add buttons dynamically in android?

  15. 15

    Add search toolbar over google map like in native android app

  16. 16

    How to add multiple buttons in accessory view of UITableView?

  17. 17

    How to create a view with buttons and add them to a UITableViewController

  18. 18

    How to add multiple buttons in accessory view of UITableView?

  19. 19

    How to hide all toolbar/windows and buttons in Android Studio with a keyboard shortcut?

  20. 20

    How to add more shortcuts to android studio toolbar?

  21. 21

    Android Toolbar Action Buttons not showing

  22. 22

    Android Adding Buttons to Toolbar Programmatically

  23. 23

    How to highlight buttons in a GTK toolbar

  24. 24

    How to highlight buttons in a GTK toolbar

  25. 25

    Add toolbar in android app

  26. 26

    Add items to android toolbar

  27. 27

    Add icon to Toolbar in Android

  28. 28

    Add an image to the toolbar in android

  29. 29

    Add custom view to the right of toolbar

HotTag

Archive