How to hide close button in searchview when it's empty android
<p>I am working on an android application and implemented searchview in it. What I want to hide the close button in searchview when it is initializes and only show that button when I start typing. Hide the close button of searchview when it is empty. </p>

<p>I've done this like below.</p>

<pre><code>public void initializeSearchView(){

    SearchManager searchManager = (SearchManager)mContext.getSystemService(Context.SEARCH_SERVICE);
    mSearchView.setSearchableInfo(searchManager.getSearchableInfo(((Activity)mContext).getComponentName()));
    mSearchView.setIconifiedByDefault(true);
    mSearchView.setIconified(false);
    mSearchView.clearFocus();
    int searchImgId = mContext.getResources().getIdentifier("android:id/search_mag_icon", null, null);
    ImageView searchIconView = (ImageView) mSearchView.findViewById(searchImgId);
    if(searchIconView != null){
        searchIconView.setImageResource(R.drawable.ic_action_search);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        searchIconView.setLayoutParams(layoutParams);
    }
    int searchPlateId = mSearchView.getContext().getResources().
            getIdentifier("android:id/search_plate", null, null);
    View searchPlate = mSearchView.findViewById(searchPlateId);
    if (searchPlate != null) {
        int searchTextId = searchPlate.getContext().getResources().
                getIdentifier("android:id/search_src_text", null, null);
        final TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
        searchText.setHintTextColor(ContextCompat.getColor(mContext, R.color.gray_stroke_color));
        mSearchView.setQueryHint("Search");
        int searchCloseBtnId = searchPlate.getContext().getResources().
                getIdentifier("android:id/search_close_btn", null, null);
        searchCloseBtn = (ImageView) searchPlate.findViewById(searchCloseBtnId);
        searchCloseBtn.setVisibility(View.GONE);
        searchCloseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchText.setText("");
                mSearchView.clearFocus();
            }
        });
    }
    mSearchView.setOnQueryTextListener(this);
}
</code></pre>

<p>I've set the Visibility GONE to the close button at the time of searchview initialization but it is not working. How can I achieve this, please help if anyone have idea here.</p>

<p>Thanks a lot in advanced.</p>