How to get Keyboard Height by the time app is opened - android

I have used the below code for getting the keyboard height.
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect rect = new Rect();
view.getWindowVisibleDisplayFrame(rect);
int screenHeight = view.getRootView().getHeight();
int keyboardHeight = screenHeight - rect.bottom;
if(keyboardHeight != 0){
if(orientation == Configuration.ORIENTATION_LANDSCAPE)
AppConfig.landscapeKeyboardHeight = keyboardHeight;
else if(orientation == Configuration.ORIENTATION_PORTRAIT)
AppConfig.portraitKeyboardHeight = keyboardHeight;
}
}
});
But this gives height only when the app opens keyboard for the first time at least. I want the height of keyboard even before the it gets open for the first time. Is there any way of doing this? Thanks in advance...

I had the same problem before for getting the height of keyboard for showing some dialog boxes. You can use the method below to solve it.
rootView = getWindow().getDecorView().getRootView();
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect rect = new Rect();
rootView.getWindowVisibleDisplayFrame(rect);
int screenHeight = rootView.getHeight();
int keyboardHeight = screenHeight - (rect.bottom - rect.top);
if(keyboardHeight > screenHeight / 3){
keyboardActive = true;
Log.d("Keyboard", "Active");
}
else{
keyboardActive = false;
Log.d("Keyboard", "Not Active");
}
}
});

Related

Is there a way when we focus in input then it display in above of soft keyboard (WebView)?

I use HTML website to load in WebView, but when the user clicks in TextBox, then it hides under the keyboard. so what will be suggestion for doing this?
Staus Bar Present
If your activity is not a full screen activity then you can use android:windowSoftInputMode="adjustResize"or
android:windowSoftInputMode="adjustPan"
in your AndroidManifest.xml to achieve this.
For Full Screen Activity
First method will not work for full screen activity. So use have to create a utility
class for do this manually
Create public class WindowResize.java
public class WindowResize{
public static void assistActivity(Activity activity) {
new WindowResize(activity);
}
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private WindowResize(final Activity activity) {
FrameLayout content = activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent(activity);
}
});
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
private void possiblyResizeChildOfContent(Activity activity) {
//Visible height
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
//Total window size. We have to decrement the SoftNavigation button height from total height if there any
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight() - getNavigationButtonHeight(activity);
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard / 4)) {
// keyboard visible
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
} else {
// keyboard hidden
frameLayoutParams.height = usableHeightSansKeyboard;
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
//Usable height in current window.
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
//SoftInput navigation buttons height.
private int getNavigationButtonHeight(Activity activity) {
// getRealMetrics is only available with API 17 and +
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int usableHeight = metrics.heightPixels;
activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int realHeight = metrics.heightPixels;
if (realHeight > usableHeight)
return realHeight - usableHeight;
else
return 0;
}
}
add WindowResize.assistActivity(this); in main activity which contains corresponding webview.

How to get keyboard height when adjust nothing?

I am trying to get android keyboard height with following code.
The popupWindow solution doesn't work in some devices , is there another solution?
parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
parentLayout.getWindowVisibleDisplayFrame(r);
int screenHeight = parentLayout.getRootView().getHeight();
int heightDifference = screenHeight - (r.bottom);
previousHeightDiffrence = heightDifference;
if (heightDifference > 100) {
isKeyBoardVisible = true;
changeKeyboardHeight(heightDifference);
} else {
if(emojiKeyboard.getVisibility()==View.INVISIBLE){
emojiKeyboard.setVisibility(View.GONE);
}
isKeyBoardVisible = false;
}
}
});
I would go with insets solution - where rootWindowInsets.systemWindowInsetBottom would include keyboard if present. Take a look at documentation
final Window mRootWindow = getWindow();
ll_main.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
public void onGlobalLayout(){
int screenHeight = ll_main.getRootView().getHeight();
Rect r = new Rect();
View view = mRootWindow.getDecorView();
view.getWindowVisibleDisplayFrame(r);
int keyBoardHeight = screenHeight - r.bottom;
}
});
Full code on this link. this link also help for AdjustNothing.

How to listen soft keyboard's state changed event?

help,Android question:I hava to listen soft keyboard's state changed event ,but I could'nt find some api to use ?So , asking for help here,thanks..
You can use this function:
public void setListenerToRootView() {
ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int height = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (height > 100) {
isKeyboardVisible = true;
keyboardHeight = height;
} else {
isKeyboardVisible = false;
difHeight = height;
}
}
};
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
}

How to get the Height of Android Keyboard?

How get height of Android Keyboard?
I try:
KeyboardView keyboardView = new KeyboardView(_activity.getApplicationContext(), null);
Log.i("","xxx height " + keyboardCustom.mKeyboardView.getHeight());
Log.i("","xxx height " + keyboardCustom.mKeyboardView.getBottom());
But always get 0.
Use OnGlobalLayoutListener for getting Keyboard height or implement above code snippet
chatRootLayout is your xml root layout
pass this rootLayout as parentLayout parameter in checkKeyboardHeight
private void checkKeyboardHeight(final View parentLayout)
{
chatRootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
Rect r = new Rect();
chatRootLayout.getWindowVisibleDisplayFrame(r);
int screenHeight = chatRootLayout.getRootView().getHeight();
int keyboardHeight = screenHeight - (r.bottom);
if (previousHeightDiffrence - keyboardHeight > 50)
{
// Do some stuff here
}
previousHeightDiffrence = keyboardHeight;
if (keyboardHeight> 100)
{
isKeyBoardVisible = true;
changeKeyboardHeight(keyboardHeight);
}
else
{
isKeyBoardVisible = false;
}
}
});
}
Here is changeKeyboardHeight() method
private void changeKeyboardHeight(int height)
{
if (height > 100)
{
keyboardHeight = height;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, keyboardHeight);
yourLayout.setLayoutParams(params);
}
}

Android replace keyboard with Emoji Fragment

I'm making a little chat in my app and I have an Emoticon Fragment (just like the one from whatsapp or telegram). How can I switch between the fragment and the keyboard without any weird animation?
I already have the fragment wih the emojis and the custom EditText. I just want to switch beween that fragment and keyboard. I really want it to work like whatsapp or Telegram.
For the emoticon Fragment I made a library. I add a fragment (Grid view with SpannableTextViews for each emoji) in the same layout as the EditText.
Any help will be really appreciated.
You don't need to replace the keyboard, you can put your fragment over activity using PopupWindow as Telegram does. Just look at the source: method showEmojiPopup creates EmojiView and put it inside PopupWindow then calculates appropriate size and shows it.
emojiPopup.setHeight(View.MeasureSpec.makeMeasureSpec(currentHeight, View.MeasureSpec.EXACTLY));
emojiPopup.setWidth(View.MeasureSpec.makeMeasureSpec(contentView.getWidth(), View.MeasureSpec.EXACTLY));
emojiPopup.showAtLocation(parentActivity.getWindow().getDecorView(), 83, 0, 0);
You need to code :
public class EmojiKeyboard {
private static final String TAG = "EmojiKeyboard";
private static final String PREF_KEY_HEIGHT_KB = "EmojiKbHeight";
private Context context;
private int screenHeight = -1;
private int emojiKbHeight = -1;
private PopupWindow emojiKeyboardPopup;
private View view;
private SharedPreferences preferences;
public EmojiKeyboard(Context context, View view) {
if (context instanceof Activity) {
this.context = context;
this.view = view;
preferences = context.getSharedPreferences(context.getString(R.string.app_name), Context.MODE_PRIVATE);
//Restore EmojiKeyboard Height
emojiKbHeight = preferences.getInt(PREF_KEY_HEIGHT_KB, -1);
//TODO support less then 11 API, and not perfect resizing when switched the keyboard
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
/*
* Get root view height
* */
screenHeight = screenHeight == -1 && bottom > oldBottom
? bottom
: screenHeight;
/*
* Calculate soft keyboard height
* */
int dHeight = oldBottom - bottom;
boolean validHeight = emojiKbHeight == -1 && dHeight > 80 && bottom != oldBottom;
/*
* Сheck twice because the keyboard may have been switched
* */
emojiKbHeight = validHeight
? dHeight : emojiKbHeight != (dHeight) && dHeight > 0
? dHeight
: emojiKbHeight;
/*
* Store emoji keyboard height into SharedPreferences
* */
preferences.edit().putInt(PREF_KEY_HEIGHT_KB, emojiKbHeight).commit();
/*
* If layout returned to a standard height then dismissing keyboard (OnBackPressed)
* */
if (screenHeight == bottom) {
dismissEmojiKeyboard();
}
/*
* Resize emoji on the go when a user switches between keyboards
* */
resizeEmoji();
}
});
}
}
public void showEmoji() {
if (emojiKeyboardPopup == null) {
createEmojiKeyboard();
}
if (!isShowed()) {
new Handler().postDelayed(new Runnable() {
public void run() {
emojiKeyboardPopup.showAtLocation(view, Gravity.BOTTOM, 0, 0);
resizeEmoji();
}
}, 10L);
} else {
dismissEmojiKeyboard();
}
}
public void createEmojiKeyboard() {
EmojiView emojiKeyboard = new EmojiView(context, EmojiView.EMOJI_DARK_STYLE, new EmojiView.onEmojiClickListener() {
public void onBackspace() {
if (((Activity) context).getWindow().getCurrentFocus() instanceof EditText) {
((Activity) context).getWindow().getCurrentFocus().dispatchKeyEvent(new KeyEvent(0, 67));
}
}
public void onEmojiSelected(Emojicon emojicon) {
if (((Activity) context).getWindow().getCurrentFocus() instanceof EditText) {
EmojiView.input((EditText) ((Activity) context).getWindow().getCurrentFocus(), emojicon);
}
}
});
emojiKeyboardPopup = new PopupWindow(emojiKeyboard);
emojiKeyboardPopup.setHeight(View.MeasureSpec.makeMeasureSpec(setEmojiKeyboardHeight(), View.MeasureSpec.EXACTLY));
emojiKeyboardPopup.setWidth(View.MeasureSpec.makeMeasureSpec(getDisplayDimensions(context).x, View.MeasureSpec.EXACTLY));
emojiKeyboardPopup.setAnimationStyle(0);
}
public void dismissEmojiKeyboard() {
if (isShowed()) {
emojiKeyboardPopup.dismiss();
}
}
public boolean isShowed() {
return emojiKeyboardPopup != null && emojiKeyboardPopup.isShowing();
}
/*
* Emoji set up size
* */
public void resizeEmoji() {
if (isShowed()) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) emojiKeyboardPopup.getContentView().getLayoutParams();
layoutParams.height = setEmojiKeyboardHeight();
wm.updateViewLayout(emojiKeyboardPopup.getContentView(), layoutParams);
}
}
public int setEmojiKeyboardHeight() {
return emojiKbHeight == -1 && emojiKbHeight != screenHeight && emojiKbHeight < 80
? (getDisplayDimensions(context).y / 2)
: emojiKbHeight;
}
public Point getDisplayDimensions(Context context) {
Point size = new Point();
WindowManager w = ((Activity) context).getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(size);
} else {
Display d = w.getDefaultDisplay();
size.x = d.getWidth();
size.y = d.getHeight();
}
return size;
}
}
Solve problem

Categories

Resources