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.
Related
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.
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");
}
}
});
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 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);
}
}
I want to use QuickAction dialog in my learning App, but I don't know how to change the size of the dialog. Now, the dialog is close to phone border, but I want to left blank between borders or reduce the width.
Thanks~
call setRootViewId(R.layout.notify_quick_action); at constructor.
public void setRootViewId(int id) {
mRootView = (ViewGroup) inflater.inflate(id, null);
mTrack = (ViewGroup) mRootView.findViewById(R.id.tracks);
mArrowDown = (ImageView) mRootView.findViewById(R.id.arrow_down);
mArrowUp = (ImageView) mRootView.findViewById(R.id.arrow_up);
mRootView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
setContentView(mRootView);
}
call show() at setOnItemClickListener()
public void show (View anchor) {
preShow();
int[] location = new int[2];
mDidAction = false;
anchor.getLocationOnScreen(location);
Rect anchorRect = new Rect(location[0], location[1], location[0] + anchor.getWidth(), location[1]
+ anchor.getHeight());
mRootView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int rootWidth = mRootView.getMeasuredWidth();
int rootHeight = mRootView.getMeasuredHeight();
int screenWidth = mWindowManager.getDefaultDisplay().getWidth();
int xPos = (screenWidth - rootWidth) / 2;
int yPos = anchorRect.top - rootHeight;
boolean onTop = true;
if (rootHeight > anchor.getTop()) {
yPos = anchorRect.bottom;
onTop = false;
}
showArrow(((onTop) ? R.id.arrow_down : R.id.arrow_up), anchorRect.centerX()+180);
setAnimationStyle(screenWidth, anchorRect.centerX(), onTop);
Log.d("-------", "xPos="+xPos+";yPos="+yPos+";rootWidth="+rootWidth+";rootHeight="+rootHeight+";screenWidth="+screenWidth);
mWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, 100, yPos);
if (mAnimateTrack) mTrack.startAnimation(mTrackAnim);
}
Just use custom PopupWindow. Yeah, here link:
https://androidresearch.wordpress.com/2012/05/06/how-to-create-popups-in-android/