Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/android margin adjustment for keyboard #300

Merged
merged 2 commits into from
Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified dist/unity-webview.unitypackage
Binary file not shown.
Binary file modified dist/unity-webview.zip
Binary file not shown.
38 changes: 37 additions & 1 deletion plugins/WebViewObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public class WebViewObject : MonoBehaviour
Callback onError;
Callback onLoaded;
bool visibility;
int mMarginLeft;
int mMarginTop;
int mMarginRight;
int mMarginBottom;
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
IntPtr webView;
Rect rect;
Expand All @@ -60,12 +64,40 @@ public class WebViewObject : MonoBehaviour
#elif UNITY_ANDROID
AndroidJavaObject webView;

bool mIsKeyboardVisible0 = false;
bool mIsKeyboardVisible = false;

/// Called from Java native plugin to set when the keyboard is opened
public void SetKeyboardVisible(string pIsVisible)
{
mIsKeyboardVisible = (pIsVisible == "true");
if (mIsKeyboardVisible != mIsKeyboardVisible0)
{
mIsKeyboardVisible0 = mIsKeyboardVisible;
SetMargins(mMarginLeft, mMarginTop, mMarginRight, mMarginBottom);
}
}

public int AdjustBottomMargin(int bottom)
{
if (!mIsKeyboardVisible)
{
return bottom;
}
else
{
int keyboardHeight = 0;
using(AndroidJavaClass UnityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
AndroidJavaObject View = UnityClass.GetStatic<AndroidJavaObject>("currentActivity").Get<AndroidJavaObject>("mUnityPlayer").Call<AndroidJavaObject>("getView");
using(AndroidJavaObject Rct = new AndroidJavaObject("android.graphics.Rect"))
{
View.Call("getWindowVisibleDisplayFrame", Rct);
keyboardHeight = View.Call<int>("getHeight") - Rct.Call<int>("height");
}
}
return (bottom > keyboardHeight) ? bottom : keyboardHeight;
}
}
#else
IntPtr webView;
Expand Down Expand Up @@ -376,8 +408,12 @@ public void SetMargins(int left, int top, int right, int bottom)
#elif UNITY_ANDROID
if (webView == null)
return;
webView.Call("SetMargins", left, top, right, bottom);
webView.Call("SetMargins", left, top, right, AdjustBottomMargin(bottom));
#endif
mMarginLeft = left;
mMarginTop = top;
mMarginRight = right;
mMarginBottom = bottom;
}

public void SetVisibility(bool v)
Expand Down