简体   繁体   中英

Android: Unable to get search button from searchView.

I want to change the search icon coz at the moment my search button icon was white..

Therefore i found a code to get the view of the search button.

SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
                .getActionView();
        //SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
        //searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

        int searchImgId = getResources().getIdentifier("android:id/search_button", null, null);
        AppCompatImageView v = (AppCompatImageView) searchView.findViewById(searchImgId);
        v.setImageResource(R.drawable.action_search);

Even I change AppCompatImageView to ImageView, I still have null. I guess the Id on the searchView's search button doesn't match with searchImgId so I get null. How do I get the right Id to retrieve the search button.

I think you didn't inflate menu xml file. You can do this, that is the reason you are getting null. you have to first inflate your menu file and from there you can get your search button ID.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.sample_actions, menu);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));

        searchView.setOnQueryTextListener(this);
        searchView.onActionViewExpanded();
        return super.onCreateOptionsMenu(menu);
    }

sample_action.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="@string/menu_search"
        app:showAsAction="always"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@drawable/search_white"/>
</menu>

Update

you may be looking for this

searchView.setBackgroundColor(Color.BLACK);

but you want to completely customize it, you can try this, what i am suggesting is less code and let UI take care of itself.

I know it is white and i know this is not what you want. you can easily customize it to any drawable and it also gives you more freedom to play with it

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="8dp"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginRight="-50dp"
            android:layout_weight="1"
            android:hint="some Text"
            android:paddingLeft="4dp" />

        <ImageView
            android:id="@+id/imageButton"
            style="?actionBarStyle"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@android:drawable/ic_menu_search" />
    </LinearLayout>

Output

this layout would appear something like this in which you can change the search imageView without any hustle and change the properties of editText as per requirement.

So your View will be taken care of in the layout so you will code only on the functionality. More Like Android ways

在此处输入图片说明

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