SearchView remove blue focus line and show cursor at certain position

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 02:09:41

I was able to solve it by setting the background of the default searchview plate as follows

int searchPlateId = searchView.getContext().getResources()
            .getIdentifier("android:id/search_plate", null, null);
    View searchPlateView = searchView.findViewById(searchPlateId);
    if (searchPlateView != null) {
        searchPlateView.setBackgroundColor(Color.BLACK);
    }

This article helped.article link

You can use

android:queryBackground="@android:color/transparent"

I was able to make it work by clearingfocus on the search edit text during the menu creation function. Code below:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        ...

        final MenuItem searchItem = menu.findItem(R.id.action_search);

        if (searchItem != null) {
            // Associate searchable configuration with the SearchView
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            final SearchView view = (SearchView) searchItem.getActionView();
            if(view !=null){
                LinearLayout searchPlate = (LinearLayout)mSearchView.findViewById(R.id.search_plate);
                if(searchPlate != null){
                    mSearchEditText = (EditText)searchPlate.findViewById(R.id.search_src_text);
                    if(mSearchEditText != null){
                        mSearchEditText.clearFocus();     // This fixes the keyboard from popping up each time
                    }
                }
            }
        }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!