Reputation: 2516
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;
}
});
Upvotes: 2
Views: 12645
Reputation: 733
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);
Upvotes: 1
Reputation: 472
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);
}
});
Upvotes: 0
Reputation: 472
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;
}
});
Upvotes: 0
Reputation: 39201
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;
}
}
);
Upvotes: 6