Android: How to make the SearchView take up the entire horizontal space in ActionBar?

妖精的绣舞 提交于 2019-12-06 11:15:51

问题


In the following screenshot, the SearchView widget in the ActionBar does not take up the entire space available.

I need it to take up all the horizontal space present in the ActionBar (so it spans the entire screen horizontally).

What I tried:

  • Programatically, tried this in the onCreateOptionsMenu:

    View searchViewView = (View) searchView;
    searchViewView.getLayoutParams().width=LayoutParams.MATCH_PARENT;`
    

    This gives:

    java.lang.NullPointerException: Attempt to write to
    field 'int android.view.ViewGroup$LayoutParams.width' on a null
    object reference ...       
    TestActionBarTwo.MainActivity.onCreateOptionsMenu(MainActivity.java:57)
    
  • and in the menu resource:

    The android:layout_width="match_parent" does not seem to affect anything.

  • I also tried android:layout_weight="1" (assuming that the ActionBar has a LinearLayout), but that doesn't work either.

So is there a way that the SearchView (not iconified) takes up the entire horizontal space of the ActionBar?


EDIT 1

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.i(TAG, "onCreateOptionsMenu OF MainActivity CALLED."); //check

        getMenuInflater().inflate(R.menu.activity_main_action_bar_menu, menu);

        getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        // SearchAction
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchMenuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem); 
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);

        //Adding event listeners
        searchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() { 
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.i(TAG, "onFocusChange OF ONFocusChangeListener IN MainActivity CALLED."); //check
            }
        });

        return super.onCreateOptionsMenu(menu);
    }

And the menu resource is:

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


Edit 2: Even when I DO NOT set setIconifiedByDefault(false), even though it does not seem relevant:`

Code change:

// SearchAction
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchMenuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem); 
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        //searchView.setIconifiedByDefault(false);************************

        getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

Result, when I clicked the iconified search icon to expand it:


Edit 3: Copied code from the answer by @MaheeraJazi , still doesn't seem to work. I am now posting the entire SSCCE:

MainActivity.java:-

public class MainActivity extends ActionBarActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate OF MainActivity CALLED."); // check
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportActionBar().setDisplayShowHomeEnabled(false); 
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        Log.i(TAG, "onNewIntent OF MainActivity CALLED."); // check
        handleIntent(intent);
        setIntent(intent);
    }

    private void handleIntent(Intent intent) {
        Log.i(TAG, "handleIntent OF MainActivity CALLED."); // check
        if (intent.getAction().equals(Intent.ACTION_SEARCH)) {
            showResults(intent.getStringExtra(SearchManager.QUERY));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.i(TAG, "onCreateOptionsMenu OF MainActivity CALLED."); // check

        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main_action_bar_menu, menu);

        // SearchAction
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchMenuItem = menu.findItem(R.id.mainActivity_actionSearch);
        SearchView searchViewActionView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
        searchViewActionView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchViewActionView.setIconifiedByDefault(true);

        SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextChange(String newText) {
                Log.i(TAG, "onQueryTextChange OF  OnQueryTextListener IN MainActivity CALLED.");//check
                // this is your adapter that will be filtered
                return true;

            }
            @Override
            public boolean onQueryTextSubmit(String query) {
                Log.i(TAG, "onQueryTextChange OF  OnQueryTextListener IN MainActivity CALLED.");//check
                return true;
            }
        };
        searchViewActionView.setOnQueryTextListener(queryTextListener);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        Log.i(TAG, "onOptionsItemSelected OF MainActivity CALLED."); // check
        switch (menuItem.getItemId()) {
        case R.id.mainActivity_actionSearch:
            onSearchRequested();
            return true;
        default:
            return super.onOptionsItemSelected(menuItem);
        }
    }

    private void showResults(String searchQuery) {
        Log.i(TAG, "showResults OF MainActivity CALLED."); // check
    }
}

activity_main_action_bar_menu.xml:-

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:testactionbartwo="http://schemas.android.com/apk/res-auto" >
      <item android:id="@+id/mainActivity_actionSearch"
            android:icon="@drawable/ic_action_search"
            android:title="@string/mainActivity_searchActionTitle"
            testactionbartwo:showAsAction="always"
            testactionbartwo:actionViewClass="android.support.v7.widget.SearchView"  />
</menu>

activity_main.xml:-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

Manifest File:-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="khanZarah.tests.TestActionBarTwo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.Light"
            android:launchMode="singleTop"
            android:uiOptions="splitActionBarWhenNarrow" >
            <meta-data android:name="android.support.UI_OPTIONS"
                android:value="splitActionBarWhenNarrow" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>

        <meta-data android:name="android.app.default_searchable"
            android:value=".MainActivity"/>
    </application>

回答1:


The default width of the search view in toolbar is defined by non-public attribute abc_search_view_preferred_width. It set to 320dp for all screen sizes.

A workaround is to set maxWidth of SearchView:

searchView.setMaxWidth(Integer.MAX_VALUE);



回答2:


Acutely you don't need to specify android:layout_weight or any other properties. In my code I do that and everything goes well !

On main Menu you need to add this:

  <!-- Search, should appear as action button -->
<item
    android:id="@+id/action_search"
    android:icon="@drawable/searchmenu"
    android:title="@string/search"
    yourapp:showAsAction="always"
    yourapp:actionViewClass="android.support.v7.widget.SearchView"
    />
<item

On your Activity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.top_menu, menu);

    // SearchAction
    SManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchMenuItem = menu.findItem(R.id.action_search);
    searchViewAction = (SearchView) MenuItemCompat
            .getActionView(searchMenuItem);
    searchViewAction.setSearchableInfo(SManager
            .getSearchableInfo(getComponentName()));
    searchViewAction.setIconifiedByDefault(true);

    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextChange(String newText) {
            // this is your adapter that will be filtered
                return true;

        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            // this is your adapter that will be filtered

            return true;
        }
    };
    searchViewAction.setOnQueryTextListener(queryTextListener);

    return super.onCreateOptionsMenu(menu);
}

The photo look like:

when getSupportActionBar().setDisplayShowHomeEnabled(false) the searchview look like:

I hope this help!



来源:https://stackoverflow.com/questions/28021840/android-how-to-make-the-searchview-take-up-the-entire-horizontal-space-in-actio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!