0

I'm using this code that I got here to make a textview gone when a keyboard pops up. But when the keyboard is hidden I want the textview to be back again to its normal position. Also, when the textview is gone the edittext below it will go up. Then go back its normal position when the keyboard is hidden. How can I do that? Thanks!

final View activityRootView = findViewById(R.id.LinearLayout1);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
            if (heightDiff > 100)  // if more than 100 pixels, its probably a keyboard...
                findViewById(R.id.titleTextView).setVisibility(View.GONE);

         }
    });
KatrinaP
  • 1
  • 3

2 Answers2

0
if (heightDiff > 100)  {
                findViewById(R.id.titleTextView).setVisibility(View.GONE);
      }else{
            findViewById(R.id.titleTextView).setVisibility(View.VISIBLE);
      }

That should do.

Aditya Naique
  • 1,010
  • 12
  • 21
0

It's simple - if it's more than 100 hundred when keyboard is shown it should be less than 100 when the keyboard is hidden :

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

    findViewById(R.id.titleTextView).setVisibility(View.GONE);

}else{

    findViewById(R.id.titleTextView).setVisibility(View.VISIBLE);

}

EDIT

Try this:

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

     findViewById(R.id.titleTextView).setVisibility(View.GONE);

}else if(heightDiff < 200){

     findViewById(R.id.titleTextView).setVisibility(View.VISIBLE);

}