add view over keyboard android
Solution 1:
Unfortunately, as far for Android 5.0 (dont know after) theres no way to get the keyboard height. So you can't use this value to position your View.
But, you can use the softKeyboard relayout to do what you want, by using softKeyboard (at the Activity manifest) with a value of "resize" will make your root layout View to be resized to the size of the remaining screen space. By positioning your view at the bottom will make it exactly on top of the keyboard.
PS: Also, your root view needs its height value to be match_parent instead of anything else (or you will need to deal in hard other ways)
Maybe you mean you want to replace the keyboard location instead of being on "top of it" (NORTH?), in this case, you should use a CustomView that extends View and not use the EditText that does open the keyboard for you
Solution 2:
You will have to hide the keyboard while showing window of emoji via:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edText.getWindowToken(), 0);
where edText is the edittext you have currently focus on.
EDIT: This is my code merged into yours.you can give it a try.
private void showEmojiPopup(boolean show) {
showingEmoji = show;
if (show) {
if (emojiView == null) {
if (getActivity() == null) {
return;
}
emojiView = new EmojiView(getActivity());
emojiView.setListener(new EmojiView.Listener() {
public void onBackspace() {
chatEditText1.dispatchKeyEvent(new KeyEvent(0, 67));
}
public void onEmojiSelected(String symbol) {
int i = chatEditText1.getSelectionEnd();
if (i < 0) {
i = 0;
}
try {
CharSequence localCharSequence = Emoji.replaceEmoji(symbol, chatEditText1.getPaint().getFontMetricsInt(), AndroidUtilities.dp(20));
chatEditText1.setText(chatEditText1.getText().insert(i, localCharSequence));
int j = i + localCharSequence.length();
chatEditText1.setSelection(j, j);
} catch (Exception e) {
Log.e(Constants.TAG, "Error showing emoji");
}
}
});
windowLayoutParams = new WindowManager.LayoutParams();
windowLayoutParams.gravity = Gravity.BOTTOM | Gravity.LEFT;
Log.d(TAG ,Build.VERSION.SDK_INT + " ");
if (Build.VERSION.SDK_INT >= 21) {
windowLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
} else {
windowLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
windowLayoutParams.token = getActivity().getWindow().getDecorView().getWindowToken();
}
windowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
Log.d("emoj",WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE + "");
}
final int currentHeight;
if (keyboardHeight <= 0)
keyboardHeight = App.getInstance().getSharedPreferences("emoji", 0).getInt("kbd_height", AndroidUtilities.dp(200));
currentHeight = keyboardHeight;
WindowManager wm = (WindowManager) App.getInstance().getSystemService(Activity.WINDOW_SERVICE);
windowLayoutParams.height = currentHeight;
windowLayoutParams.width = AndroidUtilities.displaySize.x;
try {
if (emojiView.getParent() != null) {
wm.removeViewImmediate(emojiView);
}
} catch (Exception e) {
Log.e(Constants.TAG, e.getMessage());
}
try {
wm.addView(emojiView, windowLayoutParams);
} catch (Exception e) {
Log.e(Constants.TAG, e.getMessage());
return;
}
if (!keyboardVisible) {
if (sizeNotifierRelativeLayout != null) {
sizeNotifierRelativeLayout.setPadding(0, 0, 0, currentHeight);
}
return;
}
} else {
removeEmojiWindow();
if (sizeNotifierRelativeLayout != null) {
sizeNotifierRelativeLayout.post(new Runnable() {
public void run() {
if (sizeNotifierRelativeLayout != null) {
sizeNotifierRelativeLayout.setPadding(0, 0, 0, 0);
}
}
});
}
}
}
@Override
public void onSizeChanged(int height) {
Rect localRect = new Rect();
getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
WindowManager wm = (WindowManager) App.getInstance().getSystemService(Activity.WINDOW_SERVICE);
if (wm == null || wm.getDefaultDisplay() == null) {
return;
}
if (height > AndroidUtilities.dp(50) && keyboardVisible) {
keyboardHeight = height;
App.getInstance().getSharedPreferences("emoji", 0).edit().putInt("kbd_height", keyboardHeight).commit();
}
if (showingEmoji) {
//code to hide keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edText.getWindowToken(), 0);
//end
int newHeight = 0;
newHeight = keyboardHeight;
if (windowLayoutParams.width != AndroidUtilities.displaySize.x || windowLayoutParams.height != newHeight) {
windowLayoutParams.width = AndroidUtilities.displaySize.x;
windowLayoutParams.height = newHeight;
wm.updateViewLayout(emojiView, windowLayoutParams);
if (!keyboardVisible) {
sizeNotifierRelativeLayout.post(new Runnable() {
@Override
public void run() {
if (sizeNotifierRelativeLayout != null) {
sizeNotifierRelativeLayout.setPadding(0, 0, 0, windowLayoutParams.height);
sizeNotifierRelativeLayout.requestLayout();
}
}
});
}
}
}
Solution 3:
You can create a custom EditText and disable showing the keyboard by override onCheckIsTextEditor. Refer code below:
public class NoKeyBoardEditText extends EditText {
@Override
public boolean onCheckIsTextEditor() {
return false;
}
}
Solution 4:
You can do something like this for creating View over softKeyboard.
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout android:id="@+id/my_content"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1">
<!-- Your content here If edittext here then change the layout_height="wrap_content" on your EditText to layout_height="0dip" and let the weight handle it. --> // like your edittext or something.
</FrameLayout>
<LinearLayout android:id="@+id/yourkeyboardlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Your yourkeyboardlayout items here -->
</LinearLayout>
</LinearLayout>
Note :
Remove this line from your code if you have used it.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
In your manifest :
<activity android:name="YourActivity"
android:theme="@android:style/Theme.NoTitleBar"
android:windowSoftInputMode="adjustResize">
</activity>
Make sure that you have adjusted your SoftInputMode to AdjustResize.
For more info regarding SoftInputmode refer this link.
And if you don't want to show to your layout when keyboard is down then just hide that layout.
Hope it will help you.