Is there any way in Android to get the height of virtual keyboard of device
Yes you can, with the help of Viewtree Observer and global layout listener, just try below mentioned steps
- Get the root view of your layout
- get the Viewtree observer for this root, and add a global layout listener on top of this.
now whenever soft keyboard is displayed android will re-size your screen and you will receive call on your listener. That's it only thing you now need to do is calculate difference between height which your root view has after re-size and original size. If difference is more then 150 consider this as a keyboard has been inflated.
Below is a sample code
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
public void onGlobalLayout(){
int heightDiff = root.getRootView().getHeight()- root.getHeight();
// IF height diff is more then 150, consider keyboard as visible.
}
});
Regards,
Techfist
Get the height of virtual keyboard in Android
You can't get the keyboard height, but you can get the height of your View, which is what you really want - and you'll get this data supplied to the onLayout call into the current view.
How to get height of keyboard when activity created in android
I added global layout on root view and I was able to resize list view to same size as Keyboard.
The Pink Box is the resized ListView . I have tested this sample app on 2 Samsung devices and it work fine on Both.
Layout File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/edt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="text"
android:maxLines="1" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:visibility="gone" />
</LinearLayout>
Activity class
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.fet.minebeta.R;
public class ListActivity extends AppCompatActivity {
PagerAdapter adapterViewPager;
ViewPager viewPager;
private int heightDiff;
private ListView myListView;
private EditText editChatBox;
private boolean wasOpened;
private final int DefaultKeyboardDP = 100;
// Lollipop includes button bar in the root. Add height of button bar (48dp) to maxDiff
private final int EstimatedKeyboardDP = DefaultKeyboardDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
myListView = (ListView) findViewById(R.id.list);
editChatBox = (EditText) findViewById(R.id.edt);
//Listen for keyboard height change
setKeyboardListener();
// InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
// editChatBox.requestFocus();
// inputMethodManager.showSoftInput(editChatBox, 0);
//
// if (getCurrentFocus() != null) {
// inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
// inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
// }
//
// getWindow().setSoftInputMode(
// WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
// );
}
public final void setKeyboardListener() {
final View activityRootView = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
private final Rect r = new Rect();
@Override
public void onGlobalLayout() {
// Convert the dp to pixels.
int estimatedKeyboardHeight = (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, activityRootView.getResources().getDisplayMetrics());
// Conclude whether the keyboard is shown or not.
activityRootView.getWindowVisibleDisplayFrame(r);
heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
boolean isShown = heightDiff >= estimatedKeyboardHeight;
if (isShown == wasOpened) {
Log.d("Keyboard state", "Ignoring global layout change...");
return;
}
wasOpened = isShown;
if (isShown) {
//Set listview height
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = heightDiff;
myListView.setLayoutParams(params);
myListView.requestLayout();
myListView.setVisibility(View.VISIBLE);
Toast.makeText(ListActivity.this, "KeyBoard Open with height " + heightDiff +
"\n List View Height " + myListView.getHeight(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
Again this is just quick demo you can enhance things according to your need.
Getting the dimensions of the soft keyboard
We did it with this
myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
parent.getWindowVisibleDisplayFrame(r);
int screenHeight = parent.getRootView().getHeight();
int heightDifference = screenHeight - (r.bottom - r.top);
Log.d("Keyboard Size", "Size: " + heightDifference);
}
});
We only resize views with the keyboard, so we could use this.
How to get the Height of Android Keyboard?
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);
}
}
Related Topics
Calculate Compass Bearing/Heading to Location in Android
Scroll Recyclerview to Show Selected Item on Top
Asynctask Won't Stop Even When the Activity Has Destroyed
How to Change Default Dialog Button Text Color in Android 5
Android Linearlayout:Add Border with Shadow Around a Linearlayout
How to Increase Storage for Android Emulator? (Install_Failed_Insufficient_Storage)
Android Singletask or Singleinstance Launch Mode
How to Change Android Minsdkversion in Flutter Project
Viewpager as a Circular Queue/Wrapping
How to Resize a Custom View Programmatically
Navigationview Onnavigationitemselectedlistener Not Being Called
Listview Click to Show Image in Imageview
Valid Values for Android:Fontfamily and What They Map To
Enable/Disable Data Connection in Android Programmatically
Data Binding Class Not Generated
Android Studio - Unexpected Top-Level Exception:
How to Launch Activity Only Once When App Is Opened for First Time