Remove accent on search using android searchview

1

I would like when searched in my listview João to display João, Joao, joão, joao .

I'm using a searchview, I read something about Normalizer but I did not quite understand it.

 @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) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                arrayAdapter.getFilter().filter(newText);
                return true;
            }
        });

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

    }
    
asked by anonymous 03.03.2018 / 19:24

1 answer

2

Do this:

@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);

}
    
03.03.2018 / 19:39