You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cordova.apache.org by anchann <gi...@git.apache.org> on 2015/10/14 07:02:52 UTC

[GitHub] cordova-plugins pull request: CB-6289 Keyboard plugin on Android

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
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org