Skip to content

Instantly share code, notes, and snippets.

@manwithsteelnerves
Created December 9, 2017 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save manwithsteelnerves/754220f3235567428c23188ab8b641e3 to your computer and use it in GitHub Desktop.
Save manwithsteelnerves/754220f3235567428c23188ab8b641e3 to your computer and use it in GitHub Desktop.
Webview Adjust Pan in Fullscreen Activity
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);
}
}
}
@manwithsteelnerves
Copy link
Author

public static int convertDpToPixels(Activity activity, float dp)
{
DisplayMetrics metrics = activity.getResources().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return Math.round(px);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment