Android - Make whole search bar clickable

前端 未结 12 1971
情深已故
情深已故 2020-12-29 18:14

I am using a search-view element in my fragment to implement search feature.



        
12条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-29 18:52

    The trick isn't so much to make the entire area clickable as much as it is to expand it and then hide the keyboard.

    First, your layout in the XML is fine, leave it as is, in your Java, you want to have the following:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
    
        //Define your Searchview
        SearchView searchView = (SearchView) findViewById(R.id.search_bar);
    
        //Turn iconified to false:
        searchView.setIconified(false);
        //The above line will expand it to fit the area as well as throw up the keyboard
    
        //To remove the keyboard, but make sure you keep the expanded version:
        searchView.clearFocus();
    }
    

    What this accomplishes is it expands the keyboard to the entire length of the area, it focuses on it, which allows the entire area to be clickable, and then it removes the keyboard so that it looks to the user like they are able to click that field and bring up a keyboard.

    Should accomplish what you are looking for.

    -Sil

提交回复
热议问题