Custom searchview

Asked

Viewed 841 times

1

I need to do a searchView like this: inserir a descrição da imagem aqui

I need to put the white icon and the hint too.

  • 2

    Did you ever start any code? post it too, it gets easier to help

  • Pretty much nothing: <Searchview android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/search" android:queryHint="@string/search" android:Gravity="center" android:searchHintIcon="@drawable/ic_busca_menu"/>

  • Carlos, I left a reply below with an example of code that I use.

1 answer

2


To do well customized like this, I advise making an Edittext.

Example:

<EditText
      android:id="@+id/home_search"
      android:hint="pesquisar por nome ou profissão"
      android:textSize="15sp"
      android:inputType="textCapSentences"
      android:textColor="@android:color/white"
      android:textColorHint="@android:color/white"
      android:drawableLeft="@android:drawable/ic_menu_search"
      android:drawablePadding="8dp"
      android:paddingLeft="24dp"
      android:layout_marginLeft="24dp"
      android:layout_marginRight="24dp"
      android:background="@android:color/black"
      android:layout_width="match_parent"
      android:layout_height="45dp"
      android:imeOptions="actionSearch"
      android:imeActionLabel="Search"/>

That will be the result:

inserir a descrição da imagem aqui

Ai in Java, you declare Edittext normally and add a setOnEditorActionListener in it

Example:

EditText home_search = (EditText) findViewById(R.id.home_search);
home_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                        actionId == EditorInfo.IME_ACTION_DONE ||
                        actionId == KeyEvent.ACTION_DOWN ) {

                    String searchTerm = home_search.getText().toString();

                    if(searchTerm != null && searchTerm.length() > 2){
                        metodoQueRealizaABusca(searchTerm);
                        home_search.setText("");
                    }else{
                         Toast.makeText(this, "Digite pelo menos 3 letras para buscar",Toast.LENGTH_SHORT).show();
                    }

                    return true;
                }
                else {
                    return false;
                }
            }
        });

In this scheme that I use, the user has to enter at least 3 letters to be able to search.

And the search will stay within the method method) where he will receive the searchTerm as a parameter.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.