Weird Behaviour Of TextInputEditText If OnFocusChangeListener Is Hooked
I have two EditText
in my Activity
, each of one is inside TextInputLayout
:
<android.support.design.widget.TextInputLayout
android:id="@+id/tilm"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_below="@+id/logo"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:textColorHint="#FFFFFF"
app:hintEnabled="false">
<android.support.design.widget.TextInputEditText
android:id="@+id/etlogin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/round_edittext"
android:hint="@string/email"
android:textColorHint="#FFFFFF"
android:gravity="center"
android:padding="5dp"
android:textColor="#FFFFFF"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/til"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_below="@+id/tilm"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
app:passwordToggleEnabled="true"
android:textColorHint="#FFFFFF"
app:hintEnabled="false">
<android.support.design.widget.TextInputEditText
android:id="@+id/etpass"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/round_edittext"
android:hint="@string/pass"
android:textColorHint="#FFFFFF"
android:gravity="center"
android:padding="5dp"
android:textColor="#FFFFFF"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
Also, in code, I have the same onFocusChangeListener
hooked:
focusChangeListener=new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
scrollView.postDelayed(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
}, 200);
}
}
};
(This is in order that when the keyboard appears, an ScrollView
that is in my layout goes down, so the user can see other elements of the UI that are below, buttons specifically).
Also, both TextInputEditText
have a TextWatcher
:
textWatcher=new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(ValidateLogin(login.getText().toString()) && pass.getText().length()>0){
entrar.setEnabled(true);//This is a button
}else{
if(entrar.isEnabled()){
entrar.setEnabled(false);
}
}
}
};
login.addTextChangedListener(textWatcher);
pass.addTextChangedListener(textWatcher);
login.setOnFocusChangeListener(focusChangeListener);
pass.setOnFocusChangeListener(focusChangeListener);
The problem is, when the second TextInputEditText
gain focus, the cursor goes automatically to the first one, and I can't type anything in the second one, since the focus goes immediately to the first one. I'd like this not to happen, keep my onFocusChangeListener
in both TextInputEditText
, and not change the focus automatically.
I have tried to make another OnFocusChangeListener
for the second TextInputEditText
, with no luck. If I remove the onFocusChangeListener
, the focus maintains in the TextInputEditText
, but obviously the ScrollView
doesn't moves.
Any help?
Thank you.
Answer
The documentation of ScrollView.fullScroll() says:
This method will scroll the view to the top or bottom and give the focus to the topmost/bottommost component in the new visible area.
The problem probably is that the first TextInputEditText
is the "topmost component in the new visible area" of the ScrollView
, hence it is given focus automatically.
Possible solutions could be:
Instead of
fullScroll
, useint scrollY = Math.max(0, content.getHeight() - scrollView.getHeight()); // where content is the content view of the scroll view scrollView.smoothScrollTo(0, scrollY); // or scrollTo if you don't want animation
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View