Android - Actionbar with Search Filter and setting icon -


i trying implement action bar using action bar.
have 2 action buttons, 1 of search button , second 1 settings.

i have implemented setting button, can't implement search view.

how can implement that?
have idea on how can achieve that?

as first step have prepare search_menu.xml:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"              xmlns:app="http://schemas.android.com/apk/res-auto"> <item         android:id="@+id/action_search"         android:icon="@android:drawable/ic_menu_search"         app:showasaction="ifroom"         app:actionviewclass="android.support.v7.widget.searchview"         android:title="search"/>    ... others menu items </menu> 

in next step have use menu in fragment or activity , set onquerytextlistener searchview shown in code below:

@override public void oncreateoptionsmenu(menu menu, menuinflater inflater) {     inflater.inflate(r.menu.search_menu, menu);      final searchview search = (searchview) menu.finditem(r.id.action_search).getactionview();     search.setonquerytextlistener(new searchview.onquerytextlistener() {         @override         public boolean onquerytextsubmit(string s) {             return false;         }          @override         public boolean onquerytextchange(string s) {             return false;         }     }); } 

if want use menu in fragment remember sethasoptionsmenu(true); method.

if want customise searchview have create own style shown in code below:

<style name="customapptheme" parent="theme.appcompat">     <item name="searchviewstyle">@style/customsearchviewstyle     </item> </style> <style name="customsearchviewstyle" parent="widget.appcompat.searchview">     <!-- background search query section (e.g. edittext) -->     <item name="querybackground">...</item>     <!-- background actions section (e.g. voice, submit) -->     <item name="submitbackground">...</item>     <!-- close button icon -->     <item name="closeicon">...</item>     <!-- search button icon -->     <item name="searchicon">...</item>     <!-- go/commit button icon -->     <item name="goicon">...</item>     <!-- voice search button icon -->     <item name="voiceicon">...</item>     <!-- commit icon shown in query suggestion row -->     <item name="commiticon">...</item>     <!-- layout query suggestion rows -->     <item name="suggestionrowlayout">...</item> </style> 

example old style of edittext in searchview

<style name="customapptheme" parent="theme.appcompat">     <item name="searchviewstyle">@style/customsearchviewstyle     </item> </style> <style name="customsearchviewstyle" parent="widget.appcompat.searchview">     <item name="querybackground">@android:drawable/editbox_background_normal</item> </style> 

remember set style in androidmanifest activity:

    <activity         android:name=".youractivity"         android:theme="@style/customapptheme" 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -