I am faced 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 EditText
my app to scroll down when the focus is made or clicked so that I can see the button at the end, so as not to have to remove the keyboard to hit the button, I leave a flow of images so that be understood more clearly
Example 1 :
I put on the email, by doing this the keyboard will appear hiding the button that is behind,
What I would like is that when putting to write the mail automatically scrolls up, thing to be able to visualize the button to continue while I write, this only for the convenience of the user, thanks in advance
VIEW 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 I tried 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
I added the property in 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
Well, I think I have a good answer. First I have my Layout 'activity_main.xml'
Now in the MainActivity I initialize my Widgets.
We set a
listener
for the editTextThere we have given a scroll to the screen when we do not have the Focus on the EditText.
Extra. Remove Focus when keyboard Enter is pressed. We create an inner class in the MainActivity that does that work.
We go to
onCreate
oursMainActivity
and instantiate this class.We listen for our editText to take effect.
Added new method to get button position
And now I call him...
Add this in the xml of your manifest:
The above works if there is enough space to "shrink". Otherwise try the following code that ontouch in the last ediText scrolls down:
In the xml all the views must be inside the relative or linearlayout and this in turn of ScrollView
In order to do this, you have to give the ScrollView an id and then call the ScrollView's scroll method.
This is the definition of the scroll method:
This information is obtained from: https://developer.android.com/reference/android/widget/ScrollView.html
If you want to scroll it to "bottom", you would have to call the method like this:
This answer is taken from: https://stackoverflow.com/questions/6438061/can-i-scroll-a-scrollview-programmatically-in-android