leithdm
5/7/2020 - 12:27 PM

Keyboard: dismissing keyboard if user clicks on background

//1.implement View.OnClickListener
//2. set for e.g. the RelativeLayout as a listener
//3. implement onClick()

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  
  //
  Override
    protected void onCreate(Bundle savedInstanceState) {
        _logoImageView.setOnClickListener(this); //in this example have an ImageView and the RelativeLayout as setOnClickListener
        _backgroundLayout.setOnClickListener(this);
    }
        
        public void onClick(View v) {
        if ((v.getId() == R.id.logoImageView) || (v.getId() == R.id.backgroundLayout)){
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }