siebeprojects / samples-keyboardheight Goto Github PK
View Code? Open in Web Editor NEWCalculates the height of a floating soft input Android keyboard
Calculates the height of a floating soft input Android keyboard
I have a fragment in activity and I use keyboardHeightProvider in that activity.
I have crashed to constructor code line:
this.popupView = LayoutInflater.from(MyApplication.getContext()).inflate(layout, (ViewGroup) parentView, false);
MyActivity
public class MyActivity extends ActivityCreator implements KeyboardHeightObserver {
public static final String TAG = MyActivity.class.getSimpleName();
private MyFragment myFragment;
private KeyboardHeightProvider keyboardHeightProvider;
void initializeViews() {
edtWriteText = ViewUtil.findById(this, R.id.edtWriteText);
keyboardHeightProvider = new KeyboardHeightProvider(R.layout.my_activity, this);
//listen keyboard
edtWriteText.post(new Runnable() {
@Override
public void run() {
keyboardHeightProvider.start();
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState, int count) {
supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR_OVERLAY);
setContentView(R.layout.my_activity);
setFragment();
}
private void setFragment(){
myFragment = (MyFragment) getSupportFragmentManager().findFragmentById(R.id.my_fragment);
}
}
MyFragment
public class MyFragment extends Fragment {
private MyActivity myActivity;
public static String TAG = MyFragment.class.getSimpleName();
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.myfragment, container, false);
return rootView;
}
}
my_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/topBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginLeft="-15dp"
android:layout_marginStart="-15dp"
android:background="@color/topBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<include layout="@layout/top_bar" />
</android.support.v7.widget.Toolbar>
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/topBar"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="vertical">
<fragment
android:id="@+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:name="com.android.android.MyFragment"
android:layout_weight="1" />
<include layout="@layout/bottom_view" />
</LinearLayout>
</RelativeLayout>
My error log:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.android/com.android.android.MyActivity}: android.view.InflateException: Binary XML file line #20: Binary XML file line #53: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: android.view.InflateException: Binary XML file line #20: Binary XML file line #53: Error inflating class <unknown>
Caused by: android.view.InflateException: Binary XML file line #53: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:652)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:724)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:792)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:873)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:873)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1011)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:873)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.inflate(LayoutInflater.java:525)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at com.android.android.util.keyboard.KeyboardHeightProvider.<init>(KeyboardHeightProvider.java:54)
at com.android.android.MyActivity.initializeViews(MyActivity.java:79)
at com.android.android.MyActivity.setFragment(MyActivity.java:161)
at com.android.android.MyActivity.onCreate(MyActivity.java:98)
at com.android.android.util.functions.ActivityCreator.onCreate(ActivityCreator.java:52)
at android.app.Activity.performCreate(Activity.java:6942)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
12-14 15:56:43.069 24581-24581/com.juphoon.justalk E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 6: TypedValue{t=0x2/d=0x101009b a=1}
at android.content.res.TypedArray.getColorStateList(TypedArray.java:545)
at android.widget.TextView.<init>(TextView.java:1088)
at android.widget.TextView.<init>(TextView.java:989)
at android.widget.TextView.<init>(TextView.java:985)
... 38 more
https://play.google.com/store/apps/details?id=com.fooview.android.fooview&hl=zh
when use with FooView, PopWindow can not receive any callbacks
Only tested this phone, but onKeyboardHeightChanged
isn't called at all when in landscape.
How to make it consistent for hide the blue background when keyboard close.
Our suggestion is :
`
int orientation = getScreenOrientation();
int topCutoutHeight = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
topCutoutHeight = getTopCutoutHeight();
}
int keyboardHeight = screenSize.y + topCutoutHeight - rect.bottom;
@TargetApi(android.os.Build.VERSION_CODES.P)
private int getTopCutoutHeight(){
View decorView = activity.getWindow().getDecorView();
if(decorView == null){
return 0;
}
int cutOffHeight = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
WindowInsets windowInsets = decorView.getRootWindowInsets();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
DisplayCutout displayCutout = windowInsets.getDisplayCutout();
if(displayCutout != null){
List<Rect> list = displayCutout.getBoundingRects();
for(Rect rect : list){
if(rect.top == 0){
cutOffHeight += rect.bottom - rect.top;
}
}
}
}
}
return cutOffHeight;
}
`
when i test this code in the xiaomi devices,i find the height is not accurate。in some case,the activity window size is too larger include the top status bar,in other case, the window visible display frame is too larger include the botom navigate bar。so i chang the code like this,not use the absoulte screen height,rather than use the relative height,save the first bottom,then calculate the change when the keboard pop up
private void handleOnGlobalLayout() {
Point screenSize = new Point();
activity.getWindowManager().getDefaultDisplay().getSize(screenSize);
Rect rect = new Rect();
popupView.getWindowVisibleDisplayFrame(rect);
// REMIND, you may like to change this using the fullscreen size of the phone
// and also using the status bar and navigation bar heights of the phone to calculate
// the keyboard height. But this worked fine on a Nexus.
int orientation = getScreenOrientation();
if (lastY == 0) {
lastY = rect.bottom;
}
int keyboardHeight = lastY - rect.bottom;
if (keyboardHeight == 0) {
notifyKeyboardHeightChanged(0, orientation);
}
else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
this.keyboardPortraitHeight = keyboardHeight;
notifyKeyboardHeightChanged(keyboardPortraitHeight, orientation);
}
else {
this.keyboardLandscapeHeight = keyboardHeight;
notifyKeyboardHeightChanged(keyboardLandscapeHeight, orientation);
}
}
In HUAWEI ELE-AL00,android 9,api28。
onKeyboardHeightChanged always not be called,when use adjustNothing
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.