简体   繁体   中英

Android SearchView with custom ActionBar

I want to create search view with custom action bar same as to search view in Action-bar. I don't know how to do this.I surfing on the net but most of the result will show the search view with Action-bar not custom Action-bar.But i want to add Search view on my custom Action Bar.Can someone help me .

Here is my custom Action Bar code

 protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_post);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        dbhelper = new MyDbHelper(this);
        dbhelper.onOpen(db);

        Intent intent = new Intent(CustomActionActivity.this, MyService.class);
        startService(intent);

        ActionBar mActionBar = getSupportActionBar();
        LayoutInflater mInflater = LayoutInflater.from(this);
        View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);

        ImageView imgSearchBtn = (ImageView)mCustomView.findViewById(R.id.imgSearchButton);
        imgSearchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("Click on ", " Searchview !!!! ");

            }
        });

        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);
        mActionBar.setDisplayShowCustomEnabled(true);

        mActionBar.setCustomView(mCustomView);
        Toolbar parent =(Toolbar) mCustomView.getParent();
        parent.setContentInsetsAbsolute(0, 0);

    }

I want to call search view on button click with collapse and expand function.

ImageView imgSearchBtn = (ImageView)mCustomView.findViewById(R.id.imgSearchButton);
        imgSearchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("Click on ", " Searchview !!!! ");

            }
        });

Thanks .

I see that you use Toolbar as your custom ActionBar (Note, that you have to use NoActionBar style in your styles.xml and add Toolbar manually to your layout) You still can use menu inflater:

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:title="Search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always"/>

</menu>

Just setSupportActionBar(mToolbar); where mToolbar is your Toolbar

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search_menu, menu);
    final MenuItem searchItem = menu.findItem(R.id.action_search);

    final SearchView searchView = (SearchView) searchItem.getActionView();

    if (searchView != null) {
        // do something with it
    }
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM