Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText lost focus when scrollview fullscrolling

I have chat view, that have scroll view, that contanins textviews, and edit text above the scroll view. When new message print into textview, scroll view fullscrolling to end.

activity.scrollView.post(new Runnable() {
            @Override
            public void run() {
                activity.scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });

But if I typed text into EditText, it's always scrolling down and lose focus from edit text (so if messages printed often, it's unreal to type message). How can I fix it? I found in stack some answer, but it didn't help me. Here is it.

    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (hasFocus) {
                scrollView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                    }
                }, 200);
            } else {
            }
        }
    });

UPD: My textviews, that are child of scrollview, had property

textView.setTextIsSelectable(true);

I removed that property, and now it's OK, I can type message and autoscrolling does not prevent me. But it still question why that happened. Also I still would like the ability to select(copy) text.

like image 280
Dima Stoyanov Avatar asked Nov 08 '22 07:11

Dima Stoyanov


1 Answers

You can add a request for focus after the scroll event. You may need to wrap it in a runnable to wait for the scroll to finish, something like this:

scrollView.fullScroll(ScrollView.FOCUS_DOWN);
scrollView.post(new Runnable() {
    @Override
    public void run() {
        editText.requestFocus();
    }
});

See this post for more info.

like image 146
Trevor Avatar answered Dec 12 '22 04:12

Trevor