Skip to content

Commit

Permalink
* Fix screen slide up on Android when keyboard show
Browse files Browse the repository at this point in the history
  • Loading branch information
mops committed Nov 15, 2018
1 parent b828d85 commit 60c8701
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 34 deletions.
Binary file modified Android/UnityMobileInput/.idea/caches/build_file_checksums.ser
Binary file not shown.
8 changes: 6 additions & 2 deletions Android/UnityMobileInput/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// ----------------------------------------------------------------------------
// The MIT License
// UnityMobileInput https://github.com/mopsicus/UnityMobileInput
// Copyright (c) 2018 Mopsicus <[email protected]>
// ----------------------------------------------------------------------------

package ru.mopsicus.mobileinput;

import org.json.JSONException;
import org.json.JSONObject;

import ru.mopsicus.common.Common;

public class KeyboardListener implements KeyboardObserver {

private boolean isPreviousState = false;
private Common common = new Common();

@Override
public void onKeyboardHeight(float height, int keyboardHeight, int orientation) {
boolean isShow = (keyboardHeight > 0);
JSONObject json = new JSONObject();
try {
json.put("msg", Plugin.KEYBOARD_ACTION);
json.put("show", isShow);
json.put("height", height);
} catch (JSONException e) {}
if (isPreviousState != isShow) {
isPreviousState = isShow;
common.sendData(Plugin.name, json.toString());
}
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// ----------------------------------------------------------------------------
// The MIT License
// UnityMobileInput https://github.com/mopsicus/UnityMobileInput
// Copyright (c) 2018 Mopsicus <[email protected]>
// ----------------------------------------------------------------------------

package ru.mopsicus.mobileinput;

public interface KeyboardObserver {
void onKeyboardHeight(float height, int keyboardHeight, int orientation);
}




Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// ----------------------------------------------------------------------------
// The MIT License
// UnityMobileInput https://github.com/mopsicus/UnityMobileInput
// Copyright (c) 2018 Mopsicus <[email protected]>
// ----------------------------------------------------------------------------

package ru.mopsicus.mobileinput;

import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.PopupWindow;

public class KeyboardProvider extends PopupWindow {

private KeyboardObserver observer;
private int keyboardLandscapeHeight;
private int keyboardPortraitHeight;
private View popupView;
private View parentView;
private Activity activity;

// Constructor
public KeyboardProvider(Activity activity, ViewGroup parent, KeyboardObserver listener) {
super(activity);
this.observer = listener;
this.activity = activity;
Resources resources = this.activity.getResources();
String packageName = this.activity.getPackageName();
int id = resources.getIdentifier("popup", "layout", packageName);
LayoutInflater inflator = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
this.popupView = inflator.inflate(id, null, false);
setContentView(popupView);
setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
parentView = parent;
setWidth(0);
setHeight(WindowManager.LayoutParams.MATCH_PARENT);
setBackgroundDrawable(new ColorDrawable(0));
showAtLocation(parentView, Gravity.NO_GRAVITY, 0, 0);
popupView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (popupView != null) {
handleOnGlobalLayout();
}
}
});
}

// Close fake popup
public void disable() {
dismiss();
}

// Return screen orientation
private int getScreenOrientation() {
return activity.getResources().getConfiguration().orientation;
}

// Handler to get keyboard height
private void handleOnGlobalLayout() {
Point screenSize = new Point();
activity.getWindowManager().getDefaultDisplay().getSize(screenSize);
Rect rect = new Rect();
popupView.getWindowVisibleDisplayFrame(rect);
int orientation = getScreenOrientation();
int keyboardHeight = screenSize.y - rect.bottom;
float height = keyboardHeight / (float) screenSize.y;
if (keyboardHeight == 0) {
notifyKeyboardHeight(0, 0, orientation);
} else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
this.keyboardPortraitHeight = keyboardHeight;
notifyKeyboardHeight(height, keyboardPortraitHeight, orientation);
} else {
this.keyboardLandscapeHeight = keyboardHeight;
notifyKeyboardHeight(height, keyboardLandscapeHeight, orientation);
}
}

// Send data observer
private void notifyKeyboardHeight(float height, int keyboardHeight, int orientation) {
if (observer != null) {
observer.onKeyboardHeight(height, keyboardHeight, orientation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,25 @@
package ru.mopsicus.mobileinput;

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import ru.mopsicus.common.Common;
import com.unity3d.player.UnityPlayer;

import org.json.JSONException;
import org.json.JSONObject;

import ru.mopsicus.common.Common;

public class Plugin {

public static String name = "mobileinput";

public static String KEYBOARD_ACTION = "KEYBOARD_ACTION";
public static Activity activity;
public static RelativeLayout layout;
public static Common common;
private static ViewGroup group;
private static boolean isPreviousState = true;
private static String KEYBOARD_ACTION = "KEYBOARD_ACTION";
private static KeyboardProvider keyboardProvider;
private static KeyboardListener keyboardListener;

// Get view recursive
private static View getLeafView(View view) {
Expand Down Expand Up @@ -58,33 +53,14 @@ public void run() {
if (layout != null) {
group.removeView(layout);
}
final ViewGroup rootView = (ViewGroup) activity.findViewById (android.R.id.content);
ViewGroup rootView = (ViewGroup) activity.findViewById (android.R.id.content);
View topMostView = getLeafView(rootView);
group = (ViewGroup) topMostView.getParent();
layout = new RelativeLayout(activity);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
group.addView(layout, params);
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
rootView.getWindowVisibleDisplayFrame(rect);
int screenHeight = rootView.getRootView().getHeight();
int keyboardHeight = screenHeight - rect.bottom;
boolean isShow = (keyboardHeight > screenHeight * 0.15);
float height = (float) keyboardHeight / (float) screenHeight;
JSONObject json = new JSONObject();
try {
json.put("msg", KEYBOARD_ACTION);
json.put("show", isShow);
json.put("height", height);
} catch (JSONException e) {}
if (isPreviousState != isShow) {
isPreviousState = isShow;
common.sendData(name, json.toString());
}
}
});
keyboardListener = new KeyboardListener();
keyboardProvider = new KeyboardProvider(activity, group, keyboardListener);
}
});
}
Expand All @@ -93,6 +69,9 @@ public void onGlobalLayout() {
public static void destroy() {
activity.runOnUiThread(new Runnable() {
public void run() {
keyboardProvider.disable();
keyboardProvider = null;
keyboardListener = null;
if (layout != null) {
group.removeView(layout);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/popup" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent">

</LinearLayout>
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ You can use default InputField UI component on iOS and Android `without addition
...
</activity>
```
6. To prevent screen slide up on `Android` when keyboard show, add this option to your `AndroidManifest.xml`

```xml
<activity ... android:windowSoftInputMode="adjustNothing">
...
</activity>
```

## Demo
Open Demo scene and build, to try how it works
Expand Down
2 changes: 1 addition & 1 deletion Unity/Assets/Plugins/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="26" />
<application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="true" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:launchMode="singleTask" android:process=":unityplayer">
<activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:launchMode="singleTask" android:process=":unityplayer" android:windowSoftInputMode="adjustNothing">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down
Binary file modified Unity/Assets/Plugins/Android/mobileinput-release.aar
Binary file not shown.
13 changes: 13 additions & 0 deletions Unity/Assets/Scripts/Demo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ public class Demo : MonoBehaviour {

public MobileInputField InputText;

void Start () {
MobileInput.OnPrepareKeyboard += OnPrepareKeyboard;
MobileInput.OnShowKeyboard += OnShowKeyboard;
}

public void OnReturn () {
Debug.Log ("OnReturn action");
}
Expand All @@ -22,4 +27,12 @@ public void SetTextData () {
InputText.Text = "Text by script";
}

void OnPrepareKeyboard () {
Debug.LogFormat ("Keyboad will show");
}

void OnShowKeyboard (bool isShow, int height) {
Debug.LogFormat ("Keyboad action, show = {0}, height = {1}", isShow, height);
}

}

0 comments on commit 60c8701

Please sign in to comment.