Remove word accentuation in searchView

Asked

Viewed 69 times

0

I’m trying to remove the accent from the researcher’s words, but my code isn’t working.

I’d like you to look at an example word Você all variations ofVoce, Você, você, você, VOCE, VOCÊ.

Thank you

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.busca, menu);

    MenuItem menuItem = menu.findItem(R.id.sv);

    SearchView searchView = (SearchView) menuItem.getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
        //Irá tirar não só acentuações mas também qualquer caractere fora de ASCII
        String texto;
             texto = Normalizer.normalize(query, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");

        //seu código

            return false;
        }

       //se for pra passar o texto já modificado para o arrayAdapter, vc faz:
        @Override
        public boolean onQueryTextChange(String newText) {
             String texto;
             texto = Normalizer.normalize(newText, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");

            arrayAdapter.getFilter().filter(texto);
            return true;
        }
    });

    getMenuInflater().inflate(R.menu.activity_main, menu);
    return super.onCreateOptionsMenu(menu);

}
  • Young, here only accepted in Portuguese. Please edit and translate

  • ops, sorry.. Edited

  • All right, but if you’d be so kind as to edit ;)

1 answer

0

Try this

public static String stripAccents(String s) 
{
    s = Normalizer.normalize(s, Normalizer.Form.NFD);
    s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
    return s;
}

Browser other questions tagged

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