Search code examples
androidsearchview

Android SearchView OnClickListener


I have a working SearchView (on toolbar) on my Project. I have a boolean method for check network connection.

What I`m trying to do:

If myApp is Offline, when the user click on search icon, do not permit searchView expansion and open a dialog...

Is it possible without extending SearchView class ?

Thanks.


Solution

  • make something like that:

     public boolean onOptionsItemSelected(MenuItem item) {
    
        switch (item.getItemId()){
            case android.R.id.yoursearchviewid:
                if(!isNetworkAvailable(yourcontext)){
                 item.setActionView(null);
                 Snackbar.make(yourview,"No internet contection.",Snackbar.LENGTH_LONG).setAction("Turn on", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            defenicoes = new Intent(Settings.ACTION_WIFI_SETTINGS);
                            startActivity(defenicoes);
                        }
                    }).show();;
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    
    public boolean isNetworkAvailable(final Context context) {
        final ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
        return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
    }