[ 
https://issues.apache.org/jira/browse/CB-6289?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14956271#comment-14956271
 ] 

ASF GitHub Bot commented on CB-6289:
------------------------------------

Github user anchann commented on the pull request:

    https://github.com/apache/cordova-plugins/pull/15#issuecomment-147935881
  
    I just ended up implementing this by adopting the ionic version of the 
keyboard plugin (which seems to be the inspiration for this PR as well). Was 
going to try to contribute back, but seeing as there's already a PR for this 
feature, not going to. Still, I feel that my implementation is ever so slightly 
cleaner, as it does not assume any keyboard size threshold, so pasting it below 
in case the author wants to adopt.
    
    ```
    public class Keyboard extends CordovaPlugin {
        public void initialize(CordovaInterface cordova, CordovaWebView 
webView) {
                super.initialize(cordova, webView);
    
                final CordovaWebView appView = webView;
    
                final View rootView = 
cordova.getActivity().getWindow().getDecorView().findViewById(android.R.id.content).getRootView();
                OnGlobalLayoutListener listener = new OnGlobalLayoutListener() {
                        double heightRatioWithKeyboard = 0;
                        double heightRatioWithoutKeyboard = 0;
                        Set<Double> heightRatios = new HashSet<Double>();
    
                        double lastHeightRatio = 0;
    
                        @Override
                        public void onGlobalLayout() {
                                Rect r = new Rect();
                                //r will be populated with the coordinates of 
your view that area still visible.
                                rootView.getWindowVisibleDisplayFrame(r);
    
                                int currTotalHeight = 
rootView.getRootView().getHeight();
                                int currUsableHeight = (r.bottom - r.top);
                                double currHeightRatio = 
((double)currUsableHeight) / currTotalHeight;
    
                                if (!heightRatios.contains(currHeightRatio)) {
                                        heightRatios.add(currHeightRatio);
                                        heightRatioWithKeyboard = 
Collections.min(heightRatios);
                                        heightRatioWithoutKeyboard = 
Collections.max(heightRatios);
                                }
    
                                if (heightRatios.size() >= 2 && currHeightRatio 
!= lastHeightRatio) {
                                        if (currHeightRatio == 
heightRatioWithoutKeyboard) {
                                                
appView.sendJavascript("window.Keyboard.fireOnHide();");
                                        }
                                        else if (currHeightRatio == 
heightRatioWithKeyboard) {
                                                
appView.sendJavascript("window.Keyboard.fireOnShow();");
                                        }
                                }
    
                                lastHeightRatio = currHeightRatio;
                         }
                };
    
                // fire once to get the initial ratio
                listener.onGlobalLayout();
    
                
rootView.getViewTreeObserver().addOnGlobalLayoutListener(listener);
        }
    
        // the rest is unchanged
    ```


> Keyboard for Android
> --------------------
>
>                 Key: CB-6289
>                 URL: https://issues.apache.org/jira/browse/CB-6289
>             Project: Apache Cordova
>          Issue Type: Improvement
>          Components: Plugins
>         Environment: Android
>            Reporter: Andrey Kurdyumov
>            Priority: Minor
>              Labels: keyboard-plugin
>
> Please extend the Keyboard plugin to support Android platform.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to