How to make the Action Bar SearchView fill Entire action bar?

enter image description here

I was trying to create an action bar search in my application, but in the expanded state the SearchView is not taking the entire action bar width( it is still showing other actions)!

  • Android layout folder organization
  • Add javascript into WebView
  • Dynamically change the number of columns of a GridLayoutManager
  • (Deprecated) Fragment onOptionsItemSelected not being called
  • Gradle Android plugin generate R files for different product flavors?
  • How do you reduce space between floating editText hint and editText box in Android?
  • So, how to make the SearchView fill the full ActionBar Area (as in GMAIL app)?

    Related posts:

    Remove vertical padding from horizontal ProgressBar
    How to create an Android Activity and Service that use separate processes
    How to run terminal command in Android application?
    Is a RelativeLayout more expensive than a LinearLayout?
    android ActivityOptions.isReturning() NullPointerException
    Switchcompat not displaying the Switch
  • Printing From Android to Mobile Bluetooth Printer with PhoneGap
  • How to auto-adjust text size on a multi-line TextView according to the view max dimensions?
  • Center elemets of HorizontalScrollView when not enough to make it scroll
  • Restart the service even if app is force-stopped and Keep running service in background even after closing the app How?
  • The method showDialog(int) from the type Activity is deprecated in android?
  • HTTP Caching with Retrofit 2.0.x
  • 4 Solutions collect form web for “How to make the Action Bar SearchView fill Entire action bar?”

    None of the above solutions worked for me. Setting the MaxWidth of the SearchView worked for me:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_search, menu);
        SearchView searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView();
        searchView.setMaxWidth(Integer.MAX_VALUE);
    

    Got it

    enter image description here

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my_swipe_tabbed, menu);
    
        MenuItem searchViewItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) searchViewItem.getActionView();
        searchView.setIconifiedByDefault(false);
        ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
        searchView.setLayoutParams(params);
        searchViewItem.expandActionView();
        return true;
    }
    
    
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            tools:context=".MySwipeTabbedActivity" >
        <item android:id="@+id/action_search"
                android:title="Search"
                android:icon="@android:drawable/ic_menu_search"
                android:showAsAction="always|collapseActionView"
                android:actionViewClass="android.widget.SearchView"
                />
        <item android:id="@+id/action_share"
                android:title="Share"
                android:icon="@android:drawable/ic_menu_share"
                android:showAsAction="ifRoom" />
        <item android:id="@+id/action_settings"
                android:title="Settings"
                android:icon="@android:drawable/ic_menu_preferences"
                android:showAsAction="never" />
    </menu>
    

    [Optional] Avoid the searchView from collapsing:

        searchViewItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                return true;
            }
    
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                return false; //never collapse
            }
        });
    

    The Answer given by @Hiep is correct i followed those steps to get my solution.but i was using ActionbarCompact lib so it take some changes to made so that i get it to work This solution is only the modified answer of Hiep if you are using appcompact

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:materialdesign="http://schemas.android.com/apk/res-auto" >
    
    <item android:id="@+id/action_search"
          android:title="search"
          android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
          materialdesign:showAsAction="always|collapseActionView"
          materialdesign:actionViewClass="android.support.v7.widget.SearchView" />
    
    </menu>
    

    And in the Main Class onCreateOptionsMenu

      @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_swipe_tabbed, menu);
    
        MenuItem searchItem = menu.findItem(R.id.action_search);
    
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    
    
    
        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    
    
        ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
        searchView.setLayoutParams(params);
        searchView.setIconified(false);
    
         MenuItemCompat.expandActionView(searchItem);
    
       return super.onCreateOptionsMenu(menu);
    }
    

    Avoid the searchView from collapsing:If u use the above method in Appcompact it will create a crash so use this solution to avoid that.

          MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
    
            /* (non-Javadoc)
             * @see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionExpand(android.view.MenuItem)
             */
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
    
                return true;
            }
    
            /* (non-Javadoc)
             * @see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionCollapse(android.view.MenuItem)
             */
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
    
                return false;
            }
        });
    

    Thank you

    Try This Only will work and keep all other items set to ifRoom

    <item
        android:id="@+id/action_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:title="@string/action_search"
        app:showAsAction="always"/>
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.