2

When I touch an EditText I want my scrollView to scrolls some px. The problem is that it only works when I touch the respective EditText the second time and if the focus was on another editText. Here's the code:

KOTLIN:

usernameLogin.setOnFocusChangeListener(object: View.OnFocusChangeListener {
     override fun onFocusChange(view:View, hasFocus:Boolean) {
          if (hasFocus){
             scroll_login.scrollTo(0, 240);
          }else{
             Toast.makeText(getApplicationContext(), "Lost the focus", Toast.LENGTH_LONG).show()
         }
     }
 })

XML

<ScrollView
    android:id="@+id/scroll_login"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 ...


 <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/text_input_usernameLogin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/usernameLogin"
                    android:layout_width="match_parent"
                    android:layout_height="48dp"
                    android:hint="E-mail"
                    android:inputType="textEmailAddress" />

 </com.google.android.material.textfield.TextInputLayout>

 <com.google.android.material.textfield.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:passwordToggleEnabled="true">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="48dp"
                    android:hint="Password"
                    android:inputType="textPassword"/>

 </com.google.android.material.textfield.TextInputLayout>

 </ScrollView>
Pedro Relvas
  • 678
  • 7
  • 19

1 Answers1

3

Make sure that your EditText is not focused on default (for example when you switch to this fragment/activity) To prevent this, try to clearFocus() on that specific EditText or requestFocus() on something else

EDIT: I recently bumped into a similiar post here, hope it helps. link

qki
  • 1,769
  • 11
  • 25
  • Did you try for example forcing focus on to the next EditText and then immediately return focus back to first EditText? something like `myEditText2.requestFocus() myEditText2.clearFocus() myEditText1.requestFocus()` – qki Feb 05 '20 at 12:09
  • Yes, I've tried. The only away to work the first time is when I request the focus on the second editText, but I don't want it focused by default... – Pedro Relvas Feb 05 '20 at 12:19
  • But you can request focus on the second edit text and just after that request focus on the first edit text again - this way you will reset the focuses but the last focus would be still on the first one. That is if you don't have some onFocusEvents on the second EditText also – qki Feb 05 '20 at 12:23
  • But I don't want anything focused when the activity starts. Sorry for not tellin' you this aspect. – Pedro Relvas Feb 05 '20 at 12:41
  • Hi @qki. I know what the problem is by now, I just can't solve it yet. The problem resides in the system not knowing that we have a scrollView, because if I call the keyboard when activity starts, it do it what I want. – Pedro Relvas Feb 06 '20 at 11:10