Dimitri
Dimitri

Reputation: 687

Android hide keyboard on backkeypressed

I have successfully made part of my layout dissapear using

    activityRootView = findViewById(R.id.bottom_layout);    
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
           System.out.println("Height: "+heightDiff);

            if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...

            RelativeLayout bottom = (RelativeLayout)findViewById(R.id.bottom_layout);
            bottom.setVisibility(View.GONE);

            }else {

                RelativeLayout bottom = (RelativeLayout)findViewById(R.id.bottom_layout);
                bottom.setVisibility(View.VISIBLE);
            }
         }
    });

Upvotes: 0

Views: 168

Answers (2)

Timothy T.
Timothy T.

Reputation: 602

There is another way more proper to detect when the keyboard appeard and is hidden :

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
    final int actualHeight = getHeight();

    if (actualHeight > proposedheight){
        // Keyboard is shown
    } else {
        // Keyboard is hidden
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

source : Any android event when keyboard slide out

Upvotes: 1

Sunil Kumar
Sunil Kumar

Reputation: 7092

try to this one

  @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
           button.setVisibility(View.VISIBLE);
          imageview.setVisibility(View.VISIBLE);
        InputMethodManager inputMethodManager = (InputMethodManager)  this.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
    }

Upvotes: 2

Related Questions