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

[RNMobile] Ensure keyboard opens after requesting focus on Android #57543

Merged
merged 3 commits into from
Jan 4, 2024
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.wordpress.mobile.ReactNativeAztec;

import static android.content.ClipData.Item;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
Expand All @@ -10,18 +12,19 @@
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.text.Editable;
import android.text.InputType;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.uimanager.ThemedReactContext;
Expand All @@ -41,12 +44,10 @@

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.HashSet;
import java.util.HashMap;

import static android.content.ClipData.*;

public class ReactAztecText extends AztecText {

Expand All @@ -64,6 +65,7 @@ public class ReactAztecText extends AztecText {
private @Nullable TextWatcherDelegator mTextWatcherDelegator;
private @Nullable ContentSizeWatcher mContentSizeWatcher;
private @Nullable ScrollWatcher mScrollWatcher;
private @Nullable Runnable mKeyboardRunnable;

// FIXME: Used in `incrementAndGetEventCounter` but never read. I guess we can get rid of it, but before this
// check when it's used in EditText in RN. (maybe tests?)
Expand Down Expand Up @@ -264,18 +266,46 @@ public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
}

private void showSoftKeyboard() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
// If the text input is already focused we can show the keyboard.
if(hasWindowFocus()) {
showSoftKeyboardNow();
}
// Otherwise, we'll wait until it gets focused.
else {
getViewTreeObserver().addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() {
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus) {
showSoftKeyboardNow();
getViewTreeObserver().removeOnWindowFocusChangeListener(this);
}
}
});
}
}

private void showSoftKeyboardNow() {
// Cancel any previously scheduled Runnable
if (mKeyboardRunnable != null) {
removeCallbacks(mKeyboardRunnable);
}

mKeyboardRunnable = new Runnable() {
@Override
public void run() {
if (mInputMethodManager != null) {
mInputMethodManager.showSoftInput(ReactAztecText.this, 0);
mInputMethodManager.showSoftInput(ReactAztecText.this, InputMethodManager.SHOW_IMPLICIT);
}
}
});
};

post(mKeyboardRunnable);
}

private void hideSoftKeyboard() {
mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
if (mInputMethodManager != null) {
mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
}
}

public void setScrollWatcher(ScrollWatcher scrollWatcher) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ protected void onCreate(Bundle savedInstanceState) {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
linearLayout.setFocusable(false);
linearLayout.setFocusableInTouchMode(true);

// Create a Toolbar instance
Toolbar toolbar = new Toolbar(this);
Expand Down
Loading