Created
December 9, 2017 05:38
-
-
Save manwithsteelnerves/754220f3235567428c23188ab8b641e3 to your computer and use it in GitHub Desktop.
Webview Adjust Pan in Fullscreen Activity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.voxelbusters.nativeplugins.helpers; | |
import android.app.Activity; | |
import android.graphics.Rect; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.ViewTreeObserver; | |
import android.view.WindowManager; | |
import com.voxelbusters.nativeplugins.helpers.interfaces.IKeyboardListener; | |
import com.voxelbusters.nativeplugins.utilities.Debug; | |
import com.voxelbusters.nativeplugins.utilities.MiscUtilities; | |
import java.util.ArrayList; | |
/** | |
* Created by ayyappa on 02/12/17. | |
* Voxel Busters Interactive for u3d.as/bV0 | |
*/ | |
public class KeyboardHelper | |
{ | |
ArrayList<IKeyboardListener> listeners; | |
final float KEYBOARD_VISIBLE_THRESHOLD_IN_DP = 100; | |
ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener; | |
public KeyboardHelper(Activity rootActivity) | |
{ | |
listeners = new ArrayList<IKeyboardListener>(); | |
int softInputMode = rootActivity.getWindow().getAttributes().softInputMode; | |
registerViewObserver(rootActivity); | |
} | |
public void addListener(IKeyboardListener listener) | |
{ | |
if(!listeners.contains(listener)) | |
listeners.add(listener); | |
} | |
public void removeListener(IKeyboardListener listener) | |
{ | |
if(!listeners.contains(listener)) | |
listeners.remove(listener); | |
} | |
private void registerViewObserver(final Activity rootActivity) | |
{ | |
final View rootView = ((ViewGroup) rootActivity.findViewById(android.R.id.content)).getChildAt(0); | |
onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() | |
{ | |
private final Rect rect = new Rect(); | |
private boolean oldVisibilityState = false; | |
private final int visibleThreshold = MiscUtilities.convertDpToPixels(rootActivity, KEYBOARD_VISIBLE_THRESHOLD_IN_DP); | |
@Override | |
public void onGlobalLayout() | |
{ | |
rootView.getWindowVisibleDisplayFrame(rect); | |
int heightDiff = rootView.getRootView().getHeight() - rect.height(); | |
boolean isVisible = heightDiff > visibleThreshold; | |
if (isVisible != oldVisibilityState) | |
{ | |
notifyListeners(isVisible); | |
oldVisibilityState = isVisible; | |
} | |
} | |
}; | |
rootView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener); | |
} | |
private void notifyListeners(boolean isVisible) | |
{ | |
for(int i=0;i< listeners.size();i++) | |
{ | |
listeners.get(i).onKeyboardVisibilityChange(isVisible); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
public static int convertDpToPixels(Activity activity, float dp)
{
DisplayMetrics metrics = activity.getResources().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return Math.round(px);
}