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
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;
}
}
);
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);
}
});
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);
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;
}
});