Scroll when focusing on a Component X JAVA

2

I find myself with the following dilemma suppose I have a control in this case a EditText and below I have a Button which allows me to follow the course of the app,

Now my problem is that I want that when you focus or click the EditText my app scroll down to be able to see the button at the end, to not have to remove the keyboard to give the button, I leave a flow of images to understand it more clearly

Example 1 :

I put on the email, when doing this the keyboard will appear hiding the button that is behind,

What I would like is that when you type to write the email automatically, scroll upwards, something to be able to visualize the button to continue as I write, this only for the user's convenience, from now on thanks

VISTA XML

  <EditText
            android:id="@+id/txtCorreo"
            android:inputType="textEmailAddress"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="40dp"
            android:textSize="@dimen/T14"
            android:backgroundTint="@color/colorRayaEditText"
            android:textColor="@color/HintHomeMonto"/>



        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:onClick="GoToRegistryStep2"
                android:layout_centerHorizontal="true"
                android:layout_width="match_parent"
                android:layout_marginTop="5dp"
                android:background="@color/colorFondoHomeAzulado"
                android:text="@string/Continuar"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:id="@+id/iconodeaceptar"
                android:layout_height="40dp"
                android:textSize="@dimen/T14"
                android:textColor="@color/white"/>
        </RelativeLayout>

Methods you try so far

EditText txtCorreo = (EditText)findViewById(R.id.txtCorreo);
    txtCorreo.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (hasFocus) {
               final ScrollView sv = (ScrollView)findViewById(R.id.ScrollView01);
                sv.post(new Runnable() {
                    public void run() {
                        sv.fullScroll(sv.FOCUS_DOWN);
                    }
                });
            } else {
                Toast.makeText(getApplicationContext(), "Lost the focus", Toast.LENGTH_LONG).show();
            }
        }
    });

And also try

 txtCorreo.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (hasFocus) {
                ScrollView sv = (ScrollView)findViewById(R.id.ScrollView01);
                sv.scrollTo(0, sv.getBottom());
            } else {
                Toast.makeText(getApplicationContext(), "Lost the focus", Toast.LENGTH_LONG).show();
            }
        }
    });

preferably something that works for me from 4.1 Version onwards

Add the property to the manifest

 <activity android:name="banred.twoinnovateit.com.bimo_new.RegistroStep1"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustResize"
        >

    </activity>

but still the keyboard hides my button

    
asked by Bruno Sosa Fast Tag 23.02.2018 в 18:27
source

3 answers

3

Well, I think I have a good answer. First of all I have my Layout 'activity_main.xml'

<?xml version="1.0" encoding="utf-8"?>

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="24dp"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Name" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="205dp"
    android:layout_marginBottom="400dp"
    android:text="Texto"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="17dp"
    android:text="Mi Botón"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

Now in the MainActivity I initialize my Widgets.

scrollView - editText - textView - button

We set a listener for the editText

editText = (EditText)findViewById(R.id.editText);
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus){
                //almacenamos aquí el dispositivo que estamos utilizando para introducir información.
                InputMethodManager miTeclado = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
                //establecemos un scroll en pantalla
                scrollView.scrollTo(20, 300);
                //Ocultamos el teclado
                miTeclado.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
            }
        }
    });

There we have given a scroll to the screen when we do not have the Focus in the EditText.

Extra. Remove the Focus when the Enter Keypad is pressed. We create an internal class in the MainActivity that does that job.

class EventoTeclado implements TextView.OnEditorActionListener {

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE){ //Si se ha ocultado el teclado porque hemos pulsado el ok
            editText.clearFocus();
        }

        return false;
    }
}

We go to the onCreate of our MainActivity and instantiate this class.

EventoTeclado eventoTeclado = new EventoTeclado();

We listen to our editText to take effect.

editText.setOnEditorActionListener(eventoTeclado);

I add new method to get the position of the button

private Point obtenerPosicionDeLaVista(View view){
        int[] localizaccion = new int[2];
        view.getLocationInWindow(localizaccion);
        return new Point(localizaccion[0], localizaccion[1]);
    }

And now I send it to call ...

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus){
                //almacenamos aquí el dispositivo que estamos utilizando para introducir información.
                InputMethodManager miTeclado = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
                //establecemos un scroll en pantalla
                Point point = obtenerPosicionDeLaVista(button);
                scrollView.scrollTo(point.x, point.y);
                //Ocultamos el teclado
                miTeclado.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
            }
        }
    });
  

P.d. That's all friends:)

    
answered by 28.02.2018 / 20:02
source
1

To be able to do this, you have to give an id to the ScrollView and then call the ScrollView scroll method.

This is the definition of the scroll method:

scrollTo(int x, int y)
Set the scrolled position of your view.

This version also clamps the scrolling to the bounds of our child.

This information is obtained from: link

If you want to move it to "below", you would have to call the method in the following way:

ScrollView sv = (ScrollView)findViewById(R.id.scrollView);
sv.scrollTo(0, sv.getBottom());

This answer is taken from: link

    
answered by 26.02.2018 в 13:58
1

Add this in the xml of your manifest:

android:windowSoftInputMode="adjustResize"

The above works if there is enough space to "reduce". Otherwise try the following code that in ontouch in the last ediText scroll down:

tuUltimoEditText.setOnTouchListener(new View.OnTouchListener()
        {
            public boolean onTouch(View arg0, MotionEvent arg1)
            {
                ScrollView sv = (ScrollView)findViewById(R.id.scrollView);
                sv.scrollTo(0, sv.getBottom());
                return false;
            }
        });

In the xml all views must be within the relative or linearlayout and this in turn from ScrollView

    
answered by 25.02.2018 в 20:43