Android - Make whole search bar clickable

前端 未结 12 1896
情深已故
情深已故 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

    0 讨论(0)
  • 2020-12-29 18:52

    in code:

    private void initSearchView(View layout) {
    
        // Locate the EditText in listview_main.xml
        searchInput = (SearchView) layout.findViewById(R.id.search);
        //searchInput.onActionViewExpanded();//force show keyboard at start
    
        //==>region to enable search after click in input-text(not only at magnifier icon)
        searchInput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchInput.onActionViewExpanded();
            }
        });
    
        searchInput.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (!b) {
                    if (searchInput.getQuery().toString().length() < 1) {
                        searchInput.setIconified(true); 
                    }
    
                    searchInput.clearFocus();
    
                }
            }
        });
        //endregion
    

    }

    and in my layout:

     <androidx.appcompat.widget.SearchView
            android:id="@+id/search"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar_main"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="2dp"
            android:background="@drawable/search_border"
            android:inputType="text"
            android:layoutDirection="ltr"
            android:searchSuggestThreshold="2"
            app:queryHint="@string/search"
            app:searchHintIcon="@null" />
    
    0 讨论(0)
  • 2020-12-29 18:57

    add this to your xml android:iconifiedByDefault="false" it will keep open your searchview.

    and to clear it add clearfocus to your searchview object.

    0 讨论(0)
  • 2020-12-29 19:02

    Write the lines of code given below in your Java file:

    SearchView searchView = (SearchView) findViewById(R.id.searchView);
    searchView.setQueryHint("Enter your address");
    searchView.setIconified(false);
    searchView.clearFocus();
    

    After that, don't forget to write android:focusable="true" in your XML file inside SearchView tag.

    For example:

    <SearchView 
    android:id="@+id/searchView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:focusable="true" />
    
    0 讨论(0)
  • 2020-12-29 19:05
    search_bar.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            search_bar.onActionViewExpanded();
        }
    });
    
    0 讨论(0)
  • 2020-12-29 19:08

    Use the below code.

    <androidx.appcompat.widget.SearchView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/earningSearchView"
            app:queryHint="Search"
            app:iconifiedByDefault="false"
            app:queryBackground="@android:color/transparent"/>
    

    0 讨论(0)
提交回复
热议问题