1

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.

Edric
  • 24,639
  • 13
  • 81
  • 91
Thomas
  • 13
  • 3
  • It appears that this [question](http://stackoverflow.com/questions/20482849/how-to-disable-searchview) may be useful for you. Set an onClickListener on your search view and then in the click method call clearFocus() – harveytoro Apr 19 '16 at 15:59
  • Hi @HB3.14, thanks for replying. I tried to call clearFocus() in the onClick(View v), but the text input of SearchView is opening. I need to prevent the text input field from opening. – Thomas Apr 19 '16 at 16:26
  • If the boolean method is called from within the onCreate method then you can disable the searchView when creating the activity by calling view.setInputType(InputType.TYPE_NULL) see [question](http://stackoverflow.com/questions/11436018/disabling-searchview) – harveytoro Apr 19 '16 at 16:41
  • Sorry @HB3.14, It`s not what I`m looking for. I don`t want to show the search box (edit text): https://i.ytimg.com/vi/KbaIRPPIk-4/hqdefault.jpg – Thomas Apr 21 '16 at 13:23

1 Answers1

0

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();
}
Bruno Ferreira
  • 1,561
  • 1
  • 10
  • 17