Custom SearchView whole clickable in android

前端 未结 4 638
遇见更好的自我
遇见更好的自我 2021-01-01 15:18

I have a SearchView widget in my app, and I want to ask some questions about making it custom. First of all, you can start search only by clicking on search ico

相关标签:
4条回答
  • 2021-01-01 15:45

    By default the SearchView is 'iconified', which is displayed as a magnifying glass icon and only if the user clicks on the icon, then the edit field expands.

    To enable the user to click anywhere on the SearchView and expand the input field. We just need to add a click listener and call setIconified(false) when the user clicks.

    searchView = (SearchView)findViewById(R.id.searchView);
    searchView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchView.setIconified(false);
            }
        });
    
    0 讨论(0)
  • 2021-01-01 15:52

    By default the SearchView is 'iconified'. So you should use this code in java:

    SearchView searchView = (SearchView) findViewById(R.id.searchView);
    searchView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            searchView.setIconified(false);
        }
    });
    

    or use this in xml:

    <SearchView
            android:id="@+id/searchView"
            android:iconifiedByDefault="false" />
    

    this 2 ways is correct, but different in icons display.

    0 讨论(0)
  • 2021-01-01 16:03

    To allow writing when clicking in the whole component, I'd recommend you declaring iconifiedByDefault="false" on the xml.

    <SearchView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:iconifiedByDefault="false"/>
    
    0 讨论(0)
  • 2021-01-01 16:09

    Another simple way to do it by setting onActionViewExpanded().

    searchView = (SearchView)findViewById(R.id.searchView);
    searchView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            searchView.onActionViewExpanded();
        }
    });
    
    0 讨论(0)
提交回复
热议问题