2

When user click on edittext I want the screen to scroll to show the edittext in the middle of the screen. So I tried to listen to touch (setOnTouchListener, also tried with onClick and onFocuse) and then smoothScrollTo screen to put the edittext in the middle of the screen.

but for some reason when I add the setOnTouchListener to the edittext it doesn't get focus at all.

what do I need to fix? or how can I achieve this?

this is the setOnTouchListener code that cause the edittext not to get focused when clicked:

    et_email.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ScrollView scrollView = (ScrollView)getView().findViewById(R.id.ScrollViewSendDetails);
            scrollView.smoothScrollTo(0, et_email.getBottom());
            return true;
        }
    });
Yuval Levy
  • 2,397
  • 10
  • 44
  • 73
  • You can use onfocuschanfelistener instade of setontouch listener. – aligur Dec 15 '15 at 20:08
  • You should really be asking the answerer who suggested you [this solution instead](http://stackoverflow.com/questions/34290504/scrollto-after-focus-onclick-event-works-only-after-second-click). This is creating unnecessary noise on SO. – Sufian Dec 16 '15 at 14:15
  • @Sufian so how do you recommend achieving this auto scroll? – Yuval Levy Dec 16 '15 at 14:48
  • What happens if you remove this `OnTouchListener` and add a click listener to your `EditText` instead? does it work? – Sufian Dec 16 '15 at 15:20

4 Answers4

6

Returning true from the onTouch() method indicates that the touch event is being consumed there. To allow the event to propagate through to the View's own touch listener, you need to return false;.

To get the EditText to gain focus after the ScrollView has finished its scroll, you can post a Runnable to the ScrollView's handler to request focus on the EditText. For example:

et_email.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ...
            scrollView.smoothScrollTo(0, et_email.getTop());
            scrollView.post(new Runnable() {
                    @Override
                    public void run() {
                        et_email.requestFocus();
                    }
                }
            );
            return false;
        }
    }
);
Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • so I tried this, now it get focused but only on the second click. first click on the edittext scroll the screen and second click get focus and open the keyboard – Yuval Levy Dec 16 '15 at 11:29
1

Use this line of code on your onCreate method . It gives you auto scrolling when you clicked on EditText :

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Mir Mahfuz
  • 663
  • 4
  • 9
0

Unfortunately output program still want double click to scroll

 final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollview);
            et_space.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    scrollView.smoothScrollTo(0, 500);
                    scrollView.post(new Runnable() {
                        @Override
                        public void run() {
                            et_space.requestFocus();

                        }
                    });
                    return false;
                }
            });
Emre Tekin
  • 462
  • 7
  • 18
0

First I would like to say you have to use "OnFocusListener" and then you have to use Handler like this;

Now, When I click my edittext; keyboard opened and scrollview scrolled to top at the same time :)

et_space.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                final Handler handler;
                handler = new Handler();

                final Runnable r = new Runnable() {
                    public void run() {
                        scrollView.smoothScrollTo(0, 500);
                        handler.postDelayed(this, 200);
                    }
                };
                handler.postDelayed(r, 200);
            }
        });
Emre Tekin
  • 462
  • 7
  • 18