Reputation: 954
I'm running into some problems trying to detect if the software keyboard is visible or not.
I search for a solution (SO included) but without luck. What's weird is that I did this in the same app by checking if the window size has changed, like this:
final View activityRootView = findViewById(R.id.tlFull);
//tlFull is the activity table layout
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener()
{
public void onGlobalLayout()
{
int heightDiff = activityRootView.getRootView()
.getHeight() - activityRootView.getHeight();
if (heightDiff > 100)
{
//did some stuff here
}
else
{
//and here
}
}
});
and by adding android:windowSoftInputMode="adjustResize" in the manifest file.
Now I am trying to do the same thing on a login activity but it seams that for some reason heightDiff is always 38 so this no longer works. Same applies for every method found on SO that uses the same approach.
I also tried this but it always returns true.
final View activityRootView = findViewById(R.id.tlFull);
activityRootView.getViewTreeObserver()
.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
if (getResources().getConfiguration()
.keyboardHidden == Configuration.KEYBOARDHIDDEN_NO)
{}
else
{}
}
});
Also, since I am not using fragments I cannot use this:
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText())
{}
else
{}
I also tried a different approach. I do what I want to do in etPass (the edit text that triggers the keyboard) onClick method:
etPass.setOnClickListener(new OnClickListener()
{
public void onClick(View viewIn)
{
ivImageView.setVisibility(View.GONE);
}
});
Unfortunately, even if the keyboard shows up the image view does not always disappear on the first tap so I have to tap again.
The second problem is that I tried to show the iv again in the onBackPressed() method. This works but not how it should.
When I first hit the back button the keyboard disappears but I have to tap again to make the image visible. (I tried the solution provided here but it didn't work: EditText with soft keyboard and "Back" button)
Also, this prevents the app from closing on onBackPressed().
Any ideas on how to solve this and why that diff is always the same? I couldn't find a better solution for this and now I fear that the same problem might appear on the old activity in the future (although it seams to work fine in tests)
Upvotes: 0
Views: 15298
Reputation: 1442
you just use this method to sense the Configuration changes in your application....
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
}
}
Upvotes: 2