Android – Make whole search bar clickable

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

<SearchView
    android:id="@+id/search_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="7dp"
    android:layout_marginLeft="7dp"
    android:layout_marginRight="7dp"
    android:layout_marginBottom="7dp"
    android:background="@color/white" />

enter image description here

  • Android tests fail on Travis with ShellCommandUnresponsiveException
  • Get current visible text in textview
  • Android Chain Animations
  • Can't run project - Android Studio 2.0 Crashlytics issues
  • Is there any useful Android Animation Tutorial for beginners?
  • Dynamically create CheckBoxPreferences
  • The problem is only the search icon is clickable other area in the search bar is not clickable, when i click the icon only i can able to search.

    enter image description here

    Can you please help me to make the whole search area clickable.

    enter image description here

  • Reading Strings.xml file from apk file
  • Android: “BadTokenException: Unable to add window; is your activity running?” at showing dialog in PreferenceActivity
  • Setting up Google Wallet merchant account without business
  • How to bind both Mousedown and Touchstart, but not respond to both? Android, JQuery
  • How to change the hosts file on android
  • How to change android indeterminate ProgressBar color?
  • 5 Solutions collect form web for “Android – Make whole search bar clickable”

    What is the clickable mean? trigger search action or just make the edit area focused? If it is the first, you can just make the icon clickable=false. and make the whole layout clickable and implement a event listener.

    <SearchView
    android:id="@+id/search_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:click="onClick"
    android:layout_marginTop="7dp"
    android:layout_marginLeft="7dp"
    android:layout_marginRight="7dp"
    android:layout_marginBottom="7dp"
    android:background="@color/white" />
    

    The onClick method should be

    public void onClick(View v) {
       InputMethodManager im = ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE));
        im.showSoftInput(editText, 0);
    }
    

    This can be simply done by setting Iconified to false on OnClick of SearchView.

    searchBar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            searchBar.setIconified(false);
        }
    });
    

    Reference: Eric Lui’s answer

    Hopefully it will help.

    search_bar.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
    
                search_bar.onActionViewExpanded();
            }
        });
    

    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

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

    and to clear it add clearfocus to your searchview object.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.