Skip to content

Commit

Permalink
fix: methods for more flexible keyboard height control #114
Browse files Browse the repository at this point in the history
  • Loading branch information
mopsicus committed Aug 27, 2024
1 parent 25b58ba commit 87cc9a3
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ private int getNavigationBarHeight() {
*/
}

/**
* Get height of navigation bar for custom behaviour
*/
public int getNavBarHeight() {
if (!checkSoftKeys() || getNavBarNavigationType() == 2) {
return 0;
}
Resources resources = activity.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}

/**
* Return type of screen navigation
* 0 : Navigation is displaying with 3 buttons
Expand All @@ -138,7 +153,7 @@ private int getNavigationBarHeight() {
* @return int of type
*/
@SuppressWarnings("unused")
private int getNavBarNavigationType() {
public int getNavBarNavigationType() {
Resources resources = activity.getResources();
@SuppressLint("DiscouragedApi") int resourceId = resources.getIdentifier("config_navBarInteractionMode", "integer", "android");
if (resourceId > 0) {
Expand Down
37 changes: 31 additions & 6 deletions Android~/plugin/src/main/java/com/mopsicus/umi/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.unity3d.player.UnityPlayer;

import org.json.JSONException;
Expand Down Expand Up @@ -57,21 +58,21 @@ public class Plugin {

/**
* Get view recursive
*
* @param view View to start search
* @return Last view
*/
private static View getLeafView(View view) {
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup)view;
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); ++i) {
View result = getLeafView(viewGroup.getChildAt(i));
if (result != null) {
return result;
}
}
return null;
}
else {
} else {
return view;
}
}
Expand All @@ -98,7 +99,7 @@ public static void init(final String data) {
if (layout != null) {
group.removeView(layout);
}
ViewGroup rootView = activity.findViewById (android.R.id.content);
ViewGroup rootView = activity.findViewById(android.R.id.content);
View topMostView = getLeafView(rootView);
group = (ViewGroup) topMostView.getParent();
layout = new RelativeLayout(activity);
Expand All @@ -110,6 +111,29 @@ public static void init(final String data) {
});
}

/**
* Get height of navigation bar
*
* @return int of value
*/
@SuppressWarnings("unused")
public static int getBarHeight() {
return keyboardProvider.getNavBarHeight();
}

/**
* Return type of screen navigation
* 0 : Navigation is displaying with 3 buttons
* 1 : Navigation is displaying with 2 button(Android P navigation mode)
* 2 : Full screen gesture(Gesture on android Q)
*
* @return int of type
*/
@SuppressWarnings("unused")
public static int getBarType() {
return keyboardProvider.getNavBarNavigationType();
}

/**
* Destroy plugin, remove layout
*/
Expand All @@ -128,7 +152,8 @@ public static void destroy() {

/**
* Send data to MobileInput
* @param id Input id
*
* @param id Input id
* @param data Data to process
*/
@SuppressWarnings("unused")
Expand All @@ -140,7 +165,7 @@ public static void execute(final int id, final String data) {
* Check device rotate locking
*/
@SuppressWarnings("unused")
public static boolean checkIsRotateLocked () {
public static boolean checkIsRotateLocked() {
int val = Settings.System.getInt(UnityPlayer.currentActivity.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
return (val != 1);
}
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file.

## [2.0.2] - 2024-08-27
- ### Added
- Custom caret/cursor/handles color
- Methods for more flexible keyboard height control #114
- ### Fixed
- Prevent double field initialization

## [2.0.1] - 2024-05-02
- ### Fixed
- Keyboard autohide on tap #110
Expand Down
9 changes: 9 additions & 0 deletions Documentation~/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ This script manages the interaction between the Unity app and native part. It co

`UpdateFonts()` – update fonts in app folder when changing fonts list

`IsRotationLocked()` – check is screen rotation locked (Android)

`GetBarType()` – get navigation bar type (Android)

`GetBarHeight()` – get navigation bar height, if exist (Android)

`Action<bool, int> OnKeyboardAction` and `Action<HardwareOrientation> OnOrientationChange` – two events that you can subscribe to and detect keyboard appearance and orientation changes

```csharp
Expand Down Expand Up @@ -120,6 +126,7 @@ Many options from `TMP Input Field` will be applied to the native field. You can
- text color
- placeholder text
- placeholder text color
- cursor/caret color
- character limit
- font size
- text align
Expand Down Expand Up @@ -171,6 +178,8 @@ Different native input types are used for multiline and singleline options: `UIT

When you get the keyboard height from `OnKeyboardAction`, you get the "true" height. To convert it to Unity UI height, you need to divide it by the screen aspect ratio. Screen aspect ratio is the ratio of the height of the screen to the height of the UI canvas and to the screen scale (for iOS).

For Android builds, you can use the `GetBarType()` and `GetBarHeight()` methods to determine the screen device parameters for more flexible control.

See [demo app](../Samples~/Demo/) with chat screen.


Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ In the inspector, you can edit several options that will be applied to the nativ
- text color
- placeholder text
- placeholder text color
- cursor/caret color
- character limit
- font size
- text align
Expand Down
1 change: 1 addition & 0 deletions README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public class Bootstrap : MonoBehaviour {

- цвет текста
- цвет текста подсказки
- цвет курсора/каретки
- текст подсказки
- лимит символов
- размер шрифта
Expand Down
34 changes: 29 additions & 5 deletions Runtime/MobileInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,35 @@ void OnDataReceive(string data) {
/// <summary>
/// Check if screen rotation locked
/// </summary>
bool IsRotatationLocked() {
public static bool IsRotationLocked() {
using (var plugin = new AndroidJavaClass(PLUGIN_PACKAGE)) {
return plugin.CallStatic<bool>("checkIsRotateLocked");
}
}

/// <summary>
/// Return type of screen navigation
///
/// 0 : Navigation is displaying with 3 buttons
/// 1 : Navigation is displaying with 2 button(Android P navigation mode)
/// 2 : Full screen gesture(Gesture on android Q)
/// </summary>
/// <returns>Type of navigation bar</returns>
public static int GetBarType() {
using (var plugin = new AndroidJavaClass(PLUGIN_PACKAGE)) {
return plugin.CallStatic<int>("getBarType");
}
}

/// <summary>
/// Get height of navigation bar
/// </summary>
/// <returns>Height of navbar</returns>
public static int GetBarHeight() {
using (var plugin = new AndroidJavaClass(PLUGIN_PACKAGE)) {
return plugin.CallStatic<int>("getBarHeight");
}
}
#endif

/// <summary>
Expand Down Expand Up @@ -427,12 +451,12 @@ public static void Destroy() {
/// Check screen scale factor (iOS)
/// </summary>
public static float GetScreenScale() {
#if UNITY_ANDROID
return 1f;
#elif UNITY_IOS
#if UNITY_IOS
return scaleFactor();
#else
return 1f;
#endif
}
}

/// <summary>
/// Update fonts
Expand Down

0 comments on commit 87cc9a3

Please sign in to comment.