0

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.

LittleFunny
  • 8,155
  • 15
  • 87
  • 198

2 Answers2

0

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>
Anuj Sharma
  • 4,294
  • 2
  • 37
  • 53
-1

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

enter image description here

Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30