GithubHelp home page GithubHelp logo

samples-keyboardheight's Issues

fit more divices with modify the KeyboardHeightProvider handleOnGlobalLayout methods

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);
    }
}

android.view.InflateException: Binary XML file line #20: Binary XML file line #53: Error inflating class <unknown>

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

It can't work well for the Android P which contain the cutout mode.

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;
    }

`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.