Frederick Eskens
Frederick Eskens

Reputation: 416

Hide keyboard when checkbox in ListView is clicked

I currently have ListView where each Item has a TextView and a CheckBox. On top of the ListView is an EditText to search the ListView. When a user has searched something and clicks one of the CheckBoxes or scrolls in the list of given results, I'd like the keyboard to go away. How can I do this?

I've handled the keyboard in my other activities using the InputMethodManager, but I can't call it inside the adapter because the EditText is outside its scope and I can't call it in the activity using the adapter because as far as it's concerned nothing's changing when clicking the CheckBox.

Can anyone point me in the right direction?

Thanks in advance

Upvotes: 1

Views: 423

Answers (2)

Artem Zelinskiy
Artem Zelinskiy

Reputation: 2210

1) Pass an interface to your adapter

class MyActivity {
      public interface OnCheckBoxClickListener{
          public void OnCheckboxClicked();
      }

      public class MyOnCheckBoxClickListener :OnCheckBoxClickListener {

          private WeakReference<Context> mContext;

          public MyOnCheckBoxClickListener(Context context){
                 mContext = new WeakReference<Context>(context);
          }

          public void OnCheckboxClicked(){
          Context context = null;
          mContext.TryGetTarget(out context);

          var editText = (context as Activity).FindViewById<EditText>(Resource.Id.edittextId);
          editText.ClearFocus();
          }
      }

      YourListAdapter adapter = new YourListAdapter(new MyOnCheckBoxClickListener(this)); 
}

2) Then in your adapter:

    class YourListAdapter {

    OnCheckBoxClickListener mListener;

        public override View GetView(int position, View convertView, ViewGroup parent) {   
            //code for view generation
            var checkbox = convertView.FindViewById<Checkbox>(Resource.Id.YourCheckbox);
            checkbox.Click += (sender, args) => {
                mListener.OnCheckboxClicked();
            }
        }
    }

Also you can achieve same thing using delegates.

Upvotes: 1

Guntars Ļuta
Guntars Ļuta

Reputation: 61

You can use this function when you need to hide keyboard.

private void hideKeyboard() {
    View view = this.getCurrentFocus();
    if (view != null) {
        view.clearFocus();
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

You can use it on listView:

listView.setOnScrollListener(new AbsListView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        hideKeyboard();
    }
});

Or checkBox:

checkBox.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        hideKeyboard();
        ...
    }
});

Upvotes: 0

Related Questions