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

when richeditor in scrollview , the cursor will be below of keyboard #71

Open
oldfeel opened this issue Jul 28, 2016 · 24 comments
Open

Comments

@oldfeel
Copy link

oldfeel commented Jul 28, 2016

text is: cursor at here

9032b3ab0c4b45f518024d6b29201ad3

@SmiffyKMc
Copy link

I was having the same issue and added android:windowSoftInputMode="adjustResize" in my tag. Now the cursor will always stay above the keyboard and you can scroll etc fine.

@guobinAndroid
Copy link

I also encountered this problem, I would like to ask how to solve the problem,added android:windowSoftInputMode="adjustResize" is no use

@JasonLinkinBright
Copy link

The same problem!
Cursor is behind the softkeyboard.

@danieltigse
Copy link

android:windowSoftInputMode="adjustResize" doesn't work for me. Any other idea?

@ejwdhr
Copy link

ejwdhr commented Apr 28, 2018

Did you guys solved?

@ashish-bahl
Copy link

ashish-bahl commented May 15, 2018

I used this:
`editor.setOnTextChangeListener(new RichEditor.OnTextChangeListener() {

        @Override
        public void onTextChange(String text) {
            if (!text.isEmpty()) {
                mScroll.post(new Runnable() {
                    @Override
                    public void run() {
                        mScroll.fullScroll(View.FOCUS_DOWN);
                    }
                });
            } else {
                mScroll.post(new Runnable() {
                    @Override
                    public void run() {
                        mScroll.fullScroll(View.FOCUS_UP);
                    }
                });
            }
        }
    });`

where editor is Rich Editor and mScroll is the ScrollView

@dhruvkaushal11
Copy link

dhruvkaushal11 commented May 25, 2018

Thanks, @abahl817
But still it ruins everything when you try to edit in between text

The best thing I come up with was to remove the Editor from Scrollview

@beczesz
Copy link

beczesz commented Sep 4, 2018

I would need solution too to this one

@dhruvkaushal11
Copy link

@beczesz Don't put web view inside the scroll view.

@HaidyCao
Copy link

HaidyCao commented Jan 3, 2019

I found a solution:

        mRichEditor.setOnTextChangeListener(new RichEditor.OnTextChangeListener() {

        int oldScrollViewHeight = 0;
        int oldScrollY = 0;

        @Override
        public void onTextChange(String text) {
            if (text.isEmpty()) {
                return;
            }

            int scrollViewHeight = mScrollView.getChildAt(0).getHeight();
            int scrollY = mScrollView.getScrollY();

            if (oldScrollViewHeight != scrollViewHeight) {
                if (oldScrollViewHeight == 0) {
                    mScrollView.fullScroll(View.FOCUS_DOWN);
                } else {
                    int offset = scrollViewHeight - oldScrollViewHeight;
                    if (offset > 0) {
                        mScrollView.scrollBy(0, offset);
                    }
                }
            }

            oldScrollY = scrollY;
            oldScrollViewHeight = scrollViewHeight;
        }
    });

@rubendarioramirez
Copy link

None of the solutions worked for me.

@rubendarioramirez
Copy link

Thanks, @abahl817
But still it ruins everything when you try to edit in between text

The best thing I come up with was to remove the Editor from Scrollview

But how do you scroll the text later?

@Nivas78
Copy link

Nivas78 commented Jan 17, 2019

For me also getting same issue,After 15 lines Cursor is going Below keypad .

Any solution for this ,Any Idea..

@rubendarioramirez
Copy link

rubendarioramirez commented Jan 17, 2019

For me also getting same issue,After 15 lines Cursor is going Below keypad .

Any solution for this ,Any Idea..

I just found the solution. Just remove the scroll view, keep just the editor inside a constraint view, like this:

<jp.wasabeef.richeditor.RichEditor
    android:id="@+id/editor"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:background="?android:attr/colorBackground"
    app:layout_constraintBottom_toTopOf="@+id/adView_offile_story"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editorView"
    app:layout_constraintVertical_bias="0.0" />

Then go to your activity and add this line:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

That will do the work. I realized that the rich text editor it's already scrolling by adding another scrollview, it creates a conflict. I hope it works for you, it did for me.

Also if you want to have even more space, you can add more PADDING bottom when you initialize the editor, a value of 50 gives enough space to have always the cursor around middle of the screen.

@Nivas78
Copy link

Nivas78 commented Jan 24, 2019

For me also getting same issue,After 15 lines Cursor is going Below keypad .
Any solution for this ,Any Idea..

I just found the solution. Just remove the scroll view, keep just the editor inside a constraint view, like this:

<jp.wasabeef.richeditor.RichEditor
    android:id="@+id/editor"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:background="?android:attr/colorBackground"
    app:layout_constraintBottom_toTopOf="@+id/adView_offile_story"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editorView"
    app:layout_constraintVertical_bias="0.0" />

Then go to your activity and add this line:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

That will do the work. I realized that the rich text editor it's already scrolling by adding another scrollview, it creates a conflict. I hope it works for you, it did for me.

Also if you want to have even more space, you can add more PADDING bottom when you initialize the editor, a value of 50 gives enough space to have always the cursor around middle of the screen.

No luck,Still Same issue

@ashish-bahl
Copy link

ashish-bahl commented Jan 25, 2019

Thanks, @abahl817
But still it ruins everything when you try to edit in between text
The best thing I come up with was to remove the Editor from Scrollview

But how do you scroll the text later?

@rubendarioramirez I just leave the rest to the user ;p

@jasonSuzhow
Copy link

`/**

  • 让输入框在 软键盘上方
    */
    public class SoftHideKeyBoardUtil {
    public static void assistActivity(Activity activity) {
    new SoftHideKeyBoardUtil(activity);
    }

    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;
    //为适应华为小米等手机键盘上方出现黑条或不适配
    private int contentHeight;//获取setContentView本来view的高度
    private boolean isfirst = true;//只用获取一次
    private int statusBarHeight;//状态栏高度

    private SoftHideKeyBoardUtil(Activity activity) {
    //1、找到Activity的最外层布局控件,它其实是一个DecorView,它所用的控件就是FrameLayout
    FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
    //2、获取到setContentView放进去的View
    mChildOfContent = content.getChildAt(0);
    //3、给Activity的xml布局设置View树监听,当布局有变化,如键盘弹出或收起时,都会回调此监听
    mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    //4、软键盘弹起会使GlobalLayout发生变化
    public void onGlobalLayout() {
    if (isfirst) {
    contentHeight = mChildOfContent.getHeight();//兼容华为等机型
    isfirst = false;
    }
    //5、当前布局发生变化时,对Activity的xml布局进行重绘
    possiblyResizeChildOfContent();
    }
    });
    //6、获取到Activity的xml布局的放置参数
    frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }

    // 获取界面可用高度,如果软键盘弹起后,Activity的xml布局可用高度需要减去键盘高度
    private void possiblyResizeChildOfContent() {
    //1、获取当前界面可用高度,键盘弹起后,当前界面可用布局会减少键盘的高度
    int usableHeightNow = computeUsableHeight();
    //2、如果当前可用高度和原始值不一样
    if (usableHeightNow != usableHeightPrevious) {
    //3、获取Activity中xml中布局在当前界面显示的高度
    int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
    //4、Activity中xml布局的高度-当前可用高度
    int heightDifference = usableHeightSansKeyboard - usableHeightNow;
    //5、高度差大于屏幕1/4时,说明键盘弹出
    if (heightDifference > (usableHeightSansKeyboard / 4)) {
    // 6、键盘弹出了,Activity的xml布局高度应当减去键盘高度
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference + statusBarHeight;
    } else {
    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
    }
    } else {
    frameLayoutParams.height = contentHeight;
    }
    //7、 重绘Activity的xml布局
    mChildOfContent.requestLayout();
    usableHeightPrevious = usableHeightNow;
    }
    }

    private int computeUsableHeight() {
    Rect r = new Rect();
    mChildOfContent.getWindowVisibleDisplayFrame(r);
    // 全屏模式下:直接返回r.bottom,r.top其实是状态栏的高度
    return (r.bottom - r.top);
    }
    }`

test this SoftHideKeyBoardUtil.assistActivity(this) , it's ok for me

@ChanphengHor
Copy link

ViewTreeObserver observer = scrollView.getViewTreeObserver();
observer.addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener(){
@OverRide
public void onScrollChanged() {
scrollX = scrollView.getScrollX();
scrollY = scrollView.getScrollY();
}
});
//=======================================================/
mRichEditor.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@OverRide
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if(mEditor.getHtml() != null && !mEditor.getHtml().isEmpty()){
scrollView.scrollTo(0,scrollY + getDP(18));
}
}
});

@birdfly2014
Copy link

Has anybody solved it?

@rubendarioramirez
Copy link

Has anybody solved it?

Yes, with this: getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

@wenge8n
Copy link

wenge8n commented Sep 16, 2019

This one works without scrollview. But not working inside scrollview.
Anybody solved this issue inside scrollview? I need to use webview inside scrollview.

@yyyalmanci
Copy link

yyyalmanci commented Apr 12, 2020

class customEditor : RichEditor {

private lateinit var parentScrollView: NestedScrollView
private var scrollSize = 0

constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
    context,
    attrs,
    defStyleAttr
)

override fun onAttachedToWindow() {
    super.onAttachedToWindow()
    onChangeListener()
}

private fun onChangeListener() {
    this.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
        scrollSize = oldScrollY
        parentScrollView.onNestedScroll(parentScrollView, 0, 0, 0, scrollSize)
        //this for last line of editorr
    }
    parentScrollView.onLayoutChange { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
        parentScrollView.onNestedScroll(parentScrollView, 0, 0, 0, scrollSize)
        //this for different line at the editor
    }
}

fun setParentScrollView(parentScroll: NestedScrollView) {
    parentScrollView = parentScroll
}

}

this works fine guys you can try

@LongLongLongLongLongLongLongLongLong

getDP

请问 getDP(18)是什么意思,是计算RicheEditor中内容的高度吗?

@LongLongLongLongLongLongLongLongLong

`/**

  • 让输入框在 软键盘上方
    */
    public class SoftHideKeyBoardUtil {
    public static void assistActivity(Activity activity) {
    new SoftHideKeyBoardUtil(activity);
    }
    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;
    //为适应华为小米等手机键盘上方出现黑条或不适配
    private int contentHeight;//获取setContentView本来view的高度
    private boolean isfirst = true;//只用获取一次
    private int statusBarHeight;//状态栏高度
    private SoftHideKeyBoardUtil(Activity activity) {
    //1、找到Activity的最外层布局控件,它其实是一个DecorView,它所用的控件就是FrameLayout
    FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
    //2、获取到setContentView放进去的View
    mChildOfContent = content.getChildAt(0);
    //3、给Activity的xml布局设置View树监听,当布局有变化,如键盘弹出或收起时,都会回调此监听
    mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    //4、软键盘弹起会使GlobalLayout发生变化
    public void onGlobalLayout() {
    if (isfirst) {
    contentHeight = mChildOfContent.getHeight();//兼容华为等机型
    isfirst = false;
    }
    //5、当前布局发生变化时,对Activity的xml布局进行重绘
    possiblyResizeChildOfContent();
    }
    });
    //6、获取到Activity的xml布局的放置参数
    frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }
    // 获取界面可用高度,如果软键盘弹起后,Activity的xml布局可用高度需要减去键盘高度
    private void possiblyResizeChildOfContent() {
    //1、获取当前界面可用高度,键盘弹起后,当前界面可用布局会减少键盘的高度
    int usableHeightNow = computeUsableHeight();
    //2、如果当前可用高度和原始值不一样
    if (usableHeightNow != usableHeightPrevious) {
    //3、获取Activity中xml中布局在当前界面显示的高度
    int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
    //4、Activity中xml布局的高度-当前可用高度
    int heightDifference = usableHeightSansKeyboard - usableHeightNow;
    //5、高度差大于屏幕1/4时,说明键盘弹出
    if (heightDifference > (usableHeightSansKeyboard / 4)) {
    // 6、键盘弹出了,Activity的xml布局高度应当减去键盘高度
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference + statusBarHeight;
    } else {
    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
    }
    } else {
    frameLayoutParams.height = contentHeight;
    }
    //7、 重绘Activity的xml布局
    mChildOfContent.requestLayout();
    usableHeightPrevious = usableHeightNow;
    }
    }
    private int computeUsableHeight() {
    Rect r = new Rect();
    mChildOfContent.getWindowVisibleDisplayFrame(r);
    // 全屏模式下:直接返回r.bottom,r.top其实是状态栏的高度
    return (r.bottom - r.top);
    }
    }`

test this SoftHideKeyBoardUtil.assistActivity(this) , it's ok for me

对我有用,非常感谢,大佬。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests